2a037033d13e4e798c3cc32f645112f5d7eef7d7
[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 char *ppc_state[] = { "None", "Encap", "Decap" };
29
30 ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main;
31
32 #define foreach_ip6_hbyh_ioam_input_next        \
33   _(IP6_REWRITE, "ip6-rewrite")                 \
34   _(IP6_LOOKUP, "ip6-lookup")                   \
35   _(DROP, "error-drop")
36
37 typedef enum
38 {
39 #define _(s,n) IP6_HBYH_IOAM_INPUT_NEXT_##s,
40   foreach_ip6_hbyh_ioam_input_next
41 #undef _
42     IP6_HBYH_IOAM_INPUT_N_NEXT,
43 } ip6_hbyh_ioam_input_next_t;
44
45
46
47 /* The main h-b-h tracer will be invoked, no need to do much here */
48 int
49 ip6_hbh_add_register_option (u8 option,
50                              u8 size,
51                              int rewrite_options (u8 * rewrite_string,
52                                                   u8 * rewrite_size))
53 {
54   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
55
56   ASSERT (option < ARRAY_LEN (hm->add_options));
57
58   /* Already registered */
59   if (hm->add_options[option])
60     return (-1);
61
62   hm->add_options[option] = rewrite_options;
63   hm->options_size[option] = size;
64
65   return (0);
66 }
67
68 int
69 ip6_hbh_add_unregister_option (u8 option)
70 {
71   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
72
73   ASSERT (option < ARRAY_LEN (hm->add_options));
74
75   /* Not registered */
76   if (!hm->add_options[option])
77     return (-1);
78
79   hm->add_options[option] = NULL;
80   hm->options_size[option] = 0;
81   return (0);
82 }
83
84 typedef struct
85 {
86   u32 next_index;
87 } ip6_add_hop_by_hop_trace_t;
88
89 /* packet trace format function */
90 static u8 *
91 format_ip6_add_hop_by_hop_trace (u8 * s, va_list * args)
92 {
93   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
94   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
95   ip6_add_hop_by_hop_trace_t *t = va_arg (*args,
96                                           ip6_add_hop_by_hop_trace_t *);
97
98   s = format (s, "IP6_ADD_HOP_BY_HOP: next index %d", t->next_index);
99   return s;
100 }
101
102 vlib_node_registration_t ip6_add_hop_by_hop_node;
103
104 #define foreach_ip6_add_hop_by_hop_error \
105 _(PROCESSED, "Pkts w/ added ip6 hop-by-hop options")
106
107 typedef enum
108 {
109 #define _(sym,str) IP6_ADD_HOP_BY_HOP_ERROR_##sym,
110   foreach_ip6_add_hop_by_hop_error
111 #undef _
112     IP6_ADD_HOP_BY_HOP_N_ERROR,
113 } ip6_add_hop_by_hop_error_t;
114
115 static char *ip6_add_hop_by_hop_error_strings[] = {
116 #define _(sym,string) string,
117   foreach_ip6_add_hop_by_hop_error
118 #undef _
119 };
120
121 static uword
122 ip6_add_hop_by_hop_node_fn (vlib_main_t * vm,
123                             vlib_node_runtime_t * node, vlib_frame_t * frame)
124 {
125   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
126   u32 n_left_from, *from, *to_next;
127   ip_lookup_next_t next_index;
128   u32 processed = 0;
129   u8 *rewrite = hm->rewrite;
130   u32 rewrite_length = vec_len (rewrite);
131
132   from = vlib_frame_vector_args (frame);
133   n_left_from = frame->n_vectors;
134   next_index = node->cached_next_index;
135
136   while (n_left_from > 0)
137     {
138       u32 n_left_to_next;
139
140       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
141       while (n_left_from >= 4 && n_left_to_next >= 2)
142         {
143           u32 bi0, bi1;
144           vlib_buffer_t *b0, *b1;
145           u32 next0, next1;
146           ip6_header_t *ip0, *ip1;
147           ip6_hop_by_hop_header_t *hbh0, *hbh1;
148           u64 *copy_src0, *copy_dst0, *copy_src1, *copy_dst1;
149           u16 new_l0, new_l1;
150
151           /* Prefetch next iteration. */
152           {
153             vlib_buffer_t *p2, *p3;
154
155             p2 = vlib_get_buffer (vm, from[2]);
156             p3 = vlib_get_buffer (vm, from[3]);
157
158             vlib_prefetch_buffer_header (p2, LOAD);
159             vlib_prefetch_buffer_header (p3, LOAD);
160
161             CLIB_PREFETCH (p2->data - rewrite_length,
162                            2 * CLIB_CACHE_LINE_BYTES, STORE);
163             CLIB_PREFETCH (p3->data - rewrite_length,
164                            2 * CLIB_CACHE_LINE_BYTES, STORE);
165           }
166
167           /* speculatively enqueue b0 and b1 to the current next frame */
168           to_next[0] = bi0 = from[0];
169           to_next[1] = bi1 = from[1];
170           from += 2;
171           to_next += 2;
172           n_left_from -= 2;
173           n_left_to_next -= 2;
174
175           b0 = vlib_get_buffer (vm, bi0);
176           b1 = vlib_get_buffer (vm, bi1);
177
178           /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
179           ASSERT (b0->current_data == 0);
180           ASSERT (b1->current_data == 0);
181
182           ip0 = vlib_buffer_get_current (b0);
183           ip1 = vlib_buffer_get_current (b1);
184
185           /* Copy the ip header left by the required amount */
186           copy_dst0 = (u64 *) (((u8 *) ip0) - rewrite_length);
187           copy_dst1 = (u64 *) (((u8 *) ip1) - rewrite_length);
188           copy_src0 = (u64 *) ip0;
189           copy_src1 = (u64 *) ip1;
190
191           copy_dst0[0] = copy_src0[0];
192           copy_dst0[1] = copy_src0[1];
193           copy_dst0[2] = copy_src0[2];
194           copy_dst0[3] = copy_src0[3];
195           copy_dst0[4] = copy_src0[4];
196
197           copy_dst1[0] = copy_src1[0];
198           copy_dst1[1] = copy_src1[1];
199           copy_dst1[2] = copy_src1[2];
200           copy_dst1[3] = copy_src1[3];
201           copy_dst1[4] = copy_src1[4];
202
203           vlib_buffer_advance (b0, -(word) rewrite_length);
204           vlib_buffer_advance (b1, -(word) rewrite_length);
205           ip0 = vlib_buffer_get_current (b0);
206           ip1 = vlib_buffer_get_current (b1);
207
208           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
209           hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
210           /* $$$ tune, rewrite_length is a multiple of 8 */
211           clib_memcpy (hbh0, rewrite, rewrite_length);
212           clib_memcpy (hbh1, rewrite, rewrite_length);
213           /* Patch the protocol chain, insert the h-b-h (type 0) header */
214           hbh0->protocol = ip0->protocol;
215           hbh1->protocol = ip1->protocol;
216           ip0->protocol = 0;
217           ip1->protocol = 0;
218           new_l0 =
219             clib_net_to_host_u16 (ip0->payload_length) + rewrite_length;
220           new_l1 =
221             clib_net_to_host_u16 (ip1->payload_length) + rewrite_length;
222           ip0->payload_length = clib_host_to_net_u16 (new_l0);
223           ip1->payload_length = clib_host_to_net_u16 (new_l1);
224
225           /* Populate the (first) h-b-h list elt */
226           next0 = IP6_HBYH_IOAM_INPUT_NEXT_IP6_LOOKUP;
227           next1 = IP6_HBYH_IOAM_INPUT_NEXT_IP6_LOOKUP;
228
229
230           /* $$$$$ End of processing 2 x packets $$$$$ */
231
232           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
233             {
234               if (b0->flags & VLIB_BUFFER_IS_TRACED)
235                 {
236                   ip6_add_hop_by_hop_trace_t *t =
237                     vlib_add_trace (vm, node, b0, sizeof (*t));
238                   t->next_index = next0;
239                 }
240               if (b1->flags & VLIB_BUFFER_IS_TRACED)
241                 {
242                   ip6_add_hop_by_hop_trace_t *t =
243                     vlib_add_trace (vm, node, b1, sizeof (*t));
244                   t->next_index = next1;
245                 }
246             }
247           processed += 2;
248           /* verify speculative enqueues, maybe switch current next frame */
249           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
250                                            to_next, n_left_to_next,
251                                            bi0, bi1, next0, next1);
252         }
253       while (n_left_from > 0 && n_left_to_next > 0)
254         {
255           u32 bi0;
256           vlib_buffer_t *b0;
257           u32 next0;
258           ip6_header_t *ip0;
259           ip6_hop_by_hop_header_t *hbh0;
260           u64 *copy_src0, *copy_dst0;
261           u16 new_l0;
262
263           /* speculatively enqueue b0 to the current next frame */
264           bi0 = from[0];
265           to_next[0] = bi0;
266           from += 1;
267           to_next += 1;
268           n_left_from -= 1;
269           n_left_to_next -= 1;
270
271           b0 = vlib_get_buffer (vm, bi0);
272
273           ip0 = vlib_buffer_get_current (b0);
274
275           /* Copy the ip header left by the required amount */
276           copy_dst0 = (u64 *) (((u8 *) ip0) - rewrite_length);
277           copy_src0 = (u64 *) ip0;
278
279           copy_dst0[0] = copy_src0[0];
280           copy_dst0[1] = copy_src0[1];
281           copy_dst0[2] = copy_src0[2];
282           copy_dst0[3] = copy_src0[3];
283           copy_dst0[4] = copy_src0[4];
284           vlib_buffer_advance (b0, -(word) rewrite_length);
285           ip0 = vlib_buffer_get_current (b0);
286
287           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
288           /* $$$ tune, rewrite_length is a multiple of 8 */
289           clib_memcpy (hbh0, rewrite, rewrite_length);
290           /* Patch the protocol chain, insert the h-b-h (type 0) header */
291           hbh0->protocol = ip0->protocol;
292           ip0->protocol = 0;
293           new_l0 =
294             clib_net_to_host_u16 (ip0->payload_length) + rewrite_length;
295           ip0->payload_length = clib_host_to_net_u16 (new_l0);
296
297           /* Populate the (first) h-b-h list elt */
298           next0 = IP6_HBYH_IOAM_INPUT_NEXT_IP6_LOOKUP;
299
300           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
301                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
302             {
303               ip6_add_hop_by_hop_trace_t *t =
304                 vlib_add_trace (vm, node, b0, sizeof (*t));
305               t->next_index = next0;
306             }
307
308           processed++;
309
310           /* verify speculative enqueue, maybe switch current next frame */
311           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
312                                            to_next, n_left_to_next,
313                                            bi0, next0);
314         }
315
316       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
317     }
318
319   vlib_node_increment_counter (vm, ip6_add_hop_by_hop_node.index,
320                                IP6_ADD_HOP_BY_HOP_ERROR_PROCESSED, processed);
321   return frame->n_vectors;
322 }
323
324 VLIB_REGISTER_NODE (ip6_add_hop_by_hop_node) =  /* *INDENT-OFF* */
325 {
326   .function = ip6_add_hop_by_hop_node_fn,.name =
327     "ip6-add-hop-by-hop",.vector_size = sizeof (u32),.format_trace =
328     format_ip6_add_hop_by_hop_trace,.type =
329     VLIB_NODE_TYPE_INTERNAL,.n_errors =
330     ARRAY_LEN (ip6_add_hop_by_hop_error_strings),.error_strings =
331     ip6_add_hop_by_hop_error_strings,
332     /* See ip/lookup.h */
333     .n_next_nodes = IP6_HBYH_IOAM_INPUT_N_NEXT,.next_nodes =
334   {
335 #define _(s,n) [IP6_HBYH_IOAM_INPUT_NEXT_##s] = n,
336     foreach_ip6_hbyh_ioam_input_next
337 #undef _
338   }
339 ,};
340
341 /* *INDENT-ON* */
342
343 VLIB_NODE_FUNCTION_MULTIARCH (ip6_add_hop_by_hop_node,
344                               ip6_add_hop_by_hop_node_fn)
345 /* The main h-b-h tracer was already invoked, no need to do much here */
346      typedef struct
347      {
348        u32 next_index;
349      } ip6_pop_hop_by_hop_trace_t;
350
351 /* packet trace format function */
352      static u8 *format_ip6_pop_hop_by_hop_trace (u8 * s, va_list * args)
353 {
354   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
355   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
356   ip6_pop_hop_by_hop_trace_t *t =
357     va_arg (*args, ip6_pop_hop_by_hop_trace_t *);
358
359   s = format (s, "IP6_POP_HOP_BY_HOP: next index %d", t->next_index);
360   return s;
361 }
362
363 int
364 ip6_hbh_pop_register_option (u8 option,
365                              int options (ip6_header_t * ip,
366                                           ip6_hop_by_hop_option_t * opt))
367 {
368   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
369
370   ASSERT (option < ARRAY_LEN (hm->pop_options));
371
372   /* Already registered */
373   if (hm->pop_options[option])
374     return (-1);
375
376   hm->pop_options[option] = options;
377
378   return (0);
379 }
380
381 int
382 ip6_hbh_pop_unregister_option (u8 option)
383 {
384   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
385
386   ASSERT (option < ARRAY_LEN (hm->pop_options));
387
388   /* Not registered */
389   if (!hm->pop_options[option])
390     return (-1);
391
392   hm->pop_options[option] = NULL;
393   return (0);
394 }
395
396 vlib_node_registration_t ip6_pop_hop_by_hop_node;
397
398 #define foreach_ip6_pop_hop_by_hop_error                \
399 _(PROCESSED, "Pkts w/ removed ip6 hop-by-hop options")  \
400 _(NO_HOHO, "Pkts w/ no ip6 hop-by-hop options")         \
401 _(OPTION_FAILED, "ip6 pop hop-by-hop failed to process")
402
403 typedef enum
404 {
405 #define _(sym,str) IP6_POP_HOP_BY_HOP_ERROR_##sym,
406   foreach_ip6_pop_hop_by_hop_error
407 #undef _
408     IP6_POP_HOP_BY_HOP_N_ERROR,
409 } ip6_pop_hop_by_hop_error_t;
410
411 static char *ip6_pop_hop_by_hop_error_strings[] = {
412 #define _(sym,string) string,
413   foreach_ip6_pop_hop_by_hop_error
414 #undef _
415 };
416
417 static inline void
418 ioam_pop_hop_by_hop_processing (vlib_main_t * vm,
419                                 ip6_header_t * ip0,
420                                 ip6_hop_by_hop_header_t * hbh0)
421 {
422   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
423   ip6_hop_by_hop_option_t *opt0, *limit0;
424   u8 type0;
425
426   if (!hbh0 || !ip0)
427     return;
428
429   opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
430   limit0 = (ip6_hop_by_hop_option_t *)
431     ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
432
433   /* Scan the set of h-b-h options, process ones that we understand */
434   while (opt0 < limit0)
435     {
436       type0 = opt0->type;
437       switch (type0)
438         {
439         case 0:         /* Pad1 */
440           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
441           continue;
442         case 1:         /* PadN */
443           break;
444         default:
445           if (hm->pop_options[type0])
446             {
447               if ((*hm->pop_options[type0]) (ip0, opt0) < 0)
448                 {
449                   vlib_node_increment_counter (vm,
450                                                ip6_pop_hop_by_hop_node.index,
451                                                IP6_POP_HOP_BY_HOP_ERROR_OPTION_FAILED,
452                                                1);
453                 }
454             }
455         }
456       opt0 =
457         (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
458                                      sizeof (ip6_hop_by_hop_option_t));
459     }
460 }
461
462 static uword
463 ip6_pop_hop_by_hop_node_fn (vlib_main_t * vm,
464                             vlib_node_runtime_t * node, vlib_frame_t * frame)
465 {
466   ip6_main_t *im = &ip6_main;
467   ip_lookup_main_t *lm = &im->lookup_main;
468   u32 n_left_from, *from, *to_next;
469   ip_lookup_next_t next_index;
470   u32 processed = 0;
471   u32 no_header = 0;
472
473   from = vlib_frame_vector_args (frame);
474   n_left_from = frame->n_vectors;
475   next_index = node->cached_next_index;
476
477   while (n_left_from > 0)
478     {
479       u32 n_left_to_next;
480
481       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
482
483       while (n_left_from >= 4 && n_left_to_next >= 2)
484         {
485           u32 bi0, bi1;
486           vlib_buffer_t *b0, *b1;
487           u32 next0, next1;
488           u32 adj_index0, adj_index1;
489           ip6_header_t *ip0, *ip1;
490           ip_adjacency_t *adj0, *adj1;
491           ip6_hop_by_hop_header_t *hbh0, *hbh1;
492           u64 *copy_dst0, *copy_src0, *copy_dst1, *copy_src1;
493           u16 new_l0, new_l1;
494
495           /* Prefetch next iteration. */
496           {
497             vlib_buffer_t *p2, *p3;
498
499             p2 = vlib_get_buffer (vm, from[2]);
500             p3 = vlib_get_buffer (vm, from[3]);
501
502             vlib_prefetch_buffer_header (p2, LOAD);
503             vlib_prefetch_buffer_header (p3, LOAD);
504
505             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
506             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
507           }
508
509           /* speculatively enqueue b0 and b1 to the current next frame */
510           to_next[0] = bi0 = from[0];
511           to_next[1] = bi1 = from[1];
512           from += 2;
513           to_next += 2;
514           n_left_from -= 2;
515           n_left_to_next -= 2;
516
517           b0 = vlib_get_buffer (vm, bi0);
518           b1 = vlib_get_buffer (vm, bi1);
519
520           /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
521           ASSERT (b0->current_data == 0);
522           ASSERT (b1->current_data == 0);
523
524           ip0 = vlib_buffer_get_current (b0);
525           ip1 = vlib_buffer_get_current (b1);
526           adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
527           adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
528           adj0 = ip_get_adjacency (lm, adj_index0);
529           adj1 = ip_get_adjacency (lm, adj_index1);
530
531           next0 = adj0->lookup_next_index;
532           next1 = adj1->lookup_next_index;
533
534           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
535           hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
536
537           ioam_pop_hop_by_hop_processing (vm, ip0, hbh0);
538           ioam_pop_hop_by_hop_processing (vm, ip1, hbh1);
539
540           vlib_buffer_advance (b0, (hbh0->length + 1) << 3);
541           vlib_buffer_advance (b1, (hbh1->length + 1) << 3);
542
543           new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
544             ((hbh0->length + 1) << 3);
545           new_l1 = clib_net_to_host_u16 (ip1->payload_length) -
546             ((hbh1->length + 1) << 3);
547
548           ip0->payload_length = clib_host_to_net_u16 (new_l0);
549           ip1->payload_length = clib_host_to_net_u16 (new_l1);
550
551           ip0->protocol = hbh0->protocol;
552           ip1->protocol = hbh1->protocol;
553
554           copy_src0 = (u64 *) ip0;
555           copy_src1 = (u64 *) ip1;
556           copy_dst0 = copy_src0 + (hbh0->length + 1);
557           copy_dst0[4] = copy_src0[4];
558           copy_dst0[3] = copy_src0[3];
559           copy_dst0[2] = copy_src0[2];
560           copy_dst0[1] = copy_src0[1];
561           copy_dst0[0] = copy_src0[0];
562           copy_dst1 = copy_src1 + (hbh1->length + 1);
563           copy_dst1[4] = copy_src1[4];
564           copy_dst1[3] = copy_src1[3];
565           copy_dst1[2] = copy_src1[2];
566           copy_dst1[1] = copy_src1[1];
567           copy_dst1[0] = copy_src1[0];
568           processed += 2;
569           /* $$$$$ End of processing 2 x packets $$$$$ */
570
571           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
572             {
573               if (b0->flags & VLIB_BUFFER_IS_TRACED)
574                 {
575                   ip6_pop_hop_by_hop_trace_t *t =
576                     vlib_add_trace (vm, node, b0, sizeof (*t));
577                   t->next_index = next0;
578                 }
579               if (b1->flags & VLIB_BUFFER_IS_TRACED)
580                 {
581                   ip6_pop_hop_by_hop_trace_t *t =
582                     vlib_add_trace (vm, node, b1, sizeof (*t));
583                   t->next_index = next1;
584                 }
585             }
586
587           /* verify speculative enqueues, maybe switch current next frame */
588           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
589                                            to_next, n_left_to_next,
590                                            bi0, bi1, next0, next1);
591         }
592
593       while (n_left_from > 0 && n_left_to_next > 0)
594         {
595           u32 bi0;
596           vlib_buffer_t *b0;
597           u32 next0;
598           u32 adj_index0;
599           ip6_header_t *ip0;
600           ip_adjacency_t *adj0;
601           ip6_hop_by_hop_header_t *hbh0;
602           u64 *copy_dst0, *copy_src0;
603           u16 new_l0;
604
605           /* speculatively enqueue b0 to the current next frame */
606           bi0 = from[0];
607           to_next[0] = bi0;
608           from += 1;
609           to_next += 1;
610           n_left_from -= 1;
611           n_left_to_next -= 1;
612
613           b0 = vlib_get_buffer (vm, bi0);
614
615           ip0 = vlib_buffer_get_current (b0);
616           adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
617           adj0 = ip_get_adjacency (lm, adj_index0);
618
619           /* Default use the next_index from the adjacency. */
620           next0 = adj0->lookup_next_index;
621
622           /* Perfectly normal to end up here w/ out h-b-h header */
623           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
624
625           /* TODO:Temporarily doing it here.. do this validation in end_of_path_cb */
626           ioam_pop_hop_by_hop_processing (vm, ip0, hbh0);
627           /* Pop the trace data */
628           vlib_buffer_advance (b0, (hbh0->length + 1) << 3);
629           new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
630             ((hbh0->length + 1) << 3);
631           ip0->payload_length = clib_host_to_net_u16 (new_l0);
632           ip0->protocol = hbh0->protocol;
633           copy_src0 = (u64 *) ip0;
634           copy_dst0 = copy_src0 + (hbh0->length + 1);
635           copy_dst0[4] = copy_src0[4];
636           copy_dst0[3] = copy_src0[3];
637           copy_dst0[2] = copy_src0[2];
638           copy_dst0[1] = copy_src0[1];
639           copy_dst0[0] = copy_src0[0];
640           processed++;
641
642           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
643                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
644             {
645               ip6_pop_hop_by_hop_trace_t *t =
646                 vlib_add_trace (vm, node, b0, sizeof (*t));
647               t->next_index = next0;
648             }
649
650           /* verify speculative enqueue, maybe switch current next frame */
651           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
652                                            to_next, n_left_to_next,
653                                            bi0, next0);
654         }
655
656       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
657     }
658
659   vlib_node_increment_counter (vm, ip6_pop_hop_by_hop_node.index,
660                                IP6_POP_HOP_BY_HOP_ERROR_PROCESSED, processed);
661   vlib_node_increment_counter (vm, ip6_pop_hop_by_hop_node.index,
662                                IP6_POP_HOP_BY_HOP_ERROR_NO_HOHO, no_header);
663   return frame->n_vectors;
664 }
665
666 /* *INDENT-OFF* */
667 VLIB_REGISTER_NODE (ip6_pop_hop_by_hop_node) =
668 {
669   .function = ip6_pop_hop_by_hop_node_fn,.name =
670     "ip6-pop-hop-by-hop",.vector_size = sizeof (u32),.format_trace =
671     format_ip6_pop_hop_by_hop_trace,.type =
672     VLIB_NODE_TYPE_INTERNAL,.sibling_of = "ip6-lookup",.n_errors =
673     ARRAY_LEN (ip6_pop_hop_by_hop_error_strings),.error_strings =
674     ip6_pop_hop_by_hop_error_strings,
675     /* See ip/lookup.h */
676 .n_next_nodes = 0,};
677
678 /* *INDENT-ON* */
679
680 VLIB_NODE_FUNCTION_MULTIARCH (ip6_pop_hop_by_hop_node,
681                               ip6_pop_hop_by_hop_node_fn)
682      static clib_error_t *ip6_hop_by_hop_ioam_init (vlib_main_t * vm)
683 {
684   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
685
686   hm->vlib_main = vm;
687   hm->vnet_main = vnet_get_main ();
688   hm->unix_time_0 = (u32) time (0);     /* Store starting time */
689   hm->vlib_time_0 = vlib_time_now (vm);
690   hm->ioam_flag = IOAM_HBYH_MOD;
691   memset (hm->add_options, 0, sizeof (hm->add_options));
692   memset (hm->pop_options, 0, sizeof (hm->pop_options));
693   memset (hm->options_size, 0, sizeof (hm->options_size));
694
695   return (0);
696 }
697
698 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_init);
699
700 int
701 ip6_ioam_set_rewrite (u8 ** rwp, int has_trace_option,
702                       int has_pot_option, int has_ppc_option)
703 {
704   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
705   u8 *rewrite = 0;
706   u32 size, rnd_size;
707   ip6_hop_by_hop_header_t *hbh;
708   u8 *current;
709   u8 trace_data_size = 0;
710   u8 pot_data_size = 0;
711
712   vec_free (*rwp);
713
714   if (has_trace_option == 0 && has_pot_option == 0)
715     return -1;
716
717   /* Work out how much space we need */
718   size = sizeof (ip6_hop_by_hop_header_t);
719
720   //if (has_trace_option && hm->get_sizeof_options[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] != 0)
721   if (has_trace_option
722       && hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] != 0)
723     {
724       size += sizeof (ip6_hop_by_hop_option_t);
725       size += hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST];
726     }
727   if (has_pot_option
728       && hm->add_options[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT] != 0)
729     {
730       size += sizeof (ip6_hop_by_hop_option_t);
731       size += hm->options_size[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT];
732     }
733
734   /* Round to a multiple of 8 octets */
735   rnd_size = (size + 7) & ~7;
736
737   /* allocate it, zero-fill / pad by construction */
738   vec_validate (rewrite, rnd_size - 1);
739
740   hbh = (ip6_hop_by_hop_header_t *) rewrite;
741   /* Length of header in 8 octet units, not incl first 8 octets */
742   hbh->length = (rnd_size >> 3) - 1;
743   current = (u8 *) (hbh + 1);
744
745   if (has_trace_option
746       && hm->add_options[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] != 0)
747     {
748       if (0 != (hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST]))
749         {
750           trace_data_size =
751             hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST];
752           if (0 ==
753               hm->add_options[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] (current,
754                                                                      &trace_data_size))
755             current += hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST];
756         }
757     }
758   if (has_pot_option
759       && hm->add_options[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT] != 0)
760     {
761       pot_data_size = hm->options_size[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT];
762       if (0 ==
763           hm->add_options[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT] (current,
764                                                                   &pot_data_size))
765         current +=
766           sizeof (hm->options_size[HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT]);
767     }
768
769   *rwp = rewrite;
770   return 0;
771 }
772
773 clib_error_t *
774 clear_ioam_rewrite_fn (void)
775 {
776   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
777
778   vec_free (hm->rewrite);
779   hm->rewrite = 0;
780   hm->has_trace_option = 0;
781   hm->has_pot_option = 0;
782   hm->has_ppc_option = 0;
783
784   return 0;
785 }
786
787 clib_error_t *
788 clear_ioam_rewrite_command_fn (vlib_main_t * vm,
789                                unformat_input_t * input,
790                                vlib_cli_command_t * cmd)
791 {
792   return (clear_ioam_rewrite_fn ());
793 }
794
795 /* *INDENT-OFF* */
796 VLIB_CLI_COMMAND (ip6_clear_ioam_rewrite_cmd, static) =
797 {
798 .path = "clear ioam rewrite",.short_help = "clear ioam rewrite",.function =
799     clear_ioam_rewrite_command_fn,};
800 /* *INDENT-ON* */
801
802 clib_error_t *
803 ip6_ioam_enable (int has_trace_option, int has_pot_option, int has_ppc_option)
804 {
805   int rv;
806   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
807   rv = ip6_ioam_set_rewrite (&hm->rewrite, has_trace_option,
808                              has_pot_option, has_ppc_option);
809
810   switch (rv)
811     {
812     case 0:
813       hm->has_trace_option = has_trace_option;
814       hm->has_pot_option = has_pot_option;
815       hm->has_ppc_option = has_ppc_option;
816       break;
817
818     default:
819       return clib_error_return_code (0, rv, 0,
820                                      "ip6_ioam_set_rewrite returned %d", rv);
821     }
822
823   return 0;
824 }
825
826
827 static clib_error_t *
828 ip6_set_ioam_rewrite_command_fn (vlib_main_t * vm,
829                                  unformat_input_t * input,
830                                  vlib_cli_command_t * cmd)
831 {
832   int has_trace_option = 0;
833   int has_pot_option = 0;
834   int has_ppc_option = 0;
835   clib_error_t *rv = 0;
836
837   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
838     {
839       if (unformat (input, "trace"))
840         has_trace_option = 1;
841       else if (unformat (input, "pot"))
842         has_pot_option = 1;
843       else if (unformat (input, "ppc encap"))
844         has_ppc_option = PPC_ENCAP;
845       else if (unformat (input, "ppc decap"))
846         has_ppc_option = PPC_DECAP;
847       else if (unformat (input, "ppc none"))
848         has_ppc_option = PPC_NONE;
849       else
850         break;
851     }
852
853
854   rv = ip6_ioam_enable (has_trace_option, has_pot_option, has_ppc_option);
855
856   return rv;
857 }
858
859 /* *INDENT-OFF* */
860 VLIB_CLI_COMMAND (ip6_set_ioam_rewrite_cmd, static) =
861 {
862 .path = "set ioam rewrite",.short_help =
863     "set ioam [trace] [pot] [ppc <encap|decap>]",.function =
864     ip6_set_ioam_rewrite_command_fn,};
865 /* *INDENT-ON* */
866
867 static clib_error_t *
868 ip6_show_ioam_summary_cmd_fn (vlib_main_t * vm,
869                               unformat_input_t * input,
870                               vlib_cli_command_t * cmd)
871 {
872   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
873   u8 *s = 0;
874
875
876   if (!is_zero_ip6_address (&hm->adj))
877     {
878       s = format (s, "              REWRITE FLOW CONFIGS - \n");
879       s = format (s, "               Destination Address : %U\n",
880                   format_ip6_address, &hm->adj, sizeof (ip6_address_t));
881       s =
882         format (s, "                    Flow operation : %d (%s)\n",
883                 hm->ioam_flag,
884                 (hm->ioam_flag ==
885                  IOAM_HBYH_ADD) ? "Add" : ((hm->ioam_flag ==
886                                             IOAM_HBYH_MOD) ? "Mod" : "Pop"));
887     }
888   else
889     {
890       s = format (s, "              REWRITE FLOW CONFIGS - Not configured\n");
891     }
892
893
894   s = format (s, "                        TRACE OPTION - %d (%s)\n",
895               hm->has_trace_option,
896               (hm->has_trace_option ? "Enabled" : "Disabled"));
897   if (hm->has_trace_option)
898     s =
899       format (s,
900               "Try 'show ioam trace and show ioam-trace profile' for more information\n");
901
902
903   s = format (s, "                        POT OPTION - %d (%s)\n",
904               hm->has_pot_option,
905               (hm->has_pot_option ? "Enabled" : "Disabled"));
906   if (hm->has_pot_option)
907     s =
908       format (s,
909               "Try 'show ioam pot and show pot profile' for more information\n");
910
911   s = format (s, "         EDGE TO EDGE - PPC OPTION - %d (%s)\n",
912               hm->has_ppc_option, ppc_state[hm->has_ppc_option]);
913   if (hm->has_ppc_option)
914     s = format (s, "Try 'show ioam ppc' for more information\n");
915
916   vlib_cli_output (vm, "%v", s);
917   vec_free (s);
918   return 0;
919 }
920
921 /* *INDENT-OFF* */
922 VLIB_CLI_COMMAND (ip6_show_ioam_run_cmd, static) =
923 {
924 .path = "show ioam summary",.short_help =
925     "Summary of IOAM configuration",.function =
926     ip6_show_ioam_summary_cmd_fn,};
927 /* *INDENT-ON* */
928
929 int
930 ip6_ioam_set_destination (ip6_address_t * addr, u32 mask_width, u32 vrf_id,
931                           int is_add, int is_pop, int is_none)
932 {
933   ip6_main_t *im = &ip6_main;
934   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
935   ip_lookup_main_t *lm = &im->lookup_main;
936   ip_adjacency_t *adj;
937   u32 fib_index;
938   u32 len, adj_index;
939   int i, rv;
940   uword *p;
941   BVT (clib_bihash_kv) kv, value;
942
943   if ((is_add + is_pop + is_none) != 1)
944     return VNET_API_ERROR_INVALID_VALUE_2;
945
946   /* Go find the adjacency we're supposed to tickle */
947   p = hash_get (im->fib_index_by_table_id, vrf_id);
948
949   if (p == 0)
950     return VNET_API_ERROR_NO_SUCH_FIB;
951
952   fib_index = p[0];
953
954   len = vec_len (im->prefix_lengths_in_search_order);
955
956   for (i = 0; i < len; i++)
957     {
958       int dst_address_length = im->prefix_lengths_in_search_order[i];
959       ip6_address_t *mask = &im->fib_masks[dst_address_length];
960
961       if (dst_address_length != mask_width)
962         continue;
963
964       kv.key[0] = addr->as_u64[0] & mask->as_u64[0];
965       kv.key[1] = addr->as_u64[1] & mask->as_u64[1];
966       kv.key[2] = ((u64) ((fib_index)) << 32) | dst_address_length;
967
968       rv =
969         BV (clib_bihash_search_inline_2) (&im->ip6_lookup_table, &kv, &value);
970       if (rv == 0)
971         goto found;
972
973     }
974   return VNET_API_ERROR_NO_SUCH_ENTRY;
975
976 found:
977
978   /* Got it, modify as directed... */
979   adj_index = value.value;
980   adj = ip_get_adjacency (lm, adj_index);
981
982   /* Restore original lookup-next action */
983   if (adj->saved_lookup_next_index)
984     {
985       adj->lookup_next_index = adj->saved_lookup_next_index;
986       adj->saved_lookup_next_index = 0;
987     }
988
989   /* Save current action */
990   if (is_add || is_pop)
991     adj->saved_lookup_next_index = adj->lookup_next_index;
992
993   if (is_add)
994     adj->lookup_next_index =
995       (ip_lookup_next_t) IP6_LOOKUP_NEXT_ADD_HOP_BY_HOP;
996
997   if (is_pop)
998     adj->lookup_next_index =
999       (ip_lookup_next_t) IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
1000
1001   hm->adj = *addr;
1002   hm->ioam_flag = (is_add ? IOAM_HBYH_ADD :
1003                    (is_pop ? IOAM_HBYH_POP : IOAM_HBYH_MOD));
1004   return 0;
1005 }
1006
1007 static clib_error_t *
1008 ip6_set_ioam_destination_command_fn (vlib_main_t * vm,
1009                                      unformat_input_t * input,
1010                                      vlib_cli_command_t * cmd)
1011 {
1012   ip6_address_t addr;
1013   u32 mask_width = ~0;
1014   int is_add = 0;
1015   int is_pop = 0;
1016   int is_none = 0;
1017   u32 vrf_id = 0;
1018   int rv;
1019
1020   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1021     {
1022       if (unformat (input, "%U/%d", unformat_ip6_address, &addr, &mask_width))
1023         ;
1024       else if (unformat (input, "vrf-id %d", &vrf_id))
1025         ;
1026       else if (unformat (input, "add"))
1027         is_add = 1;
1028       else if (unformat (input, "pop"))
1029         is_pop = 1;
1030       else if (unformat (input, "none"))
1031         is_none = 1;
1032       else
1033         break;
1034     }
1035
1036   if ((is_add + is_pop + is_none) != 1)
1037     return clib_error_return (0, "One of (add, pop, none) required");
1038   if (mask_width == ~0)
1039     return clib_error_return (0, "<address>/<mask-width> required");
1040
1041   rv = ip6_ioam_set_destination (&addr, mask_width, vrf_id,
1042                                  is_add, is_pop, is_none);
1043
1044   switch (rv)
1045     {
1046     case 0:
1047       break;
1048     default:
1049       return clib_error_return (0, "ip6_ioam_set_destination returned %d",
1050                                 rv);
1051     }
1052
1053   return 0;
1054 }
1055
1056 /* *INDENT-OFF* */
1057 VLIB_CLI_COMMAND (ip6_set_ioam_destination_cmd, static) =
1058 {
1059 .path = "set ioam destination",.short_help =
1060     "set ioam destination <ip6-address>/<width> add | pop | none",.function
1061     = ip6_set_ioam_destination_command_fn,};
1062 /* *INDENT-ON* */
1063
1064
1065 void
1066 vnet_register_ioam_end_of_path_callback (void *cb)
1067 {
1068   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
1069
1070   hm->ioam_end_of_path_cb = cb;
1071 }
1072
1073 /*
1074  * fd.io coding-style-patch-verification: ON
1075  *
1076  * Local Variables:
1077  * eval: (c-set-style "gnu")
1078  * End:
1079  */