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