VPP-271: Coding style cleanup vnet/vnet/snap
[vpp.git] / vnet / vnet / ip / ip6_hop_by_hop.c
1 /*
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:
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 #include <vlib/vlib.h>
16 #include <vnet/vnet.h>
17 #include <vnet/pg/pg.h>
18 #include <vppinfra/error.h>
19
20 #include <vnet/ip/ip.h>
21
22 #include <vppinfra/hash.h>
23 #include <vppinfra/error.h>
24 #include <vppinfra/elog.h>
25
26 #include <vnet/ip/ip6_hop_by_hop.h>
27
28 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
29  * and nanoseconds respectively.
30  */
31 static f64 trace_tsp_mul[4] = {1, 1e3, 1e6, 1e9};
32
33 char *ppc_state[] = {"None", "Encap", "Decap"};
34
35 ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main;
36
37 #define foreach_ip6_hbyh_ioam_input_next        \
38   _(IP6_REWRITE, "ip6-rewrite")                 \
39   _(IP6_LOOKUP, "ip6-lookup")                   \
40   _(DROP, "error-drop")                        
41
42 typedef enum {
43 #define _(s,n) IP6_HBYH_IOAM_INPUT_NEXT_##s,
44   foreach_ip6_hbyh_ioam_input_next
45 #undef _
46   IP6_HBYH_IOAM_INPUT_N_NEXT,
47 } ip6_hbyh_ioam_input_next_t;
48
49 typedef union {
50     u64 as_u64;
51     u32 as_u32[2];
52 } time_u64_t;
53
54 static inline u8
55 fetch_trace_data_size(u8 trace_type)
56 {
57   u8 trace_data_size = 0;
58
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);
69
70   return trace_data_size;
71 }
72
73 static u8 * format_ioam_data_list_element (u8 * s, va_list * args)
74
75   u32 *elt = va_arg (*args, u32 *);
76   u8  *trace_type_p = va_arg (*args, u8 *);
77   u8  trace_type = *trace_type_p;
78
79
80   if (trace_type & BIT_TTL_NODEID)
81     {
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);
86
87       elt++;
88     }
89  
90   if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
91     {
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);
96         elt++;
97     }
98  
99   if (trace_type & BIT_TIMESTAMP)
100     {
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);
103         elt++;
104     }
105  
106   if (trace_type & BIT_APPDATA)
107     {
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);
110         elt++;
111     }
112  
113   return s;
114 }
115
116 u8 *
117 ip6_hbh_ioam_trace_data_list_trace_handler (u8 *s, ip6_hop_by_hop_option_t *opt)
118 {
119   ioam_trace_option_t *trace;
120   u8 trace_data_size_in_words = 0;
121   u32 *elt;
122   int elt_index = 0;
123
124   trace = (ioam_trace_option_t *)opt;
125 #if 0
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,
127               t->timestamp_msbs);
128 #endif
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);
137     elt_index++;
138     elt += trace_data_size_in_words;
139   }
140   return (s);
141 }
142
143 int
144 ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t *b, ip6_header_t *ip, ip6_hop_by_hop_option_t *opt)
145 {
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;
149   u8 elt_index = 0;
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);
153   time_u64_t time_u64;
154   u32 *elt;
155   int rv = 0;
156
157   time_u64.as_u64 = 0;
158
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.
163      */
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);
168       elt++;
169     }
170
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);
174       elt++;
175     }
176                  
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));
180
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]);
183       elt++;
184     }
185
186     if (trace->ioam_trace_type & BIT_APPDATA) {
187       /* $$$ set elt0->app_data */
188       *elt = clib_host_to_net_u32(hm->app_data);
189       elt++;
190     }
191   }
192   return (rv);
193 }
194
195 /* The main h-b-h tracer will be invoked, no need to do much here */
196 int
197 ip6_hbh_add_register_option (u8 option,
198                              u8 size,
199                              int rewrite_options(u8 *rewrite_string, u8 rewrite_size))
200 {
201   ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
202
203   ASSERT (option < ARRAY_LEN (hm->add_options));
204
205   /* Already registered */
206   if (hm->add_options[option])
207     return (-1);
208
209   hm->add_options[option] = rewrite_options;
210   hm->options_size[option] = size;
211   
212   return (0);
213 }
214
215 int
216 ip6_hbh_add_unregister_option (u8 option)
217 {
218   ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
219
220   ASSERT (option < ARRAY_LEN (hm->add_options));
221
222   /* Not registered */
223   if (!hm->add_options[option])
224     return (-1);
225
226   hm->add_options[option] = NULL;
227   hm->options_size[option] = 0;
228   return (0);
229 }
230
231 typedef struct {
232   u32 next_index;
233 } ip6_add_hop_by_hop_trace_t;
234
235 /* packet trace format function */
236 static u8 * format_ip6_add_hop_by_hop_trace (u8 * s, va_list * args)
237 {
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 *);
242   
243   s = format (s, "IP6_ADD_HOP_BY_HOP: next index %d",
244               t->next_index);
245   return s;
246 }
247
248 vlib_node_registration_t ip6_add_hop_by_hop_node;
249
250 #define foreach_ip6_add_hop_by_hop_error \
251 _(PROCESSED, "Pkts w/ added ip6 hop-by-hop options")
252
253 typedef enum {
254 #define _(sym,str) IP6_ADD_HOP_BY_HOP_ERROR_##sym,
255   foreach_ip6_add_hop_by_hop_error
256 #undef _
257   IP6_ADD_HOP_BY_HOP_N_ERROR,
258 } ip6_add_hop_by_hop_error_t;
259
260 static char * ip6_add_hop_by_hop_error_strings[] = {
261 #define _(sym,string) string,
262   foreach_ip6_add_hop_by_hop_error
263 #undef _
264 };
265
266 static uword
267 ip6_add_hop_by_hop_node_fn (vlib_main_t * vm,
268                   vlib_node_runtime_t * node,
269                   vlib_frame_t * frame)
270 {
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;
274   u32 processed = 0;
275   u8 * rewrite = hm->rewrite;
276   u32 rewrite_length = vec_len (rewrite);
277
278   from = vlib_frame_vector_args (frame);
279   n_left_from = frame->n_vectors;
280   next_index = node->cached_next_index;
281
282   while (n_left_from > 0)
283     {
284       u32 n_left_to_next;
285
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)
289         {
290           u32 bi0, bi1;
291           vlib_buffer_t * b0, *b1;
292           u32 next0, next1;
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;
296           u16 new_l0, new_l1;
297
298           /* Prefetch next iteration. */
299           {
300             vlib_buffer_t * p2, * p3;
301
302             p2 = vlib_get_buffer (vm, from[2]);
303             p3 = vlib_get_buffer (vm, from[3]);
304
305             vlib_prefetch_buffer_header (p2, LOAD);
306             vlib_prefetch_buffer_header (p3, LOAD);
307
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);
310           }
311
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];
315           from += 2;
316           to_next += 2;
317           n_left_from -= 2;
318           n_left_to_next -= 2;
319
320           b0 = vlib_get_buffer (vm, bi0);
321           b1 = vlib_get_buffer (vm, bi1);
322
323           /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
324           ASSERT (b0->current_data == 0);
325           ASSERT (b1->current_data == 0);
326
327           ip0 = vlib_buffer_get_current (b0);
328           ip1 = vlib_buffer_get_current (b1);
329
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;
335
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];
341
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];
347
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);
352
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;
361           ip0->protocol = 0;
362           ip1->protocol = 0;
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);
367
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;
371
372
373           /* $$$$$ End of processing 2 x packets $$$$$ */
374
375           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
376             {
377               if (b0->flags & VLIB_BUFFER_IS_TRACED)
378                 {
379                   ip6_add_hop_by_hop_trace_t *t =
380                     vlib_add_trace (vm, node, b0, sizeof (*t));
381                   t->next_index = next0;
382                 }
383               if (b1->flags & VLIB_BUFFER_IS_TRACED)
384                 {
385                   ip6_add_hop_by_hop_trace_t *t =
386                     vlib_add_trace (vm, node, b1, sizeof (*t));
387                   t->next_index = next1;
388                 }
389             }
390           processed+=2;
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);
395         }
396       while (n_left_from > 0 && n_left_to_next > 0)
397         {
398           u32 bi0;
399           vlib_buffer_t * b0;
400           u32 next0;
401           ip6_header_t * ip0;
402           ip6_hop_by_hop_header_t * hbh0;
403           u64 * copy_src0, * copy_dst0;
404           u16 new_l0;
405           
406           /* speculatively enqueue b0 to the current next frame */
407           bi0 = from[0];
408           to_next[0] = bi0;
409           from += 1;
410           to_next += 1;
411           n_left_from -= 1;
412           n_left_to_next -= 1;
413
414           b0 = vlib_get_buffer (vm, bi0);
415
416           ip0 = vlib_buffer_get_current (b0);
417
418           /* Copy the ip header left by the required amount */
419           copy_dst0 = (u64 *)(((u8 *)ip0) - rewrite_length);
420           copy_src0 = (u64 *) ip0;
421
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);
429
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;
435           ip0->protocol = 0;
436           new_l0 = clib_net_to_host_u16 (ip0->payload_length) + rewrite_length;
437           ip0->payload_length = clib_host_to_net_u16 (new_l0);
438           
439           /* Populate the (first) h-b-h list elt */
440           next0 = IP6_HBYH_IOAM_INPUT_NEXT_IP6_LOOKUP;
441
442           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
443                             && (b0->flags & VLIB_BUFFER_IS_TRACED))) 
444             {
445               ip6_add_hop_by_hop_trace_t *t = 
446                  vlib_add_trace (vm, node, b0, sizeof (*t));
447               t->next_index = next0;
448             }
449             
450           processed++;
451
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,
455                                            bi0, next0);
456         }
457
458       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
459     }
460
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;
464 }
465
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,
472   
473   .n_errors = ARRAY_LEN(ip6_add_hop_by_hop_error_strings),
474   .error_strings = ip6_add_hop_by_hop_error_strings,
475
476   /* See ip/lookup.h */
477   .n_next_nodes = IP6_HBYH_IOAM_INPUT_N_NEXT,
478   .next_nodes = {
479 #define _(s,n) [IP6_HBYH_IOAM_INPUT_NEXT_##s] = n,
480     foreach_ip6_hbyh_ioam_input_next
481 #undef _
482   },
483 };
484
485 VLIB_NODE_FUNCTION_MULTIARCH (ip6_add_hop_by_hop_node, ip6_add_hop_by_hop_node_fn)
486
487 /* The main h-b-h tracer was already invoked, no need to do much here */
488 typedef struct {
489   u32 next_index;
490 } ip6_pop_hop_by_hop_trace_t;
491
492 /* packet trace format function */
493 static u8 * format_ip6_pop_hop_by_hop_trace (u8 * s, va_list * args)
494 {
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 *);
498   
499   s = format (s, "IP6_POP_HOP_BY_HOP: next index %d",
500               t->next_index);
501   return s;
502 }
503
504 int
505 ip6_hbh_pop_register_option (u8 option,
506                              int options(ip6_header_t *ip, ip6_hop_by_hop_option_t *opt))
507 {
508   ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
509
510   ASSERT (option < ARRAY_LEN (hm->pop_options));
511
512   /* Already registered */
513   if (hm->pop_options[option])
514     return (-1);
515
516   hm->pop_options[option] = options;
517
518   return (0);
519 }
520
521 int
522 ip6_hbh_pop_unregister_option (u8 option)
523 {
524   ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
525
526   ASSERT (option < ARRAY_LEN (hm->pop_options));
527
528   /* Not registered */
529   if (!hm->pop_options[option])
530     return (-1);
531
532   hm->pop_options[option] = NULL;
533   return (0);
534 }
535
536 vlib_node_registration_t ip6_pop_hop_by_hop_node;
537
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")
542
543 typedef enum {
544 #define _(sym,str) IP6_POP_HOP_BY_HOP_ERROR_##sym,
545   foreach_ip6_pop_hop_by_hop_error
546 #undef _
547   IP6_POP_HOP_BY_HOP_N_ERROR,
548 } ip6_pop_hop_by_hop_error_t;
549
550 static char * ip6_pop_hop_by_hop_error_strings[] = {
551 #define _(sym,string) string,
552   foreach_ip6_pop_hop_by_hop_error
553 #undef _
554 };
555
556 static inline void ioam_pop_hop_by_hop_processing (vlib_main_t * vm,
557                                                 ip6_header_t *ip0,
558                                                 ip6_hop_by_hop_header_t *hbh0)
559 {
560   ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
561   ip6_hop_by_hop_option_t *opt0, *limit0;
562   u8 type0;
563
564   if (!hbh0 || !ip0) return;
565
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));
569
570   /* Scan the set of h-b-h options, process ones that we understand */
571   while (opt0 < limit0)
572     {
573       type0 = opt0->type;
574       switch (type0)
575         {
576         case 0: /* Pad1 */
577           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *)opt0) + 1;
578           continue;
579         case 1: /* PadN */
580           break;
581         default:
582           if (hm->pop_options[type0])
583             {
584               if ((*hm->pop_options[type0])(ip0, opt0) < 0)
585               {
586                 vlib_node_increment_counter (vm, ip6_pop_hop_by_hop_node.index, 
587                                IP6_POP_HOP_BY_HOP_ERROR_OPTION_FAILED, 1);
588               }
589             }
590         }
591         opt0 = (ip6_hop_by_hop_option_t *) (((u8 *)opt0) + opt0->length + sizeof (ip6_hop_by_hop_option_t));
592     }
593 }
594
595 static uword
596 ip6_pop_hop_by_hop_node_fn (vlib_main_t * vm,
597                   vlib_node_runtime_t * node,
598                   vlib_frame_t * frame)
599 {
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;
604   u32 processed = 0;
605   u32 no_header = 0;
606   
607   from = vlib_frame_vector_args (frame);
608   n_left_from = frame->n_vectors;
609   next_index = node->cached_next_index;
610   
611   while (n_left_from > 0)
612     {
613       u32 n_left_to_next;
614
615       vlib_get_next_frame (vm, node, next_index,
616                            to_next, n_left_to_next);
617
618       while (n_left_from >= 4 && n_left_to_next >= 2)
619         {
620           u32 bi0, bi1;
621           vlib_buffer_t * b0, * b1;
622           u32 next0, next1;
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;
628           u16 new_l0, new_l1;
629
630           /* Prefetch next iteration. */
631           {
632             vlib_buffer_t * p2, * p3;
633
634             p2 = vlib_get_buffer (vm, from[2]);
635             p3 = vlib_get_buffer (vm, from[3]);
636
637             vlib_prefetch_buffer_header (p2, LOAD);
638             vlib_prefetch_buffer_header (p3, LOAD);
639
640             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
641             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
642           }
643
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];
647           from += 2;
648           to_next += 2;
649           n_left_from -= 2;
650           n_left_to_next -= 2;
651
652           b0 = vlib_get_buffer (vm, bi0);
653           b1 = vlib_get_buffer (vm, bi1);
654
655           /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
656           ASSERT (b0->current_data == 0);
657           ASSERT (b1->current_data == 0);
658
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);
665
666           next0 = adj0->lookup_next_index;
667           next1 = adj1->lookup_next_index;
668
669           hbh0 = (ip6_hop_by_hop_header_t *)(ip0+1);
670           hbh1 = (ip6_hop_by_hop_header_t *)(ip1+1);
671
672           ioam_pop_hop_by_hop_processing(vm, ip0, hbh0);
673           ioam_pop_hop_by_hop_processing(vm, ip1, hbh1);
674
675           vlib_buffer_advance (b0, (hbh0->length+1)<<3);
676           vlib_buffer_advance (b1, (hbh1->length+1)<<3);
677
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);
682
683           ip0->payload_length = clib_host_to_net_u16 (new_l0);
684           ip1->payload_length = clib_host_to_net_u16 (new_l1);
685
686           ip0->protocol = hbh0->protocol;
687           ip1->protocol = hbh1->protocol;
688
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];
703           processed+=2;
704           /* $$$$$ End of processing 2 x packets $$$$$ */
705
706           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
707             {
708               if (b0->flags & VLIB_BUFFER_IS_TRACED)
709                 {
710                     ip6_pop_hop_by_hop_trace_t *t =
711                       vlib_add_trace (vm, node, b0, sizeof (*t));
712                     t->next_index = next0;
713                   }
714                 if (b1->flags & VLIB_BUFFER_IS_TRACED)
715                   {
716                     ip6_pop_hop_by_hop_trace_t *t =
717                       vlib_add_trace (vm, node, b1, sizeof (*t));
718                     t->next_index = next1;
719                   }
720               }
721
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);
726         }
727
728       while (n_left_from > 0 && n_left_to_next > 0)
729         {
730           u32 bi0;
731           vlib_buffer_t * b0;
732           u32 next0;
733           u32 adj_index0;
734           ip6_header_t * ip0;
735           ip_adjacency_t * adj0;
736           ip6_hop_by_hop_header_t *hbh0;
737           u64 * copy_dst0, * copy_src0;
738           u16 new_l0;
739           
740           /* speculatively enqueue b0 to the current next frame */
741           bi0 = from[0];
742           to_next[0] = bi0;
743           from += 1;
744           to_next += 1;
745           n_left_from -= 1;
746           n_left_to_next -= 1;
747
748           b0 = vlib_get_buffer (vm, bi0);
749
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);
753
754           /* Default use the next_index from the adjacency. */
755           next0 = adj0->lookup_next_index;
756
757           /* Perfectly normal to end up here w/ out h-b-h header */
758           hbh0 = (ip6_hop_by_hop_header_t *)(ip0+1);
759           
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];
775           processed++;
776               
777           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
778                             && (b0->flags & VLIB_BUFFER_IS_TRACED))) 
779             {
780               ip6_pop_hop_by_hop_trace_t *t = 
781                  vlib_add_trace (vm, node, b0, sizeof (*t));
782               t->next_index = next0;
783             }
784
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,
788                                            bi0, next0);
789         }
790
791       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
792     }
793
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;
799 }
800
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,
810
811   /* See ip/lookup.h */
812   .n_next_nodes = 0,
813 };
814
815 VLIB_NODE_FUNCTION_MULTIARCH (ip6_pop_hop_by_hop_node,
816                               ip6_pop_hop_by_hop_node_fn)
817
818 static clib_error_t *
819 ip6_hop_by_hop_ioam_init (vlib_main_t * vm)
820 {
821   ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
822
823   hm->vlib_main = vm;
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));
832
833   /*
834    * Register the handlers
835    * XXX: This should be done dynamically based on OAM feature being enabled or not.
836    */
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"));
840
841   return (0);
842 }
843
844 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_init);
845
846 int ip6_ioam_set_rewrite (u8 **rwp, u32 trace_type, u32 trace_option_elts, 
847                           int has_pot_option, int has_ppc_option)
848 {
849   ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;  
850   u8 *rewrite = 0;
851   u32 size, rnd_size;
852   ip6_hop_by_hop_header_t *hbh;
853   ioam_trace_option_t * trace_option;
854   u8 *current;
855   u8 trace_data_size = 0;  
856
857   vec_free (*rwp);
858
859   if (trace_option_elts == 0 && has_pot_option == 0)
860     return -1;
861
862   /* Work out how much space we need */
863   size = sizeof (ip6_hop_by_hop_header_t);
864
865   if (trace_option_elts)
866     {
867       size += sizeof (ip6_hop_by_hop_option_t);
868
869       trace_data_size = fetch_trace_data_size(trace_type);
870       if (trace_data_size == 0)
871           return VNET_API_ERROR_INVALID_VALUE;
872
873       if (trace_option_elts * trace_data_size > 254)
874           return VNET_API_ERROR_INVALID_VALUE;
875   
876       size += trace_option_elts * trace_data_size;
877     }
878   if (has_pot_option && hm->add_options[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT] != 0)
879     {
880       size += sizeof (ip6_hop_by_hop_option_t);
881       size += hm->options_size[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT];
882     }
883
884   /* Round to a multiple of 8 octets */
885   rnd_size = (size + 7) & ~7;
886
887   /* allocate it, zero-fill / pad by construction */
888   vec_validate (rewrite, rnd_size-1);
889
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);
894   
895   if (trace_option_elts)
896     {
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;
907     }
908   if (has_pot_option && hm->add_options[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT] != 0)
909     {
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]);
913     }
914   
915   *rwp = rewrite;
916   return 0;
917 }
918
919 clib_error_t *
920 clear_ioam_rewrite_fn(void)
921 {
922   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
923
924   vec_free(hm->rewrite);
925   hm->rewrite = 0;
926   hm->node_id = 0;
927   hm->app_data = 0;
928   hm->trace_type = 0;
929   hm->trace_option_elts = 0;
930   hm->has_pot_option = 0;
931   hm->has_ppc_option = 0;
932   hm->trace_tsp = TSP_MICROSECONDS; 
933
934   return 0;
935 }
936
937 clib_error_t * clear_ioam_rewrite_command_fn (vlib_main_t * vm,
938                                  unformat_input_t * input,
939                                  vlib_cli_command_t * cmd)
940 {
941   return(clear_ioam_rewrite_fn());
942 }
943   
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,
948 };
949
950 clib_error_t *
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, 
953                            int has_ppc_option)
954 {
955   int rv;
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);
959
960   switch (rv)
961     {
962     case 0:
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;
970       break;
971
972     default:
973       return clib_error_return_code(0, rv, 0, "ip6_ioam_set_rewrite returned %d", rv);
974     }
975
976   return 0;
977 }
978
979
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)
984 {
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;
991   
992   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
993     {
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))
998             ;
999       else if (unformat (input, "pot"))
1000         has_pot_option = 1;
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;
1007       else
1008         break;
1009     }
1010   
1011     
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);
1014
1015     return rv;
1016 }
1017
1018
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,
1023 };
1024   
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)
1029 {
1030   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
1031   u8 *s = 0;
1032
1033
1034   if (!is_zero_ip6_address(&hm->adj))
1035   {
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"));
1042   } 
1043   else 
1044   {
1045   s = format(s, "              REWRITE FLOW CONFIGS - Not configured\n");
1046   }
1047
1048   if (hm->trace_option_elts)
1049   {
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);
1063   }
1064   else
1065   {
1066   s = format(s, " HOP BY HOP OPTIONS - TRACE CONFIG - Not configured\n");
1067   }
1068
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");
1073
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");
1078
1079   vlib_cli_output(vm, "%v", s);
1080   vec_free(s);
1081   return 0;
1082 }
1083
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,
1088 };
1089
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)
1092 {
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;
1097   u32 fib_index;
1098   u32 len, adj_index;
1099   int i, rv;
1100   uword * p;
1101   BVT(clib_bihash_kv) kv, value;
1102
1103   if ((is_add + is_pop + is_none) != 1)
1104     return VNET_API_ERROR_INVALID_VALUE_2;
1105
1106   /* Go find the adjacency we're supposed to tickle */
1107   p = hash_get (im->fib_index_by_table_id, vrf_id);
1108
1109   if (p == 0)
1110     return VNET_API_ERROR_NO_SUCH_FIB;
1111
1112   fib_index = p[0];
1113
1114   len = vec_len (im->prefix_lengths_in_search_order);
1115   
1116   for (i = 0; i < len; i++)
1117     {
1118       int dst_address_length = im->prefix_lengths_in_search_order[i];
1119       ip6_address_t * mask = &im->fib_masks[dst_address_length];
1120       
1121       if (dst_address_length != mask_width)
1122         continue;
1123
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;
1127       
1128       rv = BV(clib_bihash_search_inline_2)(&im->ip6_lookup_table, &kv, &value);
1129       if (rv == 0)
1130         goto found;
1131
1132     }
1133   return VNET_API_ERROR_NO_SUCH_ENTRY;
1134   
1135  found:
1136
1137   /* Got it, modify as directed... */
1138   adj_index = value.value;
1139   adj = ip_get_adjacency (lm, adj_index);
1140
1141   /* Restore original lookup-next action */
1142   if (adj->saved_lookup_next_index)
1143     {
1144       adj->lookup_next_index = adj->saved_lookup_next_index;
1145       adj->saved_lookup_next_index = 0;
1146     }
1147
1148   /* Save current action */
1149   if (is_add || is_pop)
1150     adj->saved_lookup_next_index = adj->lookup_next_index;
1151
1152   if (is_add)
1153     adj->lookup_next_index = (ip_lookup_next_t) IP6_LOOKUP_NEXT_ADD_HOP_BY_HOP;
1154
1155   if (is_pop)
1156     adj->lookup_next_index = (ip_lookup_next_t) IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
1157
1158   hm->adj = *addr;
1159   hm->ioam_flag = (is_add ? IOAM_HBYH_ADD :
1160                   (is_pop ? IOAM_HBYH_POP : IOAM_HBYH_MOD));
1161   return 0;
1162 }
1163                               
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)
1168 {
1169   ip6_address_t addr;
1170   u32 mask_width = ~0;
1171   int is_add = 0;
1172   int is_pop = 0;
1173   int is_none = 0;
1174   u32 vrf_id = 0;
1175   int rv;
1176
1177   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1178     {
1179       if (unformat (input, "%U/%d", 
1180                     unformat_ip6_address, &addr, &mask_width))
1181         ;
1182       else if (unformat (input, "vrf-id %d", &vrf_id))
1183         ;
1184       else if (unformat (input, "add"))
1185         is_add = 1;
1186       else if (unformat (input, "pop"))
1187         is_pop = 1;
1188       else if (unformat (input, "none"))
1189         is_none = 1;
1190       else
1191         break;
1192     }
1193
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");
1198
1199   rv = ip6_ioam_set_destination (&addr, mask_width, vrf_id, 
1200                                  is_add, is_pop, is_none);
1201
1202   switch (rv)
1203     {
1204     case 0:
1205       break;
1206     default:
1207       return clib_error_return (0, "ip6_ioam_set_destination returned %d", rv);
1208     }
1209   
1210   return 0;
1211 }
1212
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,
1217 };
1218
1219
1220 void vnet_register_ioam_end_of_path_callback (void *cb)
1221 {
1222   ip6_hop_by_hop_ioam_main_t * hm = &ip6_hop_by_hop_ioam_main;
1223
1224   hm->ioam_end_of_path_cb = cb;
1225 }