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 char *ppc_state[] = { "None", "Encap", "Decap" };
30 ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main;
32 #define foreach_ip6_hbyh_ioam_input_next \
33 _(IP6_REWRITE, "ip6-rewrite") \
34 _(IP6_LOOKUP, "ip6-lookup") \
39 #define _(s,n) IP6_HBYH_IOAM_INPUT_NEXT_##s,
40 foreach_ip6_hbyh_ioam_input_next
42 IP6_HBYH_IOAM_INPUT_N_NEXT,
43 } ip6_hbyh_ioam_input_next_t;
47 /* The main h-b-h tracer will be invoked, no need to do much here */
49 ip6_hbh_add_register_option (u8 option,
51 int rewrite_options (u8 * rewrite_string,
54 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
56 ASSERT (option < ARRAY_LEN (hm->add_options));
58 /* Already registered */
59 if (hm->add_options[option])
62 hm->add_options[option] = rewrite_options;
63 hm->options_size[option] = size;
69 ip6_hbh_add_unregister_option (u8 option)
71 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
73 ASSERT (option < ARRAY_LEN (hm->add_options));
76 if (!hm->add_options[option])
79 hm->add_options[option] = NULL;
80 hm->options_size[option] = 0;
87 } ip6_add_hop_by_hop_trace_t;
89 /* packet trace format function */
91 format_ip6_add_hop_by_hop_trace (u8 * s, va_list * args)
93 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
94 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
95 ip6_add_hop_by_hop_trace_t *t = va_arg (*args,
96 ip6_add_hop_by_hop_trace_t *);
98 s = format (s, "IP6_ADD_HOP_BY_HOP: next index %d", t->next_index);
102 vlib_node_registration_t ip6_add_hop_by_hop_node;
104 #define foreach_ip6_add_hop_by_hop_error \
105 _(PROCESSED, "Pkts w/ added ip6 hop-by-hop options")
109 #define _(sym,str) IP6_ADD_HOP_BY_HOP_ERROR_##sym,
110 foreach_ip6_add_hop_by_hop_error
112 IP6_ADD_HOP_BY_HOP_N_ERROR,
113 } ip6_add_hop_by_hop_error_t;
115 static char *ip6_add_hop_by_hop_error_strings[] = {
116 #define _(sym,string) string,
117 foreach_ip6_add_hop_by_hop_error
122 ip6_add_hop_by_hop_node_fn (vlib_main_t * vm,
123 vlib_node_runtime_t * node, vlib_frame_t * frame)
125 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
126 u32 n_left_from, *from, *to_next;
127 ip_lookup_next_t next_index;
129 u8 *rewrite = hm->rewrite;
130 u32 rewrite_length = vec_len (rewrite);
132 from = vlib_frame_vector_args (frame);
133 n_left_from = frame->n_vectors;
134 next_index = node->cached_next_index;
136 while (n_left_from > 0)
140 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
141 while (n_left_from >= 4 && n_left_to_next >= 2)
144 vlib_buffer_t *b0, *b1;
146 ip6_header_t *ip0, *ip1;
147 ip6_hop_by_hop_header_t *hbh0, *hbh1;
148 u64 *copy_src0, *copy_dst0, *copy_src1, *copy_dst1;
151 /* Prefetch next iteration. */
153 vlib_buffer_t *p2, *p3;
155 p2 = vlib_get_buffer (vm, from[2]);
156 p3 = vlib_get_buffer (vm, from[3]);
158 vlib_prefetch_buffer_header (p2, LOAD);
159 vlib_prefetch_buffer_header (p3, LOAD);
161 CLIB_PREFETCH (p2->data - rewrite_length,
162 2 * CLIB_CACHE_LINE_BYTES, STORE);
163 CLIB_PREFETCH (p3->data - rewrite_length,
164 2 * CLIB_CACHE_LINE_BYTES, STORE);
167 /* speculatively enqueue b0 and b1 to the current next frame */
168 to_next[0] = bi0 = from[0];
169 to_next[1] = bi1 = from[1];
175 b0 = vlib_get_buffer (vm, bi0);
176 b1 = vlib_get_buffer (vm, bi1);
178 /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
179 ASSERT (b0->current_data == 0);
180 ASSERT (b1->current_data == 0);
182 ip0 = vlib_buffer_get_current (b0);
183 ip1 = vlib_buffer_get_current (b1);
185 /* Copy the ip header left by the required amount */
186 copy_dst0 = (u64 *) (((u8 *) ip0) - rewrite_length);
187 copy_dst1 = (u64 *) (((u8 *) ip1) - rewrite_length);
188 copy_src0 = (u64 *) ip0;
189 copy_src1 = (u64 *) ip1;
191 copy_dst0[0] = copy_src0[0];
192 copy_dst0[1] = copy_src0[1];
193 copy_dst0[2] = copy_src0[2];
194 copy_dst0[3] = copy_src0[3];
195 copy_dst0[4] = copy_src0[4];
197 copy_dst1[0] = copy_src1[0];
198 copy_dst1[1] = copy_src1[1];
199 copy_dst1[2] = copy_src1[2];
200 copy_dst1[3] = copy_src1[3];
201 copy_dst1[4] = copy_src1[4];
203 vlib_buffer_advance (b0, -(word) rewrite_length);
204 vlib_buffer_advance (b1, -(word) rewrite_length);
205 ip0 = vlib_buffer_get_current (b0);
206 ip1 = vlib_buffer_get_current (b1);
208 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
209 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
210 /* $$$ tune, rewrite_length is a multiple of 8 */
211 clib_memcpy (hbh0, rewrite, rewrite_length);
212 clib_memcpy (hbh1, rewrite, rewrite_length);
213 /* Patch the protocol chain, insert the h-b-h (type 0) header */
214 hbh0->protocol = ip0->protocol;
215 hbh1->protocol = ip1->protocol;
219 clib_net_to_host_u16 (ip0->payload_length) + rewrite_length;
221 clib_net_to_host_u16 (ip1->payload_length) + rewrite_length;
222 ip0->payload_length = clib_host_to_net_u16 (new_l0);
223 ip1->payload_length = clib_host_to_net_u16 (new_l1);
225 /* Populate the (first) h-b-h list elt */
226 next0 = IP6_HBYH_IOAM_INPUT_NEXT_IP6_LOOKUP;
227 next1 = IP6_HBYH_IOAM_INPUT_NEXT_IP6_LOOKUP;
230 /* $$$$$ End of processing 2 x packets $$$$$ */
232 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
234 if (b0->flags & VLIB_BUFFER_IS_TRACED)
236 ip6_add_hop_by_hop_trace_t *t =
237 vlib_add_trace (vm, node, b0, sizeof (*t));
238 t->next_index = next0;
240 if (b1->flags & VLIB_BUFFER_IS_TRACED)
242 ip6_add_hop_by_hop_trace_t *t =
243 vlib_add_trace (vm, node, b1, sizeof (*t));
244 t->next_index = next1;
248 /* verify speculative enqueues, maybe switch current next frame */
249 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
250 to_next, n_left_to_next,
251 bi0, bi1, next0, next1);
253 while (n_left_from > 0 && n_left_to_next > 0)
259 ip6_hop_by_hop_header_t *hbh0;
260 u64 *copy_src0, *copy_dst0;
263 /* speculatively enqueue b0 to the current next frame */
271 b0 = vlib_get_buffer (vm, bi0);
273 ip0 = vlib_buffer_get_current (b0);
275 /* Copy the ip header left by the required amount */
276 copy_dst0 = (u64 *) (((u8 *) ip0) - rewrite_length);
277 copy_src0 = (u64 *) ip0;
279 copy_dst0[0] = copy_src0[0];
280 copy_dst0[1] = copy_src0[1];
281 copy_dst0[2] = copy_src0[2];
282 copy_dst0[3] = copy_src0[3];
283 copy_dst0[4] = copy_src0[4];
284 vlib_buffer_advance (b0, -(word) rewrite_length);
285 ip0 = vlib_buffer_get_current (b0);
287 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
288 /* $$$ tune, rewrite_length is a multiple of 8 */
289 clib_memcpy (hbh0, rewrite, rewrite_length);
290 /* Patch the protocol chain, insert the h-b-h (type 0) header */
291 hbh0->protocol = ip0->protocol;
294 clib_net_to_host_u16 (ip0->payload_length) + rewrite_length;
295 ip0->payload_length = clib_host_to_net_u16 (new_l0);
297 /* Populate the (first) h-b-h list elt */
298 next0 = IP6_HBYH_IOAM_INPUT_NEXT_IP6_LOOKUP;
300 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
301 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
303 ip6_add_hop_by_hop_trace_t *t =
304 vlib_add_trace (vm, node, b0, sizeof (*t));
305 t->next_index = next0;
310 /* verify speculative enqueue, maybe switch current next frame */
311 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
312 to_next, n_left_to_next,
316 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
319 vlib_node_increment_counter (vm, ip6_add_hop_by_hop_node.index,
320 IP6_ADD_HOP_BY_HOP_ERROR_PROCESSED, processed);
321 return frame->n_vectors;
324 VLIB_REGISTER_NODE (ip6_add_hop_by_hop_node) = /* *INDENT-OFF* */
326 .function = ip6_add_hop_by_hop_node_fn,.name =
327 "ip6-add-hop-by-hop",.vector_size = sizeof (u32),.format_trace =
328 format_ip6_add_hop_by_hop_trace,.type =
329 VLIB_NODE_TYPE_INTERNAL,.n_errors =
330 ARRAY_LEN (ip6_add_hop_by_hop_error_strings),.error_strings =
331 ip6_add_hop_by_hop_error_strings,
332 /* See ip/lookup.h */
333 .n_next_nodes = IP6_HBYH_IOAM_INPUT_N_NEXT,.next_nodes =
335 #define _(s,n) [IP6_HBYH_IOAM_INPUT_NEXT_##s] = n,
336 foreach_ip6_hbyh_ioam_input_next
343 VLIB_NODE_FUNCTION_MULTIARCH (ip6_add_hop_by_hop_node,
344 ip6_add_hop_by_hop_node_fn)
345 /* The main h-b-h tracer was already invoked, no need to do much here */
349 } ip6_pop_hop_by_hop_trace_t;
351 /* packet trace format function */
352 static u8 *format_ip6_pop_hop_by_hop_trace (u8 * s, va_list * args)
354 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
355 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
356 ip6_pop_hop_by_hop_trace_t *t =
357 va_arg (*args, ip6_pop_hop_by_hop_trace_t *);
359 s = format (s, "IP6_POP_HOP_BY_HOP: next index %d", t->next_index);
364 ip6_hbh_pop_register_option (u8 option,
365 int options (ip6_header_t * ip,
366 ip6_hop_by_hop_option_t * opt))
368 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
370 ASSERT (option < ARRAY_LEN (hm->pop_options));
372 /* Already registered */
373 if (hm->pop_options[option])
376 hm->pop_options[option] = options;
382 ip6_hbh_pop_unregister_option (u8 option)
384 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
386 ASSERT (option < ARRAY_LEN (hm->pop_options));
389 if (!hm->pop_options[option])
392 hm->pop_options[option] = NULL;
396 vlib_node_registration_t ip6_pop_hop_by_hop_node;
398 #define foreach_ip6_pop_hop_by_hop_error \
399 _(PROCESSED, "Pkts w/ removed ip6 hop-by-hop options") \
400 _(NO_HOHO, "Pkts w/ no ip6 hop-by-hop options") \
401 _(OPTION_FAILED, "ip6 pop hop-by-hop failed to process")
405 #define _(sym,str) IP6_POP_HOP_BY_HOP_ERROR_##sym,
406 foreach_ip6_pop_hop_by_hop_error
408 IP6_POP_HOP_BY_HOP_N_ERROR,
409 } ip6_pop_hop_by_hop_error_t;
411 static char *ip6_pop_hop_by_hop_error_strings[] = {
412 #define _(sym,string) string,
413 foreach_ip6_pop_hop_by_hop_error
418 ioam_pop_hop_by_hop_processing (vlib_main_t * vm,
420 ip6_hop_by_hop_header_t * hbh0)
422 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
423 ip6_hop_by_hop_option_t *opt0, *limit0;
429 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
430 limit0 = (ip6_hop_by_hop_option_t *)
431 ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
433 /* Scan the set of h-b-h options, process ones that we understand */
434 while (opt0 < limit0)
440 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
445 if (hm->pop_options[type0])
447 if ((*hm->pop_options[type0]) (ip0, opt0) < 0)
449 vlib_node_increment_counter (vm,
450 ip6_pop_hop_by_hop_node.index,
451 IP6_POP_HOP_BY_HOP_ERROR_OPTION_FAILED,
457 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
458 sizeof (ip6_hop_by_hop_option_t));
463 ip6_pop_hop_by_hop_node_fn (vlib_main_t * vm,
464 vlib_node_runtime_t * node, vlib_frame_t * frame)
466 ip6_main_t *im = &ip6_main;
467 ip_lookup_main_t *lm = &im->lookup_main;
468 u32 n_left_from, *from, *to_next;
469 ip_lookup_next_t next_index;
473 from = vlib_frame_vector_args (frame);
474 n_left_from = frame->n_vectors;
475 next_index = node->cached_next_index;
477 while (n_left_from > 0)
481 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
483 while (n_left_from >= 4 && n_left_to_next >= 2)
486 vlib_buffer_t *b0, *b1;
488 u32 adj_index0, adj_index1;
489 ip6_header_t *ip0, *ip1;
490 ip_adjacency_t *adj0, *adj1;
491 ip6_hop_by_hop_header_t *hbh0, *hbh1;
492 u64 *copy_dst0, *copy_src0, *copy_dst1, *copy_src1;
495 /* Prefetch next iteration. */
497 vlib_buffer_t *p2, *p3;
499 p2 = vlib_get_buffer (vm, from[2]);
500 p3 = vlib_get_buffer (vm, from[3]);
502 vlib_prefetch_buffer_header (p2, LOAD);
503 vlib_prefetch_buffer_header (p3, LOAD);
505 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
506 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
509 /* speculatively enqueue b0 and b1 to the current next frame */
510 to_next[0] = bi0 = from[0];
511 to_next[1] = bi1 = from[1];
517 b0 = vlib_get_buffer (vm, bi0);
518 b1 = vlib_get_buffer (vm, bi1);
520 /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
521 ASSERT (b0->current_data == 0);
522 ASSERT (b1->current_data == 0);
524 ip0 = vlib_buffer_get_current (b0);
525 ip1 = vlib_buffer_get_current (b1);
526 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
527 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
528 adj0 = ip_get_adjacency (lm, adj_index0);
529 adj1 = ip_get_adjacency (lm, adj_index1);
531 next0 = adj0->lookup_next_index;
532 next1 = adj1->lookup_next_index;
534 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
535 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
537 ioam_pop_hop_by_hop_processing (vm, ip0, hbh0);
538 ioam_pop_hop_by_hop_processing (vm, ip1, hbh1);
540 vlib_buffer_advance (b0, (hbh0->length + 1) << 3);
541 vlib_buffer_advance (b1, (hbh1->length + 1) << 3);
543 new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
544 ((hbh0->length + 1) << 3);
545 new_l1 = clib_net_to_host_u16 (ip1->payload_length) -
546 ((hbh1->length + 1) << 3);
548 ip0->payload_length = clib_host_to_net_u16 (new_l0);
549 ip1->payload_length = clib_host_to_net_u16 (new_l1);
551 ip0->protocol = hbh0->protocol;
552 ip1->protocol = hbh1->protocol;
554 copy_src0 = (u64 *) ip0;
555 copy_src1 = (u64 *) ip1;
556 copy_dst0 = copy_src0 + (hbh0->length + 1);
557 copy_dst0[4] = copy_src0[4];
558 copy_dst0[3] = copy_src0[3];
559 copy_dst0[2] = copy_src0[2];
560 copy_dst0[1] = copy_src0[1];
561 copy_dst0[0] = copy_src0[0];
562 copy_dst1 = copy_src1 + (hbh1->length + 1);
563 copy_dst1[4] = copy_src1[4];
564 copy_dst1[3] = copy_src1[3];
565 copy_dst1[2] = copy_src1[2];
566 copy_dst1[1] = copy_src1[1];
567 copy_dst1[0] = copy_src1[0];
569 /* $$$$$ End of processing 2 x packets $$$$$ */
571 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
573 if (b0->flags & VLIB_BUFFER_IS_TRACED)
575 ip6_pop_hop_by_hop_trace_t *t =
576 vlib_add_trace (vm, node, b0, sizeof (*t));
577 t->next_index = next0;
579 if (b1->flags & VLIB_BUFFER_IS_TRACED)
581 ip6_pop_hop_by_hop_trace_t *t =
582 vlib_add_trace (vm, node, b1, sizeof (*t));
583 t->next_index = next1;
587 /* verify speculative enqueues, maybe switch current next frame */
588 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
589 to_next, n_left_to_next,
590 bi0, bi1, next0, next1);
593 while (n_left_from > 0 && n_left_to_next > 0)
600 ip_adjacency_t *adj0;
601 ip6_hop_by_hop_header_t *hbh0;
602 u64 *copy_dst0, *copy_src0;
605 /* speculatively enqueue b0 to the current next frame */
613 b0 = vlib_get_buffer (vm, bi0);
615 ip0 = vlib_buffer_get_current (b0);
616 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
617 adj0 = ip_get_adjacency (lm, adj_index0);
619 /* Default use the next_index from the adjacency. */
620 next0 = adj0->lookup_next_index;
622 /* Perfectly normal to end up here w/ out h-b-h header */
623 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
625 /* TODO:Temporarily doing it here.. do this validation in end_of_path_cb */
626 ioam_pop_hop_by_hop_processing (vm, ip0, hbh0);
627 /* Pop the trace data */
628 vlib_buffer_advance (b0, (hbh0->length + 1) << 3);
629 new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
630 ((hbh0->length + 1) << 3);
631 ip0->payload_length = clib_host_to_net_u16 (new_l0);
632 ip0->protocol = hbh0->protocol;
633 copy_src0 = (u64 *) ip0;
634 copy_dst0 = copy_src0 + (hbh0->length + 1);
635 copy_dst0[4] = copy_src0[4];
636 copy_dst0[3] = copy_src0[3];
637 copy_dst0[2] = copy_src0[2];
638 copy_dst0[1] = copy_src0[1];
639 copy_dst0[0] = copy_src0[0];
642 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
643 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
645 ip6_pop_hop_by_hop_trace_t *t =
646 vlib_add_trace (vm, node, b0, sizeof (*t));
647 t->next_index = next0;
650 /* verify speculative enqueue, maybe switch current next frame */
651 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
652 to_next, n_left_to_next,
656 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
659 vlib_node_increment_counter (vm, ip6_pop_hop_by_hop_node.index,
660 IP6_POP_HOP_BY_HOP_ERROR_PROCESSED, processed);
661 vlib_node_increment_counter (vm, ip6_pop_hop_by_hop_node.index,
662 IP6_POP_HOP_BY_HOP_ERROR_NO_HOHO, no_header);
663 return frame->n_vectors;
667 VLIB_REGISTER_NODE (ip6_pop_hop_by_hop_node) =
669 .function = ip6_pop_hop_by_hop_node_fn,.name =
670 "ip6-pop-hop-by-hop",.vector_size = sizeof (u32),.format_trace =
671 format_ip6_pop_hop_by_hop_trace,.type =
672 VLIB_NODE_TYPE_INTERNAL,.sibling_of = "ip6-lookup",.n_errors =
673 ARRAY_LEN (ip6_pop_hop_by_hop_error_strings),.error_strings =
674 ip6_pop_hop_by_hop_error_strings,
675 /* See ip/lookup.h */
680 VLIB_NODE_FUNCTION_MULTIARCH (ip6_pop_hop_by_hop_node,
681 ip6_pop_hop_by_hop_node_fn)
682 static clib_error_t *ip6_hop_by_hop_ioam_init (vlib_main_t * vm)
684 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
687 hm->vnet_main = vnet_get_main ();
688 hm->unix_time_0 = (u32) time (0); /* Store starting time */
689 hm->vlib_time_0 = vlib_time_now (vm);
690 hm->ioam_flag = IOAM_HBYH_MOD;
691 memset (hm->add_options, 0, sizeof (hm->add_options));
692 memset (hm->pop_options, 0, sizeof (hm->pop_options));
693 memset (hm->options_size, 0, sizeof (hm->options_size));
698 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_init);
701 ip6_ioam_set_rewrite (u8 ** rwp, int has_trace_option,
702 int has_pot_option, int has_ppc_option)
704 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
707 ip6_hop_by_hop_header_t *hbh;
709 u8 trace_data_size = 0;
710 u8 pot_data_size = 0;
714 if (has_trace_option == 0 && has_pot_option == 0)
717 /* Work out how much space we need */
718 size = sizeof (ip6_hop_by_hop_header_t);
720 //if (has_trace_option && hm->get_sizeof_options[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] != 0)
722 && hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] != 0)
724 size += sizeof (ip6_hop_by_hop_option_t);
725 size += hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST];
728 && hm->add_options[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT] != 0)
730 size += sizeof (ip6_hop_by_hop_option_t);
731 size += hm->options_size[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT];
734 /* Round to a multiple of 8 octets */
735 rnd_size = (size + 7) & ~7;
737 /* allocate it, zero-fill / pad by construction */
738 vec_validate (rewrite, rnd_size - 1);
740 hbh = (ip6_hop_by_hop_header_t *) rewrite;
741 /* Length of header in 8 octet units, not incl first 8 octets */
742 hbh->length = (rnd_size >> 3) - 1;
743 current = (u8 *) (hbh + 1);
746 && hm->add_options[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] != 0)
748 if (0 != (hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST]))
751 hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST];
753 hm->add_options[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] (current,
755 current += hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST];
759 && hm->add_options[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT] != 0)
761 pot_data_size = hm->options_size[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT];
763 hm->add_options[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT] (current,
766 sizeof (hm->options_size[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT]);
774 clear_ioam_rewrite_fn (void)
776 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
778 vec_free (hm->rewrite);
780 hm->has_trace_option = 0;
781 hm->has_pot_option = 0;
782 hm->has_ppc_option = 0;
788 clear_ioam_rewrite_command_fn (vlib_main_t * vm,
789 unformat_input_t * input,
790 vlib_cli_command_t * cmd)
792 return (clear_ioam_rewrite_fn ());
796 VLIB_CLI_COMMAND (ip6_clear_ioam_rewrite_cmd, static) =
798 .path = "clear ioam rewrite",.short_help = "clear ioam rewrite",.function =
799 clear_ioam_rewrite_command_fn,};
803 ip6_ioam_enable (int has_trace_option, int has_pot_option, int has_ppc_option)
806 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
807 rv = ip6_ioam_set_rewrite (&hm->rewrite, has_trace_option,
808 has_pot_option, has_ppc_option);
813 hm->has_trace_option = has_trace_option;
814 hm->has_pot_option = has_pot_option;
815 hm->has_ppc_option = has_ppc_option;
819 return clib_error_return_code (0, rv, 0,
820 "ip6_ioam_set_rewrite returned %d", rv);
827 static clib_error_t *
828 ip6_set_ioam_rewrite_command_fn (vlib_main_t * vm,
829 unformat_input_t * input,
830 vlib_cli_command_t * cmd)
832 int has_trace_option = 0;
833 int has_pot_option = 0;
834 int has_ppc_option = 0;
835 clib_error_t *rv = 0;
837 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
839 if (unformat (input, "trace"))
840 has_trace_option = 1;
841 else if (unformat (input, "pot"))
843 else if (unformat (input, "ppc encap"))
844 has_ppc_option = PPC_ENCAP;
845 else if (unformat (input, "ppc decap"))
846 has_ppc_option = PPC_DECAP;
847 else if (unformat (input, "ppc none"))
848 has_ppc_option = PPC_NONE;
854 rv = ip6_ioam_enable (has_trace_option, has_pot_option, has_ppc_option);
860 VLIB_CLI_COMMAND (ip6_set_ioam_rewrite_cmd, static) =
862 .path = "set ioam rewrite",.short_help =
863 "set ioam [trace] [pot] [ppc <encap|decap>]",.function =
864 ip6_set_ioam_rewrite_command_fn,};
867 static clib_error_t *
868 ip6_show_ioam_summary_cmd_fn (vlib_main_t * vm,
869 unformat_input_t * input,
870 vlib_cli_command_t * cmd)
872 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
876 if (!is_zero_ip6_address (&hm->adj))
878 s = format (s, " REWRITE FLOW CONFIGS - \n");
879 s = format (s, " Destination Address : %U\n",
880 format_ip6_address, &hm->adj, sizeof (ip6_address_t));
882 format (s, " Flow operation : %d (%s)\n",
885 IOAM_HBYH_ADD) ? "Add" : ((hm->ioam_flag ==
886 IOAM_HBYH_MOD) ? "Mod" : "Pop"));
890 s = format (s, " REWRITE FLOW CONFIGS - Not configured\n");
894 s = format (s, " TRACE OPTION - %d (%s)\n",
895 hm->has_trace_option,
896 (hm->has_trace_option ? "Enabled" : "Disabled"));
897 if (hm->has_trace_option)
900 "Try 'show ioam trace and show ioam-trace profile' for more information\n");
903 s = format (s, " POT OPTION - %d (%s)\n",
905 (hm->has_pot_option ? "Enabled" : "Disabled"));
906 if (hm->has_pot_option)
909 "Try 'show ioam pot and show pot profile' for more information\n");
911 s = format (s, " EDGE TO EDGE - PPC OPTION - %d (%s)\n",
912 hm->has_ppc_option, ppc_state[hm->has_ppc_option]);
913 if (hm->has_ppc_option)
914 s = format (s, "Try 'show ioam ppc' for more information\n");
916 vlib_cli_output (vm, "%v", s);
922 VLIB_CLI_COMMAND (ip6_show_ioam_run_cmd, static) =
924 .path = "show ioam summary",.short_help =
925 "Summary of IOAM configuration",.function =
926 ip6_show_ioam_summary_cmd_fn,};
930 ip6_ioam_set_destination (ip6_address_t * addr, u32 mask_width, u32 vrf_id,
931 int is_add, int is_pop, int is_none)
933 ip6_main_t *im = &ip6_main;
934 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
935 ip_lookup_main_t *lm = &im->lookup_main;
941 BVT (clib_bihash_kv) kv, value;
943 if ((is_add + is_pop + is_none) != 1)
944 return VNET_API_ERROR_INVALID_VALUE_2;
946 /* Go find the adjacency we're supposed to tickle */
947 p = hash_get (im->fib_index_by_table_id, vrf_id);
950 return VNET_API_ERROR_NO_SUCH_FIB;
954 len = vec_len (im->prefix_lengths_in_search_order);
956 for (i = 0; i < len; i++)
958 int dst_address_length = im->prefix_lengths_in_search_order[i];
959 ip6_address_t *mask = &im->fib_masks[dst_address_length];
961 if (dst_address_length != mask_width)
964 kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
965 kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
966 kv.key[2] = ((u64) ((fib_index)) << 32) | dst_address_length;
969 BV (clib_bihash_search_inline_2) (&im->ip6_lookup_table, &kv, &value);
974 return VNET_API_ERROR_NO_SUCH_ENTRY;
978 /* Got it, modify as directed... */
979 adj_index = value.value;
980 adj = ip_get_adjacency (lm, adj_index);
982 /* Restore original lookup-next action */
983 if (adj->saved_lookup_next_index)
985 adj->lookup_next_index = adj->saved_lookup_next_index;
986 adj->saved_lookup_next_index = 0;
989 /* Save current action */
990 if (is_add || is_pop)
991 adj->saved_lookup_next_index = adj->lookup_next_index;
994 adj->lookup_next_index =
995 (ip_lookup_next_t) IP6_LOOKUP_NEXT_ADD_HOP_BY_HOP;
998 adj->lookup_next_index =
999 (ip_lookup_next_t) IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
1002 hm->ioam_flag = (is_add ? IOAM_HBYH_ADD :
1003 (is_pop ? IOAM_HBYH_POP : IOAM_HBYH_MOD));
1007 static clib_error_t *
1008 ip6_set_ioam_destination_command_fn (vlib_main_t * vm,
1009 unformat_input_t * input,
1010 vlib_cli_command_t * cmd)
1013 u32 mask_width = ~0;
1020 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1022 if (unformat (input, "%U/%d", unformat_ip6_address, &addr, &mask_width))
1024 else if (unformat (input, "vrf-id %d", &vrf_id))
1026 else if (unformat (input, "add"))
1028 else if (unformat (input, "pop"))
1030 else if (unformat (input, "none"))
1036 if ((is_add + is_pop + is_none) != 1)
1037 return clib_error_return (0, "One of (add, pop, none) required");
1038 if (mask_width == ~0)
1039 return clib_error_return (0, "<address>/<mask-width> required");
1041 rv = ip6_ioam_set_destination (&addr, mask_width, vrf_id,
1042 is_add, is_pop, is_none);
1049 return clib_error_return (0, "ip6_ioam_set_destination returned %d",
1057 VLIB_CLI_COMMAND (ip6_set_ioam_destination_cmd, static) =
1059 .path = "set ioam destination",.short_help =
1060 "set ioam destination <ip6-address>/<width> add | pop | none",.function
1061 = ip6_set_ioam_destination_command_fn,};
1066 vnet_register_ioam_end_of_path_callback (void *cb)
1068 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
1070 hm->ioam_end_of_path_cb = cb;
1074 * fd.io coding-style-patch-verification: ON
1077 * eval: (c-set-style "gnu")