Create macro for next_nodes used in lookup family of nodes
[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_main_t ip6_hop_by_hop_main;
36
37 /*
38  * ip6 hop-by-hop option handling. We push pkts with h-b-h options to
39  * ip6_hop_by_hop_node_fn from ip6-lookup at a cost of ~2 clocks/pkt in
40  * the speed path.
41  * 
42  * We parse through the h-b-h option TLVs, specifically looking for
43  * HBH_OPTION_TYPE_IOAM_DATA_LIST. [Someone needs to get bananas from
44  * IANA, aka to actually allocate the option TLV codes.]
45  * 
46  * If we find the indicated option type, and we have remaining list
47  * elements in the trace list, allocate and populate the trace list
48  * element. 
49  *
50  * At the ingress edge: punch in the h-b-h rewrite, then visit the
51  * standard h-b-h option handler. We have to be careful in the standard 
52  * h-b-h handler, to avoid looping until we run out of rewrite space.
53  * Ask me how I know that.
54  * 
55  * Remaining work:
56  *  decide on egress point "pop and count" scheme
57  *  time stamp handling: usec since the top of the hour?
58  *  configure the node id
59  *  trace list application data support
60  *  cons up analysis / steering plug-in(s)
61  *  add configuration binary APIs, vpp_api_test_support, yang models and
62  *  orca code
63  *  perf tune: dual loop, replace memcpy w/ N x 8-byte load/stores
64  *  
65  */
66
67 /* 
68  * primary h-b-h handler trace support
69  * We work pretty hard on the problem for obvious reasons
70  */
71 typedef struct {
72   u32 next_index;
73   u32 trace_len;
74   u32 timestamp_msbs; /* Store the top set of bits of timestamp */
75   u8 option_data[256];
76 } ip6_hop_by_hop_trace_t;
77
78 typedef union {
79     u64 as_u64;
80     u32 as_u32[2];
81 } time_u64_t;
82
83 static inline u8
84 fetch_trace_data_size(u8 trace_type)
85 {
86   u8 trace_data_size = 0;
87
88   if (trace_type == TRACE_TYPE_IF_TS_APP)   
89       trace_data_size = sizeof(ioam_trace_if_ts_app_t);
90   else if(trace_type == TRACE_TYPE_IF)      
91       trace_data_size = sizeof(ioam_trace_if_t);
92   else if(trace_type == TRACE_TYPE_TS)      
93       trace_data_size = sizeof(ioam_trace_ts_t);
94   else if(trace_type == TRACE_TYPE_APP)     
95       trace_data_size = sizeof(ioam_trace_app_t);
96   else if(trace_type == TRACE_TYPE_TS_APP)  
97       trace_data_size = sizeof(ioam_trace_ts_app_t);
98
99   return trace_data_size;
100 }
101
102 static u8 * format_ioam_data_list_element (u8 * s, va_list * args)
103
104   u32 *elt = va_arg (*args, u32 *);
105   u8  *trace_type_p = va_arg (*args, u8 *);
106   u8  trace_type = *trace_type_p;
107
108
109   if (trace_type & BIT_TTL_NODEID)
110     {
111       u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
112       s = format (s, "ttl 0x%x node id 0x%x ",
113               ttl_node_id_host_byte_order>>24,
114               ttl_node_id_host_byte_order & 0x00FFFFFF);
115
116       elt++;
117     }
118  
119   if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
120     {
121         u32 ingress_host_byte_order = clib_net_to_host_u32(*elt);
122         s = format (s, "ingress 0x%x egress 0x%x ", 
123                    ingress_host_byte_order >> 16, 
124                    ingress_host_byte_order &0xFFFF);
125         elt++;
126     }
127  
128   if (trace_type & BIT_TIMESTAMP)
129     {
130         u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
131         s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
132         elt++;
133     }
134  
135   if (trace_type & BIT_APPDATA)
136     {
137         u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
138         s = format (s, "app 0x%x ", appdata_in_host_byte_order);
139         elt++;
140     }
141  
142   return s;
143 }
144
145 static u8 * format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
146 {
147   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
148   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
149   ip6_hop_by_hop_trace_t * t = va_arg (*args, ip6_hop_by_hop_trace_t *);
150   ip6_hop_by_hop_header_t *hbh0;
151   ip6_hop_by_hop_option_t *opt0, *limit0;
152   ioam_trace_option_t * trace0;
153   u8 trace_data_size_in_words = 0;
154   u32 * elt0;
155   int elt_index;
156   u8 type0;
157   
158   hbh0 = (ip6_hop_by_hop_header_t *)t->option_data;
159
160   s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d\n",
161               t->next_index, (hbh0->length+1)<<3, t->trace_len);
162   
163   opt0 = (ip6_hop_by_hop_option_t *) (hbh0+1);
164   limit0 = (ip6_hop_by_hop_option_t *) ((u8 *)hbh0) + t->trace_len;
165
166   while (opt0 < limit0)
167     {
168       type0 = opt0->type & HBH_OPTION_TYPE_MASK;
169       elt_index = 0;
170       switch (type0)
171         {
172         case HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST:
173           trace0 = (ioam_trace_option_t *)opt0;
174           s = format (s, "  Trace Type 0x%x , %d elts left ts msb(s) 0x%x\n", 
175                       trace0->ioam_trace_type, trace0->data_list_elts_left,
176                       t->timestamp_msbs);
177           trace_data_size_in_words = 
178             fetch_trace_data_size(trace0->ioam_trace_type)/4;
179           elt0 = &trace0->elts[0];
180           while ((u8 *) elt0 < 
181                  ((u8 *)(&trace0->elts[0]) + trace0->hdr.length - 2 
182                   /* -2 accounts for ioam_trace_type,elts_left */))
183             {
184               s = format (s, "    [%d] %U\n",elt_index,  
185                           format_ioam_data_list_element, 
186                           elt0, &trace0->ioam_trace_type);
187               elt_index++;
188               elt0 += trace_data_size_in_words;
189             }
190           
191           opt0 = (ip6_hop_by_hop_option_t *) 
192             (((u8 *)opt0) + opt0->length 
193              + sizeof (ip6_hop_by_hop_option_t));
194           break;
195
196         case HBH_OPTION_TYPE_IOAM_PROOF_OF_WORK:
197           s = format (s, "    POW opt present\n");
198           opt0 = (ip6_hop_by_hop_option_t *) 
199             (((u8 *)opt0) + sizeof (ioam_pow_option_t));
200           break;
201           
202         case 0: /* Pad, just stop */
203           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *)opt0) + 1;
204           break;
205
206         default:
207           s = format (s, "Unknown %d", type0);
208           opt0 = (ip6_hop_by_hop_option_t *) 
209             (((u8 *)opt0) + opt0->length 
210              + sizeof (ip6_hop_by_hop_option_t));
211           break;
212         }
213     }
214   return s;
215 }
216
217 vlib_node_registration_t ip6_hop_by_hop_node;
218
219 #define foreach_ip6_hop_by_hop_error \
220 _(PROCESSED, "Pkts with ip6 hop-by-hop options") \
221 _(UNKNOWN_OPTION, "Unknown ip6 hop-by-hop options")
222
223 typedef enum {
224 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
225   foreach_ip6_hop_by_hop_error
226 #undef _
227   IP6_HOP_BY_HOP_N_ERROR,
228 } ip6_hop_by_hop_error_t;
229
230 static char * ip6_hop_by_hop_error_strings[] = {
231 #define _(sym,string) string,
232   foreach_ip6_hop_by_hop_error
233 #undef _
234 };
235
236 static uword
237 ip6_hop_by_hop_node_fn (vlib_main_t * vm,
238                   vlib_node_runtime_t * node,
239                   vlib_frame_t * frame)
240 {
241   ip6_main_t * im = &ip6_main;
242   ip_lookup_main_t * lm = &im->lookup_main;
243   ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
244   u32 n_left_from, * from, * to_next;
245   ip_lookup_next_t next_index;
246   u32 processed = 0, unknown_opts = 0;
247   u8 elt_index = 0;
248   time_u64_t time_u64;
249
250   time_u64.as_u64 = 0;
251   from = vlib_frame_vector_args (frame);
252   n_left_from = frame->n_vectors;
253   next_index = node->cached_next_index;
254
255   while (n_left_from > 0)
256     {
257       u32 n_left_to_next;
258
259       vlib_get_next_frame (vm, node, next_index,
260                            to_next, n_left_to_next);
261
262 #if 0 /* $$$ DUAL-LOOP ME */
263       while (n_left_from >= 4 && n_left_to_next >= 2)
264         {
265           u32 next0 = IP6_HOP_BY_HOP_NEXT_INTERFACE_OUTPUT;
266           u32 next1 = IP6_HOP_BY_HOP_NEXT_INTERFACE_OUTPUT;
267           u32 sw_if_index0, sw_if_index1;
268           u8 tmp0[6], tmp1[6];
269           ethernet_header_t *en0, *en1;
270           u32 bi0, bi1;
271           vlib_buffer_t * b0, * b1;
272           
273           /* Prefetch next iteration. */
274           {
275             vlib_buffer_t * p2, * p3;
276             
277             p2 = vlib_get_buffer (vm, from[2]);
278             p3 = vlib_get_buffer (vm, from[3]);
279             
280             vlib_prefetch_buffer_header (p2, LOAD);
281             vlib_prefetch_buffer_header (p3, LOAD);
282
283             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
284             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
285           }
286
287           /* speculatively enqueue b0 and b1 to the current next frame */
288           to_next[0] = bi0 = from[0];
289           to_next[1] = bi1 = from[1];
290           from += 2;
291           to_next += 2;
292           n_left_from -= 2;
293           n_left_to_next -= 2;
294
295           b0 = vlib_get_buffer (vm, bi0);
296           b1 = vlib_get_buffer (vm, bi1);
297
298           /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
299           ASSERT (b0->current_data == 0);
300           ASSERT (b1->current_data == 0);
301           
302           ip0 = vlib_buffer_get_current (b0);
303           ip1 = vlib_buffer_get_current (b0);
304
305           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
306           sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
307
308           /* $$$$$ End of processing 2 x packets $$$$$ */
309
310           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
311             {
312               if (b0->flags & VLIB_BUFFER_IS_TRACED) 
313                 {
314                     ip6_hop_by_hop_trace_t *t = 
315                       vlib_add_trace (vm, node, b0, sizeof (*t));
316                     t->sw_if_index = sw_if_index0;
317                     t->next_index = next0;
318                   }
319                 if (b1->flags & VLIB_BUFFER_IS_TRACED) 
320                   {
321                     ip6_hop_by_hop_trace_t *t = 
322                       vlib_add_trace (vm, node, b1, sizeof (*t));
323                     t->sw_if_index = sw_if_index1;
324                     t->next_index = next1;
325                   }
326               }
327             
328             /* verify speculative enqueues, maybe switch current next frame */
329             vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
330                                              to_next, n_left_to_next,
331                                              bi0, bi1, next0, next1);
332         }
333 #endif
334
335       while (n_left_from > 0 && n_left_to_next > 0)
336         {
337           u32 bi0;
338           vlib_buffer_t * b0;
339           u32 next0;
340           u32 adj_index0;
341           ip6_header_t * ip0;
342           ip_adjacency_t * adj0;
343           ip6_hop_by_hop_header_t *hbh0;
344           ip6_hop_by_hop_option_t *opt0, *limit0;
345           ioam_trace_option_t * trace0;
346           u32 * elt0;
347           u8 type0;
348          
349           /* speculatively enqueue b0 to the current next frame */
350           bi0 = from[0];
351           to_next[0] = bi0;
352           from += 1;
353           to_next += 1;
354           n_left_from -= 1;
355           n_left_to_next -= 1;
356
357           b0 = vlib_get_buffer (vm, bi0);
358
359           ip0 = vlib_buffer_get_current (b0);
360           adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
361           adj0 = ip_get_adjacency (lm, adj_index0);
362           hbh0 = (ip6_hop_by_hop_header_t *)(ip0+1);
363           opt0 = (ip6_hop_by_hop_option_t *)(hbh0+1);
364           limit0 = (ip6_hop_by_hop_option_t *)
365             ((u8 *)hbh0 + ((hbh0->length+1)<<3));
366           
367           /* Scan the set of h-b-h options, process ones that we understand */
368           while (opt0 < limit0)
369             {
370               type0 = opt0->type & HBH_OPTION_TYPE_MASK;
371               switch (type0)
372                 {
373                 case HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST:
374                   trace0 = (ioam_trace_option_t *)opt0;
375                   if (PREDICT_TRUE (trace0->data_list_elts_left))
376                     {
377                       trace0->data_list_elts_left--;
378                       /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
379                        * to skip to this node's location.
380                        */
381                       elt_index = trace0->data_list_elts_left *
382                                   fetch_trace_data_size(trace0->ioam_trace_type)/4;
383                       elt0 = &trace0->elts[elt_index];
384                       if (trace0->ioam_trace_type & BIT_TTL_NODEID) 
385                         {
386                           *elt0 = 
387                             clib_host_to_net_u32 ((ip0->hop_limit<<24) 
388                                               | hm->node_id);
389                           elt0++;
390                         }
391
392                       if (trace0->ioam_trace_type & BIT_ING_INTERFACE) 
393                         {
394                           *elt0 =
395                           (vnet_buffer(b0)->sw_if_index[VLIB_RX]&0xFFFF) << 16 |                           (adj0->rewrite_header.sw_if_index & 0xFFFF);
396                           *elt0 = clib_host_to_net_u32(*elt0);
397                           elt0++;
398                         }
399                  
400                       if (trace0->ioam_trace_type & BIT_TIMESTAMP)
401                         {
402                             /* Send least significant 32 bits */
403                             f64 time_f64 = (f64)(((f64)hm->unix_time_0) +
404                               (vlib_time_now(hm->vlib_main) - hm->vlib_time_0));
405
406                             time_u64.as_u64 = 
407                                time_f64 * trace_tsp_mul[hm->trace_tsp];
408                             *elt0 = clib_host_to_net_u32(time_u64.as_u32[0]);
409                             elt0++;
410                         }
411
412                       if (trace0->ioam_trace_type & BIT_APPDATA)
413                         {
414                           /* $$$ set elt0->app_data */
415                           *elt0 = clib_host_to_net_u32(hm->app_data);
416                           elt0++;
417                         }
418                     }
419
420                   opt0 = (ip6_hop_by_hop_option_t *) 
421                     (((u8 *)opt0) + opt0->length 
422                      + sizeof (ip6_hop_by_hop_option_t));
423                   break;
424
425                 case HBH_OPTION_TYPE_IOAM_PROOF_OF_WORK:
426                   opt0 = (ip6_hop_by_hop_option_t *) 
427                     (((u8 *)opt0) + sizeof (ioam_pow_option_t));
428                   break;
429
430                 case 0: /* Pad */
431                   opt0 = (ip6_hop_by_hop_option_t *) ((u8 *)opt0) + 1;
432                   goto out0;
433
434                 default:
435                   opt0 = (ip6_hop_by_hop_option_t *)
436                   (((u8 *)opt0) + opt0->length
437                   + sizeof (ip6_hop_by_hop_option_t));
438                   unknown_opts++;
439                   break;
440                 }
441             }
442
443         out0:
444
445           /* 
446            * Since we push pkts here from the h-b-h header imposition code
447            * we have to be careful what we wish for...
448            */
449           next0 = adj0->lookup_next_index != IP_LOOKUP_NEXT_ADD_HOP_BY_HOP ?
450               adj0->lookup_next_index : adj0->saved_lookup_next_index;
451
452           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
453                             && (b0->flags & VLIB_BUFFER_IS_TRACED))) 
454             {
455               ip6_hop_by_hop_trace_t *t = 
456                  vlib_add_trace (vm, node, b0, sizeof (*t));
457               u32 trace_len = (hbh0->length+1)<<3;
458               t->next_index = next0;
459               /* Capture the h-b-h option verbatim */
460               trace_len = trace_len < ARRAY_LEN(t->option_data) ? 
461                 trace_len : ARRAY_LEN(t->option_data);
462               t->trace_len = trace_len;
463               t->timestamp_msbs = time_u64.as_u32[1];
464               memcpy (t->option_data, hbh0, trace_len);
465             }
466             
467           processed++;
468
469           /* verify speculative enqueue, maybe switch current next frame */
470           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
471                                            to_next, n_left_to_next,
472                                            bi0, next0);
473         }
474
475       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
476     }
477
478     if (PREDICT_FALSE(unknown_opts > 0)) {
479       vlib_node_increment_counter (vm, ip6_hop_by_hop_node.index,
480                                    IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION, unknown_opts);
481     }
482
483   vlib_node_increment_counter (vm, ip6_hop_by_hop_node.index, 
484                                IP6_HOP_BY_HOP_ERROR_PROCESSED, processed);
485   return frame->n_vectors;
486 }
487
488 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) = {
489   .function = ip6_hop_by_hop_node_fn,
490   .name = "ip6-hop-by-hop",
491   .vector_size = sizeof (u32),
492   .format_trace = format_ip6_hop_by_hop_trace,
493   .type = VLIB_NODE_TYPE_INTERNAL,
494   
495   .n_errors = ARRAY_LEN(ip6_hop_by_hop_error_strings),
496   .error_strings = ip6_hop_by_hop_error_strings,
497
498   /* See ip/lookup.h */
499   .n_next_nodes = IP_LOOKUP_N_NEXT,
500   .next_nodes = IP6_LOOKUP_NEXT_NODES,
501 };
502
503 /* The main h-b-h tracer will be invoked, no need to do much here */
504 typedef struct {
505   u32 next_index;
506 } ip6_add_hop_by_hop_trace_t;
507
508 /* packet trace format function */
509 static u8 * format_ip6_add_hop_by_hop_trace (u8 * s, va_list * args)
510 {
511   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
512   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
513   ip6_add_hop_by_hop_trace_t * t = va_arg (*args, 
514                                             ip6_add_hop_by_hop_trace_t *);
515   
516   s = format (s, "IP6_ADD_HOP_BY_HOP: next index %d",
517               t->next_index);
518   return s;
519 }
520
521 vlib_node_registration_t ip6_add_hop_by_hop_node;
522
523 #define foreach_ip6_add_hop_by_hop_error \
524 _(PROCESSED, "Pkts w/ added ip6 hop-by-hop options")
525
526 typedef enum {
527 #define _(sym,str) IP6_ADD_HOP_BY_HOP_ERROR_##sym,
528   foreach_ip6_add_hop_by_hop_error
529 #undef _
530   IP6_ADD_HOP_BY_HOP_N_ERROR,
531 } ip6_add_hop_by_hop_error_t;
532
533 static char * ip6_add_hop_by_hop_error_strings[] = {
534 #define _(sym,string) string,
535   foreach_ip6_add_hop_by_hop_error
536 #undef _
537 };
538
539 static uword
540 ip6_add_hop_by_hop_node_fn (vlib_main_t * vm,
541                   vlib_node_runtime_t * node,
542                   vlib_frame_t * frame)
543 {
544   ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
545   u32 n_left_from, * from, * to_next;
546   ip_lookup_next_t next_index;
547   u32 processed = 0;
548   u8 * rewrite = hm->rewrite;
549   u32 rewrite_length = vec_len (rewrite);
550
551   from = vlib_frame_vector_args (frame);
552   n_left_from = frame->n_vectors;
553   next_index = node->cached_next_index;
554
555   while (n_left_from > 0)
556     {
557       u32 n_left_to_next;
558
559       vlib_get_next_frame (vm, node, next_index,
560                            to_next, n_left_to_next);
561
562 #if 0
563       while (n_left_from >= 4 && n_left_to_next >= 2)
564         {
565           u32 next0 = IP6_ADD_HOP_BY_HOP_NEXT_INTERFACE_OUTPUT;
566           u32 next1 = IP6_ADD_HOP_BY_HOP_NEXT_INTERFACE_OUTPUT;
567           u32 sw_if_index0, sw_if_index1;
568           u8 tmp0[6], tmp1[6];
569           ethernet_header_t *en0, *en1;
570           u32 bi0, bi1;
571           vlib_buffer_t * b0, * b1;
572           
573           /* Prefetch next iteration. */
574           {
575             vlib_buffer_t * p2, * p3;
576             
577             p2 = vlib_get_buffer (vm, from[2]);
578             p3 = vlib_get_buffer (vm, from[3]);
579             
580             vlib_prefetch_buffer_header (p2, LOAD);
581             vlib_prefetch_buffer_header (p3, LOAD);
582
583             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
584             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
585           }
586
587           /* speculatively enqueue b0 and b1 to the current next frame */
588           to_next[0] = bi0 = from[0];
589           to_next[1] = bi1 = from[1];
590           from += 2;
591           to_next += 2;
592           n_left_from -= 2;
593           n_left_to_next -= 2;
594
595           b0 = vlib_get_buffer (vm, bi0);
596           b1 = vlib_get_buffer (vm, bi1);
597
598           /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
599           ASSERT (b0->current_data == 0);
600           ASSERT (b1->current_data == 0);
601           
602           ip0 = vlib_buffer_get_current (b0);
603           ip1 = vlib_buffer_get_current (b0);
604
605           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
606           sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
607
608           /* $$$$$ End of processing 2 x packets $$$$$ */
609
610           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
611             {
612               if (b0->flags & VLIB_BUFFER_IS_TRACED) 
613                 {
614                     ip6_add_hop_by_hop_trace_t *t = 
615                       vlib_add_trace (vm, node, b0, sizeof (*t));
616                     t->sw_if_index = sw_if_index0;
617                     t->next_index = next0;
618                   }
619                 if (b1->flags & VLIB_BUFFER_IS_TRACED) 
620                   {
621                     ip6_add_hop_by_hop_trace_t *t = 
622                       vlib_add_trace (vm, node, b1, sizeof (*t));
623                     t->sw_if_index = sw_if_index1;
624                     t->next_index = next1;
625                   }
626               }
627             
628             /* verify speculative enqueues, maybe switch current next frame */
629             vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
630                                              to_next, n_left_to_next,
631                                              bi0, bi1, next0, next1);
632         }
633 #endif
634
635       while (n_left_from > 0 && n_left_to_next > 0)
636         {
637           u32 bi0;
638           vlib_buffer_t * b0;
639           u32 next0;
640           ip6_header_t * ip0;
641           ip6_hop_by_hop_header_t * hbh0;
642           u64 * copy_src0, * copy_dst0;
643           u16 new_l0;
644           
645           /* speculatively enqueue b0 to the current next frame */
646           bi0 = from[0];
647           to_next[0] = bi0;
648           from += 1;
649           to_next += 1;
650           n_left_from -= 1;
651           n_left_to_next -= 1;
652
653           b0 = vlib_get_buffer (vm, bi0);
654
655           ip0 = vlib_buffer_get_current (b0);
656
657           /* Copy the ip header left by the required amount */
658           copy_dst0 = (u64 *)(((u8 *)ip0) - rewrite_length);
659           copy_src0 = (u64 *) ip0;
660
661           copy_dst0 [0] = copy_src0 [0];
662           copy_dst0 [1] = copy_src0 [1];
663           copy_dst0 [2] = copy_src0 [2];
664           copy_dst0 [3] = copy_src0 [3];
665           copy_dst0 [4] = copy_src0 [4];
666           vlib_buffer_advance (b0, - (word)rewrite_length);
667           ip0 = vlib_buffer_get_current (b0);
668
669           hbh0 = (ip6_hop_by_hop_header_t *)(ip0 + 1);
670           /* $$$ tune, rewrite_length is a multiple of 8 */
671           memcpy (hbh0, rewrite, rewrite_length);
672           /* Patch the protocol chain, insert the h-b-h (type 0) header */
673           hbh0->protocol = ip0->protocol;
674           ip0->protocol = 0;
675           new_l0 = clib_net_to_host_u16 (ip0->payload_length) + rewrite_length;
676           ip0->payload_length = clib_host_to_net_u16 (new_l0);
677           
678           /* Populate the (first) h-b-h list elt */
679           next0 = IP_LOOKUP_NEXT_HOP_BY_HOP;
680
681           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
682                             && (b0->flags & VLIB_BUFFER_IS_TRACED))) 
683             {
684               ip6_add_hop_by_hop_trace_t *t = 
685                  vlib_add_trace (vm, node, b0, sizeof (*t));
686               t->next_index = next0;
687             }
688             
689           processed++;
690
691           /* verify speculative enqueue, maybe switch current next frame */
692           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
693                                            to_next, n_left_to_next,
694                                            bi0, next0);
695         }
696
697       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
698     }
699
700   vlib_node_increment_counter (vm, ip6_add_hop_by_hop_node.index, 
701                                IP6_ADD_HOP_BY_HOP_ERROR_PROCESSED, processed);
702   return frame->n_vectors;
703 }
704
705 VLIB_REGISTER_NODE (ip6_add_hop_by_hop_node) = {
706   .function = ip6_add_hop_by_hop_node_fn,
707   .name = "ip6-add-hop-by-hop",
708   .vector_size = sizeof (u32),
709   .format_trace = format_ip6_add_hop_by_hop_trace,
710   .type = VLIB_NODE_TYPE_INTERNAL,
711   
712   .n_errors = ARRAY_LEN(ip6_add_hop_by_hop_error_strings),
713   .error_strings = ip6_add_hop_by_hop_error_strings,
714
715   /* See ip/lookup.h */
716   .n_next_nodes = IP_LOOKUP_N_NEXT,
717   .next_nodes = IP6_LOOKUP_NEXT_NODES,
718 };
719
720
721 /* The main h-b-h tracer was already invoked, no need to do much here */
722 typedef struct {
723   u32 next_index;
724 } ip6_pop_hop_by_hop_trace_t;
725
726 /* packet trace format function */
727 static u8 * format_ip6_pop_hop_by_hop_trace (u8 * s, va_list * args)
728 {
729   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
730   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
731   ip6_pop_hop_by_hop_trace_t * t = va_arg (*args, ip6_pop_hop_by_hop_trace_t *);
732   
733   s = format (s, "IP6_POP_HOP_BY_HOP: next index %d",
734               t->next_index);
735   return s;
736 }
737
738 vlib_node_registration_t ip6_pop_hop_by_hop_node;
739
740 #define foreach_ip6_pop_hop_by_hop_error                \
741 _(PROCESSED, "Pkts w/ removed ip6 hop-by-hop options")  \
742 _(NO_HOHO, "Pkts w/ no ip6 hop-by-hop options")
743
744 typedef enum {
745 #define _(sym,str) IP6_POP_HOP_BY_HOP_ERROR_##sym,
746   foreach_ip6_pop_hop_by_hop_error
747 #undef _
748   IP6_POP_HOP_BY_HOP_N_ERROR,
749 } ip6_pop_hop_by_hop_error_t;
750
751 static char * ip6_pop_hop_by_hop_error_strings[] = {
752 #define _(sym,string) string,
753   foreach_ip6_pop_hop_by_hop_error
754 #undef _
755 };
756
757 static uword
758 ip6_pop_hop_by_hop_node_fn (vlib_main_t * vm,
759                   vlib_node_runtime_t * node,
760                   vlib_frame_t * frame)
761 {
762   ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
763   ip6_main_t * im = &ip6_main;
764   ip_lookup_main_t * lm = &im->lookup_main;
765   u32 n_left_from, * from, * to_next;
766   ip_lookup_next_t next_index;
767   u32 processed = 0;
768   u32 no_header = 0;
769   u32 (*ioam_end_of_path_cb) (vlib_main_t *, vlib_node_runtime_t *,
770                               vlib_buffer_t *, ip6_header_t *, 
771                               ip_adjacency_t *);
772   
773   ioam_end_of_path_cb = hm->ioam_end_of_path_cb;
774   
775   from = vlib_frame_vector_args (frame);
776   n_left_from = frame->n_vectors;
777   next_index = node->cached_next_index;
778   
779   while (n_left_from > 0)
780     {
781       u32 n_left_to_next;
782
783       vlib_get_next_frame (vm, node, next_index,
784                            to_next, n_left_to_next);
785
786 #if 0
787       while (n_left_from >= 4 && n_left_to_next >= 2)
788         {
789           u32 next0 = IP6_POP_HOP_BY_HOP_NEXT_INTERFACE_OUTPUT;
790           u32 next1 = IP6_POP_HOP_BY_HOP_NEXT_INTERFACE_OUTPUT;
791           u32 sw_if_index0, sw_if_index1;
792           u8 tmp0[6], tmp1[6];
793           ethernet_header_t *en0, *en1;
794           u32 bi0, bi1;
795           vlib_buffer_t * b0, * b1;
796           
797           /* Prefetch next iteration. */
798           {
799             vlib_buffer_t * p2, * p3;
800             
801             p2 = vlib_get_buffer (vm, from[2]);
802             p3 = vlib_get_buffer (vm, from[3]);
803             
804             vlib_prefetch_buffer_header (p2, LOAD);
805             vlib_prefetch_buffer_header (p3, LOAD);
806
807             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
808             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
809           }
810
811           /* speculatively enqueue b0 and b1 to the current next frame */
812           to_next[0] = bi0 = from[0];
813           to_next[1] = bi1 = from[1];
814           from += 2;
815           to_next += 2;
816           n_left_from -= 2;
817           n_left_to_next -= 2;
818
819           b0 = vlib_get_buffer (vm, bi0);
820           b1 = vlib_get_buffer (vm, bi1);
821
822           /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
823           ASSERT (b0->current_data == 0);
824           ASSERT (b1->current_data == 0);
825           
826           ip0 = vlib_buffer_get_current (b0);
827           ip1 = vlib_buffer_get_current (b0);
828
829           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
830           sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
831
832           /* $$$$$ End of processing 2 x packets $$$$$ */
833
834           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
835             {
836               if (b0->flags & VLIB_BUFFER_IS_TRACED) 
837                 {
838                     ip6_pop_hop_by_hop_trace_t *t = 
839                       vlib_add_trace (vm, node, b0, sizeof (*t));
840                     t->sw_if_index = sw_if_index0;
841                     t->next_index = next0;
842                   }
843                 if (b1->flags & VLIB_BUFFER_IS_TRACED) 
844                   {
845                     ip6_pop_hop_by_hop_trace_t *t = 
846                       vlib_add_trace (vm, node, b1, sizeof (*t));
847                     t->sw_if_index = sw_if_index1;
848                     t->next_index = next1;
849                   }
850               }
851             
852             /* verify speculative enqueues, maybe switch current next frame */
853             vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
854                                              to_next, n_left_to_next,
855                                              bi0, bi1, next0, next1);
856         }
857 #endif
858
859       while (n_left_from > 0 && n_left_to_next > 0)
860         {
861           u32 bi0;
862           vlib_buffer_t * b0;
863           u32 next0;
864           u32 adj_index0;
865           ip6_header_t * ip0;
866           ip_adjacency_t * adj0;
867           ip6_hop_by_hop_header_t *hbh0;
868           u64 * copy_dst0, * copy_src0;
869           u16 new_l0;
870           
871           /* speculatively enqueue b0 to the current next frame */
872           bi0 = from[0];
873           to_next[0] = bi0;
874           from += 1;
875           to_next += 1;
876           n_left_from -= 1;
877           n_left_to_next -= 1;
878
879           b0 = vlib_get_buffer (vm, bi0);
880
881           ip0 = vlib_buffer_get_current (b0);
882           adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
883           adj0 = ip_get_adjacency (lm, adj_index0);
884
885           /* Perfectly normal to end up here w/ out h-b-h header */
886           if (PREDICT_TRUE (ip0->protocol == 0))
887             {
888               hbh0 = (ip6_hop_by_hop_header_t *)(ip0+1);
889           
890               /* Collect data from trace via callback */
891               next0 = ioam_end_of_path_cb ? 
892                 ioam_end_of_path_cb (vm, node, b0, ip0, adj0) 
893                 : adj0->saved_lookup_next_index;
894               
895               
896               /* Pop the trace data */
897               vlib_buffer_advance (b0, (hbh0->length+1)<<3);
898               new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
899                 ((hbh0->length+1)<<3);
900               ip0->payload_length = clib_host_to_net_u16 (new_l0);
901               ip0->protocol = hbh0->protocol;
902               copy_src0 = (u64 *)ip0;
903               copy_dst0 = copy_src0 + (hbh0->length+1);
904               copy_dst0 [4] = copy_src0[4];
905               copy_dst0 [3] = copy_src0[3];
906               copy_dst0 [2] = copy_src0[2];
907               copy_dst0 [1] = copy_src0[1];
908               copy_dst0 [0] = copy_src0[0];
909               processed++;
910             }
911           else
912             {
913               next0 = adj0->saved_lookup_next_index;
914               no_header++;
915             }
916               
917           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
918                             && (b0->flags & VLIB_BUFFER_IS_TRACED))) 
919             {
920               ip6_pop_hop_by_hop_trace_t *t = 
921                  vlib_add_trace (vm, node, b0, sizeof (*t));
922               t->next_index = next0;
923             }
924
925           /* verify speculative enqueue, maybe switch current next frame */
926           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
927                                            to_next, n_left_to_next,
928                                            bi0, next0);
929         }
930
931       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
932     }
933
934   vlib_node_increment_counter (vm, ip6_pop_hop_by_hop_node.index, 
935                                IP6_POP_HOP_BY_HOP_ERROR_PROCESSED, processed);
936   vlib_node_increment_counter (vm, ip6_pop_hop_by_hop_node.index, 
937                                IP6_POP_HOP_BY_HOP_ERROR_NO_HOHO, no_header);
938   return frame->n_vectors;
939 }
940
941 VLIB_REGISTER_NODE (ip6_pop_hop_by_hop_node) = {
942   .function = ip6_pop_hop_by_hop_node_fn,
943   .name = "ip6-pop-hop-by-hop",
944   .vector_size = sizeof (u32),
945   .format_trace = format_ip6_pop_hop_by_hop_trace,
946   .type = VLIB_NODE_TYPE_INTERNAL,
947   
948   .n_errors = ARRAY_LEN(ip6_pop_hop_by_hop_error_strings),
949   .error_strings = ip6_pop_hop_by_hop_error_strings,
950
951   /* See ip/lookup.h */
952   .n_next_nodes = IP_LOOKUP_N_NEXT,
953   .next_nodes = IP6_LOOKUP_NEXT_NODES,
954 };
955
956
957 static clib_error_t *
958 ip6_hop_by_hop_init (vlib_main_t * vm)
959 {
960   ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
961
962   hm->vlib_main = vm;
963   hm->vnet_main = vnet_get_main();
964   hm->unix_time_0 = (u32) time (0); /* Store starting time */
965   hm->vlib_time_0 = vlib_time_now (vm);
966   hm->ioam_flag = IOAM_HBYH_MOD;
967   hm->trace_tsp = TSP_MICROSECONDS; /* Micro seconds */
968
969   return 0;
970 }
971
972 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
973
974 int ip6_ioam_set_rewrite (u8 **rwp, u32 trace_type, u32 trace_option_elts, 
975                           int has_pow_option, int has_ppc_option)
976 {
977   u8 *rewrite = 0;
978   u32 size, rnd_size;
979   ip6_hop_by_hop_header_t *hbh;
980   ioam_trace_option_t * trace_option;
981   ioam_pow_option_t * pow_option;
982   u8 *current;
983   u8 trace_data_size = 0;  
984
985   vec_free (*rwp);
986
987   if (trace_option_elts == 0 && has_pow_option == 0)
988     return -1;
989
990   /* Work out how much space we need */
991   size = sizeof (ip6_hop_by_hop_header_t);
992
993   if (trace_option_elts)
994     {
995       size += sizeof (ip6_hop_by_hop_option_t);
996
997       trace_data_size = fetch_trace_data_size(trace_type);
998       if (trace_data_size == 0)
999           return VNET_API_ERROR_INVALID_VALUE;
1000
1001       if (trace_option_elts * trace_data_size > 254)
1002           return VNET_API_ERROR_INVALID_VALUE;
1003   
1004       size += trace_option_elts * trace_data_size;
1005     }
1006   if (has_pow_option)
1007     {
1008       size += sizeof (ip6_hop_by_hop_option_t);
1009       size += sizeof (ioam_pow_option_t);
1010     }
1011
1012   /* Round to a multiple of 8 octets */
1013   rnd_size = (size + 7) & ~7;
1014
1015   /* allocate it, zero-fill / pad by construction */
1016   vec_validate (rewrite, rnd_size-1);
1017
1018   hbh = (ip6_hop_by_hop_header_t *) rewrite;
1019   /* Length of header in 8 octet units, not incl first 8 octets */
1020   hbh->length = (rnd_size>>3) - 1;
1021   current = (u8 *)(hbh+1);
1022   
1023   if (trace_option_elts)
1024     {
1025       trace_option = (ioam_trace_option_t *)current;
1026       trace_option->hdr.type = HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST
1027         | HBH_OPTION_TYPE_DATA_CHANGE_ENROUTE;
1028       trace_option->hdr.length = 
1029                2 /*ioam_trace_type,data_list_elts_left */ + 
1030               trace_option_elts * trace_data_size;
1031       trace_option->ioam_trace_type = trace_type & TRACE_TYPE_MASK;
1032       trace_option->data_list_elts_left = trace_option_elts;
1033       current += sizeof (ioam_trace_option_t) + 
1034         trace_option_elts * trace_data_size;
1035     }
1036   if (has_pow_option)
1037     {
1038       pow_option = (ioam_pow_option_t *)current;
1039       pow_option->hdr.type = HBH_OPTION_TYPE_IOAM_PROOF_OF_WORK
1040         | HBH_OPTION_TYPE_DATA_CHANGE_ENROUTE;
1041       pow_option->hdr.length = sizeof (ioam_pow_option_t) - 
1042         sizeof (ip6_hop_by_hop_option_t);
1043       current += sizeof (ioam_pow_option_t);
1044     }
1045   
1046   *rwp = rewrite;
1047   return 0;
1048 }
1049
1050 clib_error_t *
1051 clear_ioam_rewrite_fn(void)
1052 {
1053   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
1054
1055   vec_free(hm->rewrite);
1056   hm->rewrite = 0;
1057   hm->node_id = 0;
1058   hm->app_data = 0;
1059   hm->trace_type = 0;
1060   hm->trace_option_elts = 0;
1061   hm->has_pow_option = 0;
1062   hm->has_ppc_option = 0;
1063   hm->trace_tsp = TSP_MICROSECONDS; 
1064
1065   return 0;
1066 }
1067
1068 clib_error_t * clear_ioam_rewrite_command_fn (vlib_main_t * vm,
1069                                  unformat_input_t * input,
1070                                  vlib_cli_command_t * cmd)
1071 {
1072   return(clear_ioam_rewrite_fn());
1073 }
1074   
1075 VLIB_CLI_COMMAND (ip6_clear_ioam_trace_cmd, static) = {
1076   .path = "clear ioam rewrite",
1077   .short_help = "clear ioam rewrite",
1078   .function = clear_ioam_rewrite_command_fn,
1079 };
1080
1081 clib_error_t *
1082 ip6_ioam_trace_profile_set(u32 trace_option_elts, u32 trace_type, u32 node_id,
1083                            u32 app_data, int has_pow_option, u32 trace_tsp, 
1084                            int has_ppc_option)
1085 {
1086   int rv;
1087   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
1088   rv = ip6_ioam_set_rewrite (&hm->rewrite, trace_type, trace_option_elts,
1089                              has_pow_option, has_ppc_option);
1090
1091   switch (rv)
1092     {
1093     case 0:
1094       hm->node_id = node_id;
1095       hm->app_data = app_data;
1096       hm->trace_type = trace_type;
1097       hm->trace_option_elts = trace_option_elts;
1098       hm->has_pow_option = has_pow_option;
1099       hm->has_ppc_option = has_ppc_option;
1100       hm->trace_tsp = trace_tsp;
1101       break;
1102
1103     default:
1104       return clib_error_return_code(0, rv, 0, "ip6_ioam_set_rewrite returned %d", rv);
1105     }
1106
1107   return 0;
1108 }
1109
1110
1111 static clib_error_t *
1112 ip6_set_ioam_rewrite_command_fn (vlib_main_t * vm,
1113                                  unformat_input_t * input,
1114                                  vlib_cli_command_t * cmd)
1115 {
1116   u32 trace_option_elts = 0;
1117   u32 trace_type = 0, node_id = 0; 
1118   u32 app_data = 0, trace_tsp = TSP_MICROSECONDS;
1119   int has_pow_option = 0;
1120   int has_ppc_option = 0;
1121   clib_error_t * rv = 0;
1122   
1123   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1124     {
1125       if (unformat (input, "trace-type 0x%x trace-elts %d "
1126                            "trace-tsp %d node-id 0x%x app-data 0x%x", 
1127                       &trace_type, &trace_option_elts, &trace_tsp,
1128                       &node_id, &app_data))
1129             ;
1130       else if (unformat (input, "pow"))
1131         has_pow_option = 1;
1132       else if (unformat (input, "ppc encap"))
1133         has_ppc_option = PPC_ENCAP;
1134       else if (unformat (input, "ppc decap"))
1135         has_ppc_option = PPC_DECAP;
1136       else if (unformat (input, "ppc none"))
1137         has_ppc_option = PPC_NONE;
1138       else
1139         break;
1140     }
1141   
1142     
1143     rv = ip6_ioam_trace_profile_set(trace_option_elts, trace_type, node_id,
1144                            app_data, has_pow_option, trace_tsp, has_ppc_option);
1145
1146     return rv;
1147 }
1148
1149
1150 VLIB_CLI_COMMAND (ip6_set_ioam_rewrite_cmd, static) = {
1151   .path = "set ioam rewrite",
1152   .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> [pow] [ppc <encap|decap>]",
1153   .function = ip6_set_ioam_rewrite_command_fn,
1154 };
1155   
1156 static clib_error_t *
1157 ip6_show_ioam_summary_cmd_fn (vlib_main_t * vm,
1158                       unformat_input_t * input,
1159                       vlib_cli_command_t * cmd)
1160 {
1161   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
1162   u8 *s = 0;
1163
1164
1165   if (!is_zero_ip6_address(&hm->adj))
1166   {
1167   s = format(s, "              REWRITE FLOW CONFIGS - \n");
1168   s = format(s, "               Destination Address : %U\n",
1169             format_ip6_address, &hm->adj, sizeof(ip6_address_t));
1170   s = format(s, "                    Flow operation : %d (%s)\n", hm->ioam_flag,
1171            (hm->ioam_flag == IOAM_HBYH_ADD) ? "Add" : 
1172           ((hm->ioam_flag == IOAM_HBYH_MOD) ? "Mod" : "Pop"));
1173   } 
1174   else 
1175   {
1176   s = format(s, "              REWRITE FLOW CONFIGS - Not configured\n");
1177   }
1178
1179   if (hm->trace_option_elts)
1180   {
1181   s = format(s, " HOP BY HOP OPTIONS - TRACE CONFIG - \n");
1182   s = format(s, "                        Trace Type : 0x%x (%d)\n", 
1183           hm->trace_type, hm->trace_type);
1184   s = format(s, "         Trace timestamp precision : %d (%s)\n", hm->trace_tsp,
1185        (hm->trace_tsp == TSP_SECONDS) ? "Seconds" : 
1186       ((hm->trace_tsp == TSP_MILLISECONDS) ? "Milliseconds" : 
1187      (((hm->trace_tsp == TSP_MICROSECONDS) ? "Microseconds" : "Nanoseconds"))));
1188   s = format(s, "                Num of trace nodes : %d\n", 
1189           hm->trace_option_elts);
1190   s = format(s, "                           Node-id : 0x%x (%d)\n", 
1191           hm->node_id, hm->node_id);
1192   s = format(s, "                          App Data : 0x%x (%d)\n", 
1193           hm->app_data, hm->app_data);
1194   }
1195   else
1196   {
1197   s = format(s, " HOP BY HOP OPTIONS - TRACE CONFIG - Not configured\n");
1198   }
1199
1200   s = format(s, "                        POW OPTION - %d (%s)\n", 
1201           hm->has_pow_option, (hm->has_pow_option?"Enabled":"Disabled"));
1202   if (hm->has_pow_option)
1203     s = format(s, "Try 'show ioam sc-profile' for more information\n");
1204
1205   s = format(s, "         EDGE TO EDGE - PPC OPTION - %d (%s)\n", 
1206          hm->has_ppc_option, ppc_state[hm->has_ppc_option]);
1207   if (hm->has_ppc_option)
1208     s = format(s, "Try 'show ioam ppc' for more information\n");
1209
1210   vlib_cli_output(vm, "%v", s);
1211   vec_free(s);
1212   return 0;
1213 }
1214
1215 VLIB_CLI_COMMAND (ip6_show_ioam_run_cmd, static) = {
1216   .path = "show ioam summary",
1217   .short_help = "Summary of IOAM configuration",
1218   .function = ip6_show_ioam_summary_cmd_fn,
1219 };
1220
1221 int ip6_ioam_set_destination (ip6_address_t *addr, u32 mask_width, u32 vrf_id,
1222                               int is_add, int is_pop, int is_none)
1223 {
1224   ip6_main_t * im = &ip6_main;
1225   ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
1226   ip_lookup_main_t * lm = &im->lookup_main;
1227   ip_adjacency_t * adj;
1228   u32 fib_index;
1229   u32 len, adj_index;
1230   int i, rv;
1231   uword * p;
1232   BVT(clib_bihash_kv) kv, value;
1233
1234   if ((is_add + is_pop + is_none) != 1)
1235     return VNET_API_ERROR_INVALID_VALUE_2;
1236
1237   /* Go find the adjacency we're supposed to tickle */
1238   p = hash_get (im->fib_index_by_table_id, vrf_id);
1239
1240   if (p == 0)
1241     return VNET_API_ERROR_NO_SUCH_FIB;
1242
1243   fib_index = p[0];
1244
1245   len = vec_len (im->prefix_lengths_in_search_order);
1246   
1247   for (i = 0; i < len; i++)
1248     {
1249       int dst_address_length = im->prefix_lengths_in_search_order[i];
1250       ip6_address_t * mask = &im->fib_masks[dst_address_length];
1251       
1252       if (dst_address_length != mask_width)
1253         continue;
1254
1255       kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
1256       kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
1257       kv.key[2] = ((u64)((fib_index))<<32) | dst_address_length;
1258       
1259       rv = BV(clib_bihash_search_inline_2)(&im->ip6_lookup_table, &kv, &value);
1260       if (rv == 0)
1261         goto found;
1262
1263     }
1264   return VNET_API_ERROR_NO_SUCH_ENTRY;
1265   
1266  found:
1267
1268   /* Got it, modify as directed... */
1269   adj_index = value.value;
1270   adj = ip_get_adjacency (lm, adj_index);
1271
1272   /* Restore original lookup-next action */
1273   if (adj->saved_lookup_next_index)
1274     {
1275       adj->lookup_next_index = adj->saved_lookup_next_index;
1276       adj->saved_lookup_next_index = 0;
1277     }
1278
1279   /* Save current action */
1280   if (is_add || is_pop)
1281     adj->saved_lookup_next_index = adj->lookup_next_index;
1282
1283   if (is_add)
1284     adj->lookup_next_index = IP_LOOKUP_NEXT_ADD_HOP_BY_HOP;
1285
1286   if (is_pop)
1287     adj->lookup_next_index = IP_LOOKUP_NEXT_POP_HOP_BY_HOP;
1288
1289   hm->adj = *addr;
1290   hm->ioam_flag = (is_add ? IOAM_HBYH_ADD :
1291                   (is_pop ? IOAM_HBYH_POP : IOAM_HBYH_MOD));
1292   return 0;
1293 }
1294                               
1295 static clib_error_t *
1296 ip6_set_ioam_destination_command_fn (vlib_main_t * vm,
1297                                      unformat_input_t * input,
1298                                      vlib_cli_command_t * cmd)
1299 {
1300   ip6_address_t addr;
1301   u32 mask_width = ~0;
1302   int is_add = 0;
1303   int is_pop = 0;
1304   int is_none = 0;
1305   u32 vrf_id = 0;
1306   int rv;
1307
1308   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1309     {
1310       if (unformat (input, "%U/%d", 
1311                     unformat_ip6_address, &addr, &mask_width))
1312         ;
1313       else if (unformat (input, "vrf-id %d", &vrf_id))
1314         ;
1315       else if (unformat (input, "add"))
1316         is_add = 1;
1317       else if (unformat (input, "pop"))
1318         is_pop = 1;
1319       else if (unformat (input, "none"))
1320         is_none = 1;
1321       else
1322         break;
1323     }
1324
1325   if ((is_add + is_pop + is_none) != 1)
1326     return clib_error_return (0, "One of (add, pop, none) required");
1327   if (mask_width == ~0)
1328     return clib_error_return (0, "<address>/<mask-width> required");
1329
1330   rv = ip6_ioam_set_destination (&addr, mask_width, vrf_id, 
1331                                  is_add, is_pop, is_none);
1332
1333   switch (rv)
1334     {
1335     case 0:
1336       break;
1337     default:
1338       return clib_error_return (0, "ip6_ioam_set_destination returned %d", rv);
1339     }
1340   
1341   return 0;
1342 }
1343
1344 VLIB_CLI_COMMAND (ip6_set_ioam_destination_cmd, static) = {
1345   .path = "set ioam destination",
1346   .short_help = "set ioam destination <ip6-address>/<width> add | pop | none",
1347   .function = ip6_set_ioam_destination_command_fn,
1348 };
1349
1350 void vnet_register_ioam_end_of_path_callback (void *cb)
1351 {
1352   ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
1353
1354   hm->ioam_end_of_path_cb = cb;
1355 }
1356                                              
1357