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:
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.
15 #include <vlib/vlib.h>
16 #include <vnet/vnet.h>
17 #include <vnet/pg/pg.h>
18 #include <vppinfra/error.h>
20 #include <vnet/ip/ip.h>
22 #include <vppinfra/hash.h>
23 #include <vppinfra/error.h>
24 #include <vppinfra/elog.h>
26 #include <vnet/ip/ip6_hop_by_hop.h>
28 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
29 * and nanoseconds respectively.
31 static f64 trace_tsp_mul[4] = {1, 1e3, 1e6, 1e9};
33 char *ppc_state[] = {"None", "Encap", "Decap"};
35 ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main;
37 #define foreach_ip6_hbyh_ioam_input_next \
38 _(IP6_REWRITE, "ip6-rewrite") \
39 _(IP6_LOOKUP, "ip6-lookup") \
43 #define _(s,n) IP6_HBYH_IOAM_INPUT_NEXT_##s,
44 foreach_ip6_hbyh_ioam_input_next
46 IP6_HBYH_IOAM_INPUT_N_NEXT,
47 } ip6_hbyh_ioam_input_next_t;
55 fetch_trace_data_size(u8 trace_type)
57 u8 trace_data_size = 0;
59 if (trace_type == TRACE_TYPE_IF_TS_APP)
60 trace_data_size = sizeof(ioam_trace_if_ts_app_t);
61 else if(trace_type == TRACE_TYPE_IF)
62 trace_data_size = sizeof(ioam_trace_if_t);
63 else if(trace_type == TRACE_TYPE_TS)
64 trace_data_size = sizeof(ioam_trace_ts_t);
65 else if(trace_type == TRACE_TYPE_APP)
66 trace_data_size = sizeof(ioam_trace_app_t);
67 else if(trace_type == TRACE_TYPE_TS_APP)
68 trace_data_size = sizeof(ioam_trace_ts_app_t);
70 return trace_data_size;
73 static u8 * format_ioam_data_list_element (u8 * s, va_list * args)
75 u32 *elt = va_arg (*args, u32 *);
76 u8 *trace_type_p = va_arg (*args, u8 *);
77 u8 trace_type = *trace_type_p;
80 if (trace_type & BIT_TTL_NODEID)
82 u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
83 s = format (s, "ttl 0x%x node id 0x%x ",
84 ttl_node_id_host_byte_order>>24,
85 ttl_node_id_host_byte_order & 0x00FFFFFF);
90 if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
92 u32 ingress_host_byte_order = clib_net_to_host_u32(*elt);
93 s = format (s, "ingress 0x%x egress 0x%x ",
94 ingress_host_byte_order >> 16,
95 ingress_host_byte_order &0xFFFF);
99 if (trace_type & BIT_TIMESTAMP)
101 u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
102 s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
106 if (trace_type & BIT_APPDATA)
108 u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
109 s = format (s, "app 0x%x ", appdata_in_host_byte_order);
117 ip6_hbh_ioam_trace_data_list_trace_handler (u8 *s, ip6_hop_by_hop_option_t *opt)
119 ioam_trace_option_t *trace;
120 u8 trace_data_size_in_words = 0;
124 trace = (ioam_trace_option_t *)opt;
126 s = format (s, " Trace Type 0x%x , %d elts left ts msb(s) 0x%x\n", trace->ioam_trace_type, trace->data_list_elts_left,
129 s = format (s, " Trace Type 0x%x , %d elts left\n", trace->ioam_trace_type, trace->data_list_elts_left);
130 trace_data_size_in_words = fetch_trace_data_size(trace->ioam_trace_type)/4;
131 elt = &trace->elts[0];
132 while ((u8 *) elt < ((u8 *)(&trace->elts[0]) + trace->hdr.length - 2
133 /* -2 accounts for ioam_trace_type,elts_left */)) {
134 s = format (s, " [%d] %U\n",elt_index,
135 format_ioam_data_list_element,
136 elt, &trace->ioam_trace_type);
138 elt += trace_data_size_in_words;
144 ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t *b, ip6_header_t *ip, ip6_hop_by_hop_option_t *opt)
146 ip6_main_t * im = &ip6_main;
147 ip_lookup_main_t * lm = &im->lookup_main;
148 ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
150 ioam_trace_option_t *trace = (ioam_trace_option_t *)opt;
151 u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
152 ip_adjacency_t *adj = ip_get_adjacency (lm, adj_index);
159 if (PREDICT_TRUE (trace->data_list_elts_left)) {
160 trace->data_list_elts_left--;
161 /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
162 * to skip to this node's location.
164 elt_index = trace->data_list_elts_left * fetch_trace_data_size(trace->ioam_trace_type) / 4;
165 elt = &trace->elts[elt_index];
166 if (trace->ioam_trace_type & BIT_TTL_NODEID) {
167 *elt = clib_host_to_net_u32 ((ip->hop_limit<<24) | hm->node_id);
171 if (trace->ioam_trace_type & BIT_ING_INTERFACE) {
172 *elt = (vnet_buffer(b)->sw_if_index[VLIB_RX]&0xFFFF) << 16 | (adj->rewrite_header.sw_if_index & 0xFFFF);
173 *elt = clib_host_to_net_u32(*elt);
177 if (trace->ioam_trace_type & BIT_TIMESTAMP) {
178 /* Send least significant 32 bits */
179 f64 time_f64 = (f64)(((f64)hm->unix_time_0) + (vlib_time_now(hm->vlib_main) - hm->vlib_time_0));
181 time_u64.as_u64 = time_f64 * trace_tsp_mul[hm->trace_tsp];
182 *elt = clib_host_to_net_u32(time_u64.as_u32[0]);
186 if (trace->ioam_trace_type & BIT_APPDATA) {
187 /* $$$ set elt0->app_data */
188 *elt = clib_host_to_net_u32(hm->app_data);
195 /* The main h-b-h tracer will be invoked, no need to do much here */
197 ip6_hbh_add_register_option (u8 option,
199 int rewrite_options(u8 *rewrite_string, u8 rewrite_size))
201 ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
203 ASSERT (option < ARRAY_LEN (hm->add_options));
205 /* Already registered */
206 if (hm->add_options[option])
209 hm->add_options[option] = rewrite_options;
210 hm->options_size[option] = size;
216 ip6_hbh_add_unregister_option (u8 option)
218 ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
220 ASSERT (option < ARRAY_LEN (hm->add_options));
223 if (!hm->add_options[option])
226 hm->add_options[option] = NULL;
227 hm->options_size[option] = 0;
233 } ip6_add_hop_by_hop_trace_t;
235 /* packet trace format function */
236 static u8 * format_ip6_add_hop_by_hop_trace (u8 * s, va_list * args)
238 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
239 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
240 ip6_add_hop_by_hop_trace_t * t = va_arg (*args,
241 ip6_add_hop_by_hop_trace_t *);
243 s = format (s, "IP6_ADD_HOP_BY_HOP: next index %d",
248 vlib_node_registration_t ip6_add_hop_by_hop_node;
250 #define foreach_ip6_add_hop_by_hop_error \
251 _(PROCESSED, "Pkts w/ added ip6 hop-by-hop options")
254 #define _(sym,str) IP6_ADD_HOP_BY_HOP_ERROR_##sym,
255 foreach_ip6_add_hop_by_hop_error
257 IP6_ADD_HOP_BY_HOP_N_ERROR,
258 } ip6_add_hop_by_hop_error_t;
260 static char * ip6_add_hop_by_hop_error_strings[] = {
261 #define _(sym,string) string,
262 foreach_ip6_add_hop_by_hop_error
267 ip6_add_hop_by_hop_node_fn (vlib_main_t * vm,
268 vlib_node_runtime_t * node,
269 vlib_frame_t * frame)
271 ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
272 u32 n_left_from, * from, * to_next;
273 ip_lookup_next_t next_index;
275 u8 * rewrite = hm->rewrite;
276 u32 rewrite_length = vec_len (rewrite);
278 from = vlib_frame_vector_args (frame);
279 n_left_from = frame->n_vectors;
280 next_index = node->cached_next_index;
282 while (n_left_from > 0)
286 vlib_get_next_frame (vm, node, next_index,
287 to_next, n_left_to_next);
288 while (n_left_from >= 4 && n_left_to_next >= 2)
291 vlib_buffer_t * b0, *b1;
293 ip6_header_t * ip0, *ip1;
294 ip6_hop_by_hop_header_t * hbh0, *hbh1;
295 u64 * copy_src0, * copy_dst0, *copy_src1, *copy_dst1;
298 /* Prefetch next iteration. */
300 vlib_buffer_t * p2, * p3;
302 p2 = vlib_get_buffer (vm, from[2]);
303 p3 = vlib_get_buffer (vm, from[3]);
305 vlib_prefetch_buffer_header (p2, LOAD);
306 vlib_prefetch_buffer_header (p3, LOAD);
308 CLIB_PREFETCH (p2->data - rewrite_length, 2 * CLIB_CACHE_LINE_BYTES, STORE);
309 CLIB_PREFETCH (p3->data - rewrite_length, 2 * CLIB_CACHE_LINE_BYTES, STORE);
312 /* speculatively enqueue b0 and b1 to the current next frame */
313 to_next[0] = bi0 = from[0];
314 to_next[1] = bi1 = from[1];
320 b0 = vlib_get_buffer (vm, bi0);
321 b1 = vlib_get_buffer (vm, bi1);
323 /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
324 ASSERT (b0->current_data == 0);
325 ASSERT (b1->current_data == 0);
327 ip0 = vlib_buffer_get_current (b0);
328 ip1 = vlib_buffer_get_current (b1);
330 /* Copy the ip header left by the required amount */
331 copy_dst0 = (u64 *)(((u8 *)ip0) - rewrite_length);
332 copy_dst1 = (u64 *)(((u8 *)ip1) - rewrite_length);
333 copy_src0 = (u64 *) ip0;
334 copy_src1 = (u64 *) ip1;
336 copy_dst0 [0] = copy_src0 [0];
337 copy_dst0 [1] = copy_src0 [1];
338 copy_dst0 [2] = copy_src0 [2];
339 copy_dst0 [3] = copy_src0 [3];
340 copy_dst0 [4] = copy_src0 [4];
342 copy_dst1 [0] = copy_src1 [0];
343 copy_dst1 [1] = copy_src1 [1];
344 copy_dst1 [2] = copy_src1 [2];
345 copy_dst1 [3] = copy_src1 [3];
346 copy_dst1 [4] = copy_src1 [4];
348 vlib_buffer_advance (b0, - (word)rewrite_length);
349 vlib_buffer_advance (b1, - (word)rewrite_length);
350 ip0 = vlib_buffer_get_current (b0);
351 ip1 = vlib_buffer_get_current (b1);
353 hbh0 = (ip6_hop_by_hop_header_t *)(ip0 + 1);
354 hbh1 = (ip6_hop_by_hop_header_t *)(ip1 + 1);
355 /* $$$ tune, rewrite_length is a multiple of 8 */
356 clib_memcpy (hbh0, rewrite, rewrite_length);
357 clib_memcpy (hbh1, rewrite, rewrite_length);
358 /* Patch the protocol chain, insert the h-b-h (type 0) header */
359 hbh0->protocol = ip0->protocol;
360 hbh1->protocol = ip1->protocol;
363 new_l0 = clib_net_to_host_u16 (ip0->payload_length) + rewrite_length;
364 new_l1 = clib_net_to_host_u16 (ip1->payload_length) + rewrite_length;
365 ip0->payload_length = clib_host_to_net_u16 (new_l0);
366 ip1->payload_length = clib_host_to_net_u16 (new_l1);
368 /* Populate the (first) h-b-h list elt */
369 next0 = IP6_HBYH_IOAM_INPUT_NEXT_IP6_LOOKUP;
370 next1 = IP6_HBYH_IOAM_INPUT_NEXT_IP6_LOOKUP;
373 /* $$$$$ End of processing 2 x packets $$$$$ */
375 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
377 if (b0->flags & VLIB_BUFFER_IS_TRACED)
379 ip6_add_hop_by_hop_trace_t *t =
380 vlib_add_trace (vm, node, b0, sizeof (*t));
381 t->next_index = next0;
383 if (b1->flags & VLIB_BUFFER_IS_TRACED)
385 ip6_add_hop_by_hop_trace_t *t =
386 vlib_add_trace (vm, node, b1, sizeof (*t));
387 t->next_index = next1;
391 /* verify speculative enqueues, maybe switch current next frame */
392 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
393 to_next, n_left_to_next,
394 bi0, bi1, next0, next1);
396 while (n_left_from > 0 && n_left_to_next > 0)
402 ip6_hop_by_hop_header_t * hbh0;
403 u64 * copy_src0, * copy_dst0;
406 /* speculatively enqueue b0 to the current next frame */
414 b0 = vlib_get_buffer (vm, bi0);
416 ip0 = vlib_buffer_get_current (b0);
418 /* Copy the ip header left by the required amount */
419 copy_dst0 = (u64 *)(((u8 *)ip0) - rewrite_length);
420 copy_src0 = (u64 *) ip0;
422 copy_dst0 [0] = copy_src0 [0];
423 copy_dst0 [1] = copy_src0 [1];
424 copy_dst0 [2] = copy_src0 [2];
425 copy_dst0 [3] = copy_src0 [3];
426 copy_dst0 [4] = copy_src0 [4];
427 vlib_buffer_advance (b0, - (word)rewrite_length);
428 ip0 = vlib_buffer_get_current (b0);
430 hbh0 = (ip6_hop_by_hop_header_t *)(ip0 + 1);
431 /* $$$ tune, rewrite_length is a multiple of 8 */
432 clib_memcpy (hbh0, rewrite, rewrite_length);
433 /* Patch the protocol chain, insert the h-b-h (type 0) header */
434 hbh0->protocol = ip0->protocol;
436 new_l0 = clib_net_to_host_u16 (ip0->payload_length) + rewrite_length;
437 ip0->payload_length = clib_host_to_net_u16 (new_l0);
439 /* Populate the (first) h-b-h list elt */
440 next0 = IP6_HBYH_IOAM_INPUT_NEXT_IP6_LOOKUP;
442 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
443 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
445 ip6_add_hop_by_hop_trace_t *t =
446 vlib_add_trace (vm, node, b0, sizeof (*t));
447 t->next_index = next0;
452 /* verify speculative enqueue, maybe switch current next frame */
453 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
454 to_next, n_left_to_next,
458 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
461 vlib_node_increment_counter (vm, ip6_add_hop_by_hop_node.index,
462 IP6_ADD_HOP_BY_HOP_ERROR_PROCESSED, processed);
463 return frame->n_vectors;
466 VLIB_REGISTER_NODE (ip6_add_hop_by_hop_node) = {
467 .function = ip6_add_hop_by_hop_node_fn,
468 .name = "ip6-add-hop-by-hop",
469 .vector_size = sizeof (u32),
470 .format_trace = format_ip6_add_hop_by_hop_trace,
471 .type = VLIB_NODE_TYPE_INTERNAL,
473 .n_errors = ARRAY_LEN(ip6_add_hop_by_hop_error_strings),
474 .error_strings = ip6_add_hop_by_hop_error_strings,
476 /* See ip/lookup.h */
477 .n_next_nodes = IP6_HBYH_IOAM_INPUT_N_NEXT,
479 #define _(s,n) [IP6_HBYH_IOAM_INPUT_NEXT_##s] = n,
480 foreach_ip6_hbyh_ioam_input_next
485 VLIB_NODE_FUNCTION_MULTIARCH (ip6_add_hop_by_hop_node, ip6_add_hop_by_hop_node_fn)
487 /* The main h-b-h tracer was already invoked, no need to do much here */
490 } ip6_pop_hop_by_hop_trace_t;
492 /* packet trace format function */
493 static u8 * format_ip6_pop_hop_by_hop_trace (u8 * s, va_list * args)
495 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
496 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
497 ip6_pop_hop_by_hop_trace_t * t = va_arg (*args, ip6_pop_hop_by_hop_trace_t *);
499 s = format (s, "IP6_POP_HOP_BY_HOP: next index %d",
505 ip6_hbh_pop_register_option (u8 option,
506 int options(ip6_header_t *ip, ip6_hop_by_hop_option_t *opt))
508 ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
510 ASSERT (option < ARRAY_LEN (hm->pop_options));
512 /* Already registered */
513 if (hm->pop_options[option])
516 hm->pop_options[option] = options;
522 ip6_hbh_pop_unregister_option (u8 option)
524 ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
526 ASSERT (option < ARRAY_LEN (hm->pop_options));
529 if (!hm->pop_options[option])
532 hm->pop_options[option] = NULL;
536 vlib_node_registration_t ip6_pop_hop_by_hop_node;
538 #define foreach_ip6_pop_hop_by_hop_error \
539 _(PROCESSED, "Pkts w/ removed ip6 hop-by-hop options") \
540 _(NO_HOHO, "Pkts w/ no ip6 hop-by-hop options") \
541 _(OPTION_FAILED, "ip6 pop hop-by-hop failed to process")
544 #define _(sym,str) IP6_POP_HOP_BY_HOP_ERROR_##sym,
545 foreach_ip6_pop_hop_by_hop_error
547 IP6_POP_HOP_BY_HOP_N_ERROR,
548 } ip6_pop_hop_by_hop_error_t;
550 static char * ip6_pop_hop_by_hop_error_strings[] = {
551 #define _(sym,string) string,
552 foreach_ip6_pop_hop_by_hop_error
556 static inline void ioam_pop_hop_by_hop_processing (vlib_main_t * vm,
558 ip6_hop_by_hop_header_t *hbh0)
560 ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
561 ip6_hop_by_hop_option_t *opt0, *limit0;
564 if (!hbh0 || !ip0) return;
566 opt0 = (ip6_hop_by_hop_option_t *)(hbh0+1);
567 limit0 = (ip6_hop_by_hop_option_t *)
568 ((u8 *)hbh0 + ((hbh0->length+1)<<3));
570 /* Scan the set of h-b-h options, process ones that we understand */
571 while (opt0 < limit0)
577 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *)opt0) + 1;
582 if (hm->pop_options[type0])
584 if ((*hm->pop_options[type0])(ip0, opt0) < 0)
586 vlib_node_increment_counter (vm, ip6_pop_hop_by_hop_node.index,
587 IP6_POP_HOP_BY_HOP_ERROR_OPTION_FAILED, 1);
591 opt0 = (ip6_hop_by_hop_option_t *) (((u8 *)opt0) + opt0->length + sizeof (ip6_hop_by_hop_option_t));
596 ip6_pop_hop_by_hop_node_fn (vlib_main_t * vm,
597 vlib_node_runtime_t * node,
598 vlib_frame_t * frame)
600 ip6_main_t * im = &ip6_main;
601 ip_lookup_main_t * lm = &im->lookup_main;
602 u32 n_left_from, * from, * to_next;
603 ip_lookup_next_t next_index;
607 from = vlib_frame_vector_args (frame);
608 n_left_from = frame->n_vectors;
609 next_index = node->cached_next_index;
611 while (n_left_from > 0)
615 vlib_get_next_frame (vm, node, next_index,
616 to_next, n_left_to_next);
618 while (n_left_from >= 4 && n_left_to_next >= 2)
621 vlib_buffer_t * b0, * b1;
623 u32 adj_index0, adj_index1;
624 ip6_header_t * ip0, *ip1;
625 ip_adjacency_t * adj0, *adj1;
626 ip6_hop_by_hop_header_t *hbh0, *hbh1;
627 u64 *copy_dst0, *copy_src0, *copy_dst1, *copy_src1;
630 /* Prefetch next iteration. */
632 vlib_buffer_t * p2, * p3;
634 p2 = vlib_get_buffer (vm, from[2]);
635 p3 = vlib_get_buffer (vm, from[3]);
637 vlib_prefetch_buffer_header (p2, LOAD);
638 vlib_prefetch_buffer_header (p3, LOAD);
640 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
641 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
644 /* speculatively enqueue b0 and b1 to the current next frame */
645 to_next[0] = bi0 = from[0];
646 to_next[1] = bi1 = from[1];
652 b0 = vlib_get_buffer (vm, bi0);
653 b1 = vlib_get_buffer (vm, bi1);
655 /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
656 ASSERT (b0->current_data == 0);
657 ASSERT (b1->current_data == 0);
659 ip0 = vlib_buffer_get_current (b0);
660 ip1 = vlib_buffer_get_current (b1);
661 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
662 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
663 adj0 = ip_get_adjacency (lm, adj_index0);
664 adj1 = ip_get_adjacency (lm, adj_index1);
666 next0 = adj0->lookup_next_index;
667 next1 = adj1->lookup_next_index;
669 hbh0 = (ip6_hop_by_hop_header_t *)(ip0+1);
670 hbh1 = (ip6_hop_by_hop_header_t *)(ip1+1);
672 ioam_pop_hop_by_hop_processing(vm, ip0, hbh0);
673 ioam_pop_hop_by_hop_processing(vm, ip1, hbh1);
675 vlib_buffer_advance (b0, (hbh0->length+1)<<3);
676 vlib_buffer_advance (b1, (hbh1->length+1)<<3);
678 new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
679 ((hbh0->length+1)<<3);
680 new_l1 = clib_net_to_host_u16 (ip1->payload_length) -
681 ((hbh1->length+1)<<3);
683 ip0->payload_length = clib_host_to_net_u16 (new_l0);
684 ip1->payload_length = clib_host_to_net_u16 (new_l1);
686 ip0->protocol = hbh0->protocol;
687 ip1->protocol = hbh1->protocol;
689 copy_src0 = (u64 *)ip0;
690 copy_src1 = (u64 *)ip1;
691 copy_dst0 = copy_src0 + (hbh0->length+1);
692 copy_dst0 [4] = copy_src0[4];
693 copy_dst0 [3] = copy_src0[3];
694 copy_dst0 [2] = copy_src0[2];
695 copy_dst0 [1] = copy_src0[1];
696 copy_dst0 [0] = copy_src0[0];
697 copy_dst1 = copy_src1 + (hbh1->length+1);
698 copy_dst1 [4] = copy_src1[4];
699 copy_dst1 [3] = copy_src1[3];
700 copy_dst1 [2] = copy_src1[2];
701 copy_dst1 [1] = copy_src1[1];
702 copy_dst1 [0] = copy_src1[0];
704 /* $$$$$ End of processing 2 x packets $$$$$ */
706 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
708 if (b0->flags & VLIB_BUFFER_IS_TRACED)
710 ip6_pop_hop_by_hop_trace_t *t =
711 vlib_add_trace (vm, node, b0, sizeof (*t));
712 t->next_index = next0;
714 if (b1->flags & VLIB_BUFFER_IS_TRACED)
716 ip6_pop_hop_by_hop_trace_t *t =
717 vlib_add_trace (vm, node, b1, sizeof (*t));
718 t->next_index = next1;
722 /* verify speculative enqueues, maybe switch current next frame */
723 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
724 to_next, n_left_to_next,
725 bi0, bi1, next0, next1);
728 while (n_left_from > 0 && n_left_to_next > 0)
735 ip_adjacency_t * adj0;
736 ip6_hop_by_hop_header_t *hbh0;
737 u64 * copy_dst0, * copy_src0;
740 /* speculatively enqueue b0 to the current next frame */
748 b0 = vlib_get_buffer (vm, bi0);
750 ip0 = vlib_buffer_get_current (b0);
751 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
752 adj0 = ip_get_adjacency (lm, adj_index0);
754 /* Default use the next_index from the adjacency. */
755 next0 = adj0->lookup_next_index;
757 /* Perfectly normal to end up here w/ out h-b-h header */
758 hbh0 = (ip6_hop_by_hop_header_t *)(ip0+1);
760 /* TODO:Temporarily doing it here.. do this validation in end_of_path_cb */
761 ioam_pop_hop_by_hop_processing(vm, ip0, hbh0);
762 /* Pop the trace data */
763 vlib_buffer_advance (b0, (hbh0->length+1)<<3);
764 new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
765 ((hbh0->length+1)<<3);
766 ip0->payload_length = clib_host_to_net_u16 (new_l0);
767 ip0->protocol = hbh0->protocol;
768 copy_src0 = (u64 *)ip0;
769 copy_dst0 = copy_src0 + (hbh0->length+1);
770 copy_dst0 [4] = copy_src0[4];
771 copy_dst0 [3] = copy_src0[3];
772 copy_dst0 [2] = copy_src0[2];
773 copy_dst0 [1] = copy_src0[1];
774 copy_dst0 [0] = copy_src0[0];
777 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
778 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
780 ip6_pop_hop_by_hop_trace_t *t =
781 vlib_add_trace (vm, node, b0, sizeof (*t));
782 t->next_index = next0;
785 /* verify speculative enqueue, maybe switch current next frame */
786 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
787 to_next, n_left_to_next,
791 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
794 vlib_node_increment_counter (vm, ip6_pop_hop_by_hop_node.index,
795 IP6_POP_HOP_BY_HOP_ERROR_PROCESSED, processed);
796 vlib_node_increment_counter (vm, ip6_pop_hop_by_hop_node.index,
797 IP6_POP_HOP_BY_HOP_ERROR_NO_HOHO, no_header);
798 return frame->n_vectors;
801 VLIB_REGISTER_NODE (ip6_pop_hop_by_hop_node) = {
802 .function = ip6_pop_hop_by_hop_node_fn,
803 .name = "ip6-pop-hop-by-hop",
804 .vector_size = sizeof (u32),
805 .format_trace = format_ip6_pop_hop_by_hop_trace,
806 .type = VLIB_NODE_TYPE_INTERNAL,
807 .sibling_of = "ip6-lookup",
808 .n_errors = ARRAY_LEN(ip6_pop_hop_by_hop_error_strings),
809 .error_strings = ip6_pop_hop_by_hop_error_strings,
811 /* See ip/lookup.h */
815 VLIB_NODE_FUNCTION_MULTIARCH (ip6_pop_hop_by_hop_node,
816 ip6_pop_hop_by_hop_node_fn)
818 static clib_error_t *
819 ip6_hop_by_hop_ioam_init (vlib_main_t * vm)
821 ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
824 hm->vnet_main = vnet_get_main();
825 hm->unix_time_0 = (u32) time (0); /* Store starting time */
826 hm->vlib_time_0 = vlib_time_now (vm);
827 hm->ioam_flag = IOAM_HBYH_MOD;
828 hm->trace_tsp = TSP_MICROSECONDS; /* Micro seconds */
829 memset(hm->add_options, 0, sizeof(hm->add_options));
830 memset(hm->pop_options, 0, sizeof(hm->pop_options));
831 memset(hm->options_size, 0, sizeof(hm->options_size));
834 * Register the handlers
835 * XXX: This should be done dynamically based on OAM feature being enabled or not.
837 if (ip6_hbh_register_option(HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST, ip6_hbh_ioam_trace_data_list_handler,
838 ip6_hbh_ioam_trace_data_list_trace_handler) < 0)
839 return (clib_error_create("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST failed"));
844 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_init);
846 int ip6_ioam_set_rewrite (u8 **rwp, u32 trace_type, u32 trace_option_elts,
847 int has_pot_option, int has_ppc_option)
849 ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
852 ip6_hop_by_hop_header_t *hbh;
853 ioam_trace_option_t * trace_option;
855 u8 trace_data_size = 0;
859 if (trace_option_elts == 0 && has_pot_option == 0)
862 /* Work out how much space we need */
863 size = sizeof (ip6_hop_by_hop_header_t);
865 if (trace_option_elts)
867 size += sizeof (ip6_hop_by_hop_option_t);
869 trace_data_size = fetch_trace_data_size(trace_type);
870 if (trace_data_size == 0)
871 return VNET_API_ERROR_INVALID_VALUE;
873 if (trace_option_elts * trace_data_size > 254)
874 return VNET_API_ERROR_INVALID_VALUE;
876 size += trace_option_elts * trace_data_size;
878 if (has_pot_option && hm->add_options[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT] != 0)
880 size += sizeof (ip6_hop_by_hop_option_t);
881 size += hm->options_size[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT];
884 /* Round to a multiple of 8 octets */
885 rnd_size = (size + 7) & ~7;
887 /* allocate it, zero-fill / pad by construction */
888 vec_validate (rewrite, rnd_size-1);
890 hbh = (ip6_hop_by_hop_header_t *) rewrite;
891 /* Length of header in 8 octet units, not incl first 8 octets */
892 hbh->length = (rnd_size>>3) - 1;
893 current = (u8 *)(hbh+1);
895 if (trace_option_elts)
897 trace_option = (ioam_trace_option_t *)current;
898 trace_option->hdr.type = HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST
899 | HBH_OPTION_TYPE_DATA_CHANGE_ENROUTE;
900 trace_option->hdr.length =
901 2 /*ioam_trace_type,data_list_elts_left */ +
902 trace_option_elts * trace_data_size;
903 trace_option->ioam_trace_type = trace_type & TRACE_TYPE_MASK;
904 trace_option->data_list_elts_left = trace_option_elts;
905 current += sizeof (ioam_trace_option_t) +
906 trace_option_elts * trace_data_size;
908 if (has_pot_option && hm->add_options[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT] != 0)
910 if (0 == hm->add_options[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT](current,
911 hm->options_size[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT]))
912 current += sizeof (hm->options_size[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT]);
920 clear_ioam_rewrite_fn(void)
922 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
924 vec_free(hm->rewrite);
929 hm->trace_option_elts = 0;
930 hm->has_pot_option = 0;
931 hm->has_ppc_option = 0;
932 hm->trace_tsp = TSP_MICROSECONDS;
937 clib_error_t * clear_ioam_rewrite_command_fn (vlib_main_t * vm,
938 unformat_input_t * input,
939 vlib_cli_command_t * cmd)
941 return(clear_ioam_rewrite_fn());
944 VLIB_CLI_COMMAND (ip6_clear_ioam_trace_cmd, static) = {
945 .path = "clear ioam rewrite",
946 .short_help = "clear ioam rewrite",
947 .function = clear_ioam_rewrite_command_fn,
951 ip6_ioam_trace_profile_set(u32 trace_option_elts, u32 trace_type, u32 node_id,
952 u32 app_data, int has_pot_option, u32 trace_tsp,
956 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
957 rv = ip6_ioam_set_rewrite (&hm->rewrite, trace_type, trace_option_elts,
958 has_pot_option, has_ppc_option);
963 hm->node_id = node_id;
964 hm->app_data = app_data;
965 hm->trace_type = trace_type;
966 hm->trace_option_elts = trace_option_elts;
967 hm->has_pot_option = has_pot_option;
968 hm->has_ppc_option = has_ppc_option;
969 hm->trace_tsp = trace_tsp;
973 return clib_error_return_code(0, rv, 0, "ip6_ioam_set_rewrite returned %d", rv);
980 static clib_error_t *
981 ip6_set_ioam_rewrite_command_fn (vlib_main_t * vm,
982 unformat_input_t * input,
983 vlib_cli_command_t * cmd)
985 u32 trace_option_elts = 0;
986 u32 trace_type = 0, node_id = 0;
987 u32 app_data = 0, trace_tsp = TSP_MICROSECONDS;
988 int has_pot_option = 0;
989 int has_ppc_option = 0;
990 clib_error_t * rv = 0;
992 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
994 if (unformat (input, "trace-type 0x%x trace-elts %d "
995 "trace-tsp %d node-id 0x%x app-data 0x%x",
996 &trace_type, &trace_option_elts, &trace_tsp,
997 &node_id, &app_data))
999 else if (unformat (input, "pot"))
1001 else if (unformat (input, "ppc encap"))
1002 has_ppc_option = PPC_ENCAP;
1003 else if (unformat (input, "ppc decap"))
1004 has_ppc_option = PPC_DECAP;
1005 else if (unformat (input, "ppc none"))
1006 has_ppc_option = PPC_NONE;
1012 rv = ip6_ioam_trace_profile_set(trace_option_elts, trace_type, node_id,
1013 app_data, has_pot_option, trace_tsp, has_ppc_option);
1019 VLIB_CLI_COMMAND (ip6_set_ioam_rewrite_cmd, static) = {
1020 .path = "set ioam rewrite",
1021 .short_help = "set ioam rewrite trace-type <0x1f|0x3|0x9|0x11|0x19> trace-elts <nn> trace-tsp <0|1|2|3> node-id <node id in hex> app-data <app_data in hex> [pot] [ppc <encap|decap>]",
1022 .function = ip6_set_ioam_rewrite_command_fn,
1025 static clib_error_t *
1026 ip6_show_ioam_summary_cmd_fn (vlib_main_t * vm,
1027 unformat_input_t * input,
1028 vlib_cli_command_t * cmd)
1030 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
1034 if (!is_zero_ip6_address(&hm->adj))
1036 s = format(s, " REWRITE FLOW CONFIGS - \n");
1037 s = format(s, " Destination Address : %U\n",
1038 format_ip6_address, &hm->adj, sizeof(ip6_address_t));
1039 s = format(s, " Flow operation : %d (%s)\n", hm->ioam_flag,
1040 (hm->ioam_flag == IOAM_HBYH_ADD) ? "Add" :
1041 ((hm->ioam_flag == IOAM_HBYH_MOD) ? "Mod" : "Pop"));
1045 s = format(s, " REWRITE FLOW CONFIGS - Not configured\n");
1048 if (hm->trace_option_elts)
1050 s = format(s, " HOP BY HOP OPTIONS - TRACE CONFIG - \n");
1051 s = format(s, " Trace Type : 0x%x (%d)\n",
1052 hm->trace_type, hm->trace_type);
1053 s = format(s, " Trace timestamp precision : %d (%s)\n", hm->trace_tsp,
1054 (hm->trace_tsp == TSP_SECONDS) ? "Seconds" :
1055 ((hm->trace_tsp == TSP_MILLISECONDS) ? "Milliseconds" :
1056 (((hm->trace_tsp == TSP_MICROSECONDS) ? "Microseconds" : "Nanoseconds"))));
1057 s = format(s, " Num of trace nodes : %d\n",
1058 hm->trace_option_elts);
1059 s = format(s, " Node-id : 0x%x (%d)\n",
1060 hm->node_id, hm->node_id);
1061 s = format(s, " App Data : 0x%x (%d)\n",
1062 hm->app_data, hm->app_data);
1066 s = format(s, " HOP BY HOP OPTIONS - TRACE CONFIG - Not configured\n");
1069 s = format(s, " POT OPTION - %d (%s)\n",
1070 hm->has_pot_option, (hm->has_pot_option?"Enabled":"Disabled"));
1071 if (hm->has_pot_option)
1072 s = format(s, "Try 'show ioam pot and show pot profile' for more information\n");
1074 s = format(s, " EDGE TO EDGE - PPC OPTION - %d (%s)\n",
1075 hm->has_ppc_option, ppc_state[hm->has_ppc_option]);
1076 if (hm->has_ppc_option)
1077 s = format(s, "Try 'show ioam ppc' for more information\n");
1079 vlib_cli_output(vm, "%v", s);
1084 VLIB_CLI_COMMAND (ip6_show_ioam_run_cmd, static) = {
1085 .path = "show ioam summary",
1086 .short_help = "Summary of IOAM configuration",
1087 .function = ip6_show_ioam_summary_cmd_fn,
1090 int ip6_ioam_set_destination (ip6_address_t *addr, u32 mask_width, u32 vrf_id,
1091 int is_add, int is_pop, int is_none)
1093 ip6_main_t * im = &ip6_main;
1094 ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
1095 ip_lookup_main_t * lm = &im->lookup_main;
1096 ip_adjacency_t * adj;
1101 BVT(clib_bihash_kv) kv, value;
1103 if ((is_add + is_pop + is_none) != 1)
1104 return VNET_API_ERROR_INVALID_VALUE_2;
1106 /* Go find the adjacency we're supposed to tickle */
1107 p = hash_get (im->fib_index_by_table_id, vrf_id);
1110 return VNET_API_ERROR_NO_SUCH_FIB;
1114 len = vec_len (im->prefix_lengths_in_search_order);
1116 for (i = 0; i < len; i++)
1118 int dst_address_length = im->prefix_lengths_in_search_order[i];
1119 ip6_address_t * mask = &im->fib_masks[dst_address_length];
1121 if (dst_address_length != mask_width)
1124 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
1125 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
1126 kv.key[2] = ((u64)((fib_index))<<32) | dst_address_length;
1128 rv = BV(clib_bihash_search_inline_2)(&im->ip6_lookup_table, &kv, &value);
1133 return VNET_API_ERROR_NO_SUCH_ENTRY;
1137 /* Got it, modify as directed... */
1138 adj_index = value.value;
1139 adj = ip_get_adjacency (lm, adj_index);
1141 /* Restore original lookup-next action */
1142 if (adj->saved_lookup_next_index)
1144 adj->lookup_next_index = adj->saved_lookup_next_index;
1145 adj->saved_lookup_next_index = 0;
1148 /* Save current action */
1149 if (is_add || is_pop)
1150 adj->saved_lookup_next_index = adj->lookup_next_index;
1153 adj->lookup_next_index = (ip_lookup_next_t) IP6_LOOKUP_NEXT_ADD_HOP_BY_HOP;
1156 adj->lookup_next_index = (ip_lookup_next_t) IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
1159 hm->ioam_flag = (is_add ? IOAM_HBYH_ADD :
1160 (is_pop ? IOAM_HBYH_POP : IOAM_HBYH_MOD));
1164 static clib_error_t *
1165 ip6_set_ioam_destination_command_fn (vlib_main_t * vm,
1166 unformat_input_t * input,
1167 vlib_cli_command_t * cmd)
1170 u32 mask_width = ~0;
1177 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1179 if (unformat (input, "%U/%d",
1180 unformat_ip6_address, &addr, &mask_width))
1182 else if (unformat (input, "vrf-id %d", &vrf_id))
1184 else if (unformat (input, "add"))
1186 else if (unformat (input, "pop"))
1188 else if (unformat (input, "none"))
1194 if ((is_add + is_pop + is_none) != 1)
1195 return clib_error_return (0, "One of (add, pop, none) required");
1196 if (mask_width == ~0)
1197 return clib_error_return (0, "<address>/<mask-width> required");
1199 rv = ip6_ioam_set_destination (&addr, mask_width, vrf_id,
1200 is_add, is_pop, is_none);
1207 return clib_error_return (0, "ip6_ioam_set_destination returned %d", rv);
1213 VLIB_CLI_COMMAND (ip6_set_ioam_destination_cmd, static) = {
1214 .path = "set ioam destination",
1215 .short_help = "set ioam destination <ip6-address>/<width> add | pop | none",
1216 .function = ip6_set_ioam_destination_command_fn,
1220 void vnet_register_ioam_end_of_path_callback (void *cb)
1222 ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
1224 hm->ioam_end_of_path_cb = cb;