3df9871e5f636af9ee3f2c32246e36434f410e63
[vpp.git] / src / plugins / ioam / ip6 / ioam_cache_tunnel_select_node.c
1 /*
2  * Copyright (c) 2017 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 /*
16  * ioam_cache_tunnel_select_node.c
17  * This file implements anycast server selection using ioam data
18  * attached to anycast service selection.
19  * Anycast service is reachable via multiple servers reachable
20  * over SR tunnels.
21  * Works with TCP Anycast application.
22  * Cache entry is created when TCP SYN is received for anycast destination.
23  * Response TCP SYN ACKs for anycast service is compared and selected
24  * response is forwarded.
25  * The functionality is introduced via graph nodes that are hooked into
26  * vnet graph via classifier configs like below:
27  *
28  * Enable anycast service selection:
29  * set ioam ip6 sr-tunnel-select oneway
30  *
31  * Enable following classifier on the anycast service client facing interface
32  * e.g. anycast service is db06::06 then:
33  * classify session acl-hit-next ip6-node ip6-add-syn-hop-by-hop table-index 0 match l3
34  * ip6 dst db06::06 ioam-encap anycast
35  *
36  * Enable following classifier on the interfaces facing the server of anycast service:
37  * classify session acl-hit-next ip6-node ip6-lookup table-index 0 match l3
38  *            ip6 src db06::06 ioam-decap anycast
39  *
40  */
41 #include <vlib/vlib.h>
42 #include <vnet/vnet.h>
43 #include <vnet/pg/pg.h>
44 #include <vppinfra/error.h>
45 #include <vnet/ip/ip.h>
46 #include <vnet/sr/sr.h>
47 #include <ioam/ip6/ioam_cache.h>
48 #include <vnet/ip/ip6_hop_by_hop.h>
49 #include <vnet/ip/ip6_hop_by_hop_packet.h>
50
51 typedef struct
52 {
53   u32 next_index;
54   u32 flow_label;
55 } cache_ts_trace_t;
56
57 /* packet trace format function */
58 static u8 *
59 format_cache_ts_trace (u8 * s, va_list * args)
60 {
61   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
62   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
63   cache_ts_trace_t *t = va_arg (*args, cache_ts_trace_t *);
64
65   s = format (s, "CACHE: flow_label %d, next index %d",
66               t->flow_label, t->next_index);
67   return s;
68 }
69
70 vlib_node_registration_t ioam_cache_ts_node;
71
72 #define foreach_cache_ts_error \
73 _(RECORDED, "ip6 iOAM headers cached")
74
75 typedef enum
76 {
77 #define _(sym,str) CACHE_TS_ERROR_##sym,
78   foreach_cache_ts_error
79 #undef _
80     CACHE_TS_N_ERROR,
81 } cache_ts_error_t;
82
83 static char *cache_ts_error_strings[] = {
84 #define _(sym,string) string,
85   foreach_cache_ts_error
86 #undef _
87 };
88
89 typedef enum
90 {
91   IOAM_CACHE_TS_NEXT_POP_HBYH,
92   IOAM_CACHE_TS_ERROR_NEXT_DROP,
93   IOAM_CACHE_TS_N_NEXT,
94 } cache_ts_next_t;
95
96 static uword
97 ip6_ioam_cache_ts_node_fn (vlib_main_t * vm,
98                            vlib_node_runtime_t * node, vlib_frame_t * frame)
99 {
100   ioam_cache_main_t *cm = &ioam_cache_main;
101   u32 n_left_from, *from, *to_next;
102   cache_ts_next_t next_index;
103   u32 recorded = 0;
104
105   from = vlib_frame_vector_args (frame);
106   n_left_from = frame->n_vectors;
107   next_index = node->cached_next_index;
108
109   while (n_left_from > 0)
110     {
111       u32 n_left_to_next;
112
113       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
114       // TODO: dual loop
115       while (n_left_from > 0 && n_left_to_next > 0)
116         {
117           u32 bi0;
118           vlib_buffer_t *p0;
119           u32 next0 = IOAM_CACHE_TS_NEXT_POP_HBYH;
120           ip6_header_t *ip0;
121           ip6_hop_by_hop_header_t *hbh0, *hbh_cmp;
122           tcp_header_t *tcp0;
123           u32 tcp_offset0;
124           u32 cache_ts_index = 0;
125           u8 cache_thread_id = 0;
126           int result = 0;
127           int skip = 0;
128
129           bi0 = from[0];
130           from += 1;
131           n_left_from -= 1;
132
133           p0 = vlib_get_buffer (vm, bi0);
134           ip0 = vlib_buffer_get_current (p0);
135           if (IP_PROTOCOL_TCP ==
136               ip6_locate_header (p0, ip0, IP_PROTOCOL_TCP, &tcp_offset0))
137             {
138               tcp0 = (tcp_header_t *) ((u8 *) ip0 + tcp_offset0);
139               if ((tcp0->flags & TCP_FLAG_SYN) == TCP_FLAG_SYN &&
140                   (tcp0->flags & TCP_FLAG_ACK) == TCP_FLAG_ACK)
141                 {
142                   /* Look up and compare */
143                   hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
144
145                   if (0 == ioam_cache_ts_lookup (ip0,
146                                                  hbh0->protocol,
147                                                  clib_net_to_host_u16
148                                                  (tcp0->src_port),
149                                                  clib_net_to_host_u16
150                                                  (tcp0->dst_port),
151                                                  clib_net_to_host_u32
152                                                  (tcp0->ack_number), &hbh_cmp,
153                                                  &cache_ts_index,
154                                                  &cache_thread_id, 1))
155                     {
156                       /* response seen */
157                       result = -1;
158                       if (hbh_cmp)
159                         result =
160                           ip6_ioam_analyse_compare_path_delay (hbh0, hbh_cmp,
161                                                                cm->criteria_oneway);
162                       if (result >= 0)
163                         {
164                           /* current syn/ack is worse than the earlier: Drop */
165                           next0 = IOAM_CACHE_TS_ERROR_NEXT_DROP;
166                           /* Check if all responses are received or time has exceeded
167                              send cached response if yes */
168                           ioam_cache_ts_check_and_send (cache_thread_id,
169                                                         cache_ts_index);
170                         }
171                       else
172                         {
173                           /* Update cache with this buffer */
174                           /* If successfully updated then skip sending it */
175                           if (0 ==
176                               (result =
177                                ioam_cache_ts_update (cache_thread_id,
178                                                      cache_ts_index, bi0,
179                                                      hbh0)))
180                             {
181                               skip = 1;
182                             }
183                           else
184                             next0 = IOAM_CACHE_TS_ERROR_NEXT_DROP;
185                         }
186                     }
187                   else
188                     {
189                       next0 = IOAM_CACHE_TS_ERROR_NEXT_DROP;
190                     }
191                 }
192               else if ((tcp0->flags & TCP_FLAG_RST) == TCP_FLAG_RST)
193                 {
194                   /* Look up and compare */
195                   hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
196                   if (0 == ioam_cache_ts_lookup (ip0, hbh0->protocol, clib_net_to_host_u16 (tcp0->src_port), clib_net_to_host_u16 (tcp0->dst_port), clib_net_to_host_u32 (tcp0->ack_number), &hbh_cmp, &cache_ts_index, &cache_thread_id, 1))   //response seen
197                     {
198                       next0 = IOAM_CACHE_TS_ERROR_NEXT_DROP;
199                       if (hbh_cmp)
200                         ioam_cache_ts_check_and_send (cache_thread_id,
201                                                       cache_ts_index);
202                     }
203
204                 }
205             }
206           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
207             {
208               if (p0->flags & VLIB_BUFFER_IS_TRACED)
209                 {
210                   cache_ts_trace_t *t =
211                     vlib_add_trace (vm, node, p0, sizeof (*t));
212                   t->flow_label =
213                     clib_net_to_host_u32
214                     (ip0->ip_version_traffic_class_and_flow_label);
215                   t->next_index = next0;
216                 }
217             }
218           /* verify speculative enqueue, maybe switch current next frame */
219           if (!skip)
220             {
221               to_next[0] = bi0;
222               to_next += 1;
223               n_left_to_next -= 1;
224               vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
225                                                to_next, n_left_to_next,
226                                                bi0, next0);
227             }
228         }
229
230       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
231     }
232   vlib_node_increment_counter (vm, ioam_cache_ts_node.index,
233                                CACHE_TS_ERROR_RECORDED, recorded);
234   return frame->n_vectors;
235 }
236
237 /*
238  * Node for IP6 iOAM header cache
239  */
240 /* *INDENT-OFF* */
241 VLIB_REGISTER_NODE (ioam_cache_ts_node) =
242 {
243   .function = ip6_ioam_cache_ts_node_fn,
244   .name = "ip6-ioam-tunnel-select",
245   .vector_size = sizeof (u32),
246   .format_trace = format_cache_ts_trace,
247   .type = VLIB_NODE_TYPE_INTERNAL,
248   .n_errors = ARRAY_LEN (cache_ts_error_strings),
249   .error_strings = cache_ts_error_strings,
250   .n_next_nodes = IOAM_CACHE_TS_N_NEXT,
251   /* edit / add dispositions here */
252   .next_nodes =
253   {
254     [IOAM_CACHE_TS_NEXT_POP_HBYH] = "ip6-pop-hop-by-hop",
255     [IOAM_CACHE_TS_ERROR_NEXT_DROP] = "error-drop",
256   },
257 };
258 /* *INDENT-ON* */
259
260 typedef struct
261 {
262   u32 next_index;
263 } ip6_reset_ts_hbh_trace_t;
264
265 /* packet trace format function */
266 static u8 *
267 format_ip6_reset_ts_hbh_trace (u8 * s, va_list * args)
268 {
269   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
270   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
271   ip6_reset_ts_hbh_trace_t *t = va_arg (*args,
272                                         ip6_reset_ts_hbh_trace_t *);
273
274   s =
275     format (s, "IP6_IOAM_RESET_TUNNEL_SELECT_HBH: next index %d",
276             t->next_index);
277   return s;
278 }
279
280 vlib_node_registration_t ip6_reset_ts_hbh_node;
281
282 #define foreach_ip6_reset_ts_hbh_error \
283 _(PROCESSED, "iOAM Syn/Ack Pkts processed") \
284 _(SAVED, "iOAM Syn Pkts state saved") \
285 _(REMOVED, "iOAM Syn/Ack Pkts state removed")
286
287 typedef enum
288 {
289 #define _(sym,str) IP6_RESET_TS_HBH_ERROR_##sym,
290   foreach_ip6_reset_ts_hbh_error
291 #undef _
292     IP6_RESET_TS_HBH_N_ERROR,
293 } ip6_reset_ts_hbh_error_t;
294
295 static char *ip6_reset_ts_hbh_error_strings[] = {
296 #define _(sym,string) string,
297   foreach_ip6_reset_ts_hbh_error
298 #undef _
299 };
300
301 #define foreach_ip6_ioam_cache_ts_input_next    \
302   _(IP6_LOOKUP, "ip6-lookup")                   \
303   _(DROP, "error-drop")
304
305 typedef enum
306 {
307 #define _(s,n) IP6_IOAM_CACHE_TS_INPUT_NEXT_##s,
308   foreach_ip6_ioam_cache_ts_input_next
309 #undef _
310     IP6_IOAM_CACHE_TS_INPUT_N_NEXT,
311 } ip6_ioam_cache_ts_input_next_t;
312
313
314 static uword
315 ip6_reset_ts_hbh_node_fn (vlib_main_t * vm,
316                           vlib_node_runtime_t * node, vlib_frame_t * frame)
317 {
318   ioam_cache_main_t *cm = &ioam_cache_main;
319   u32 n_left_from, *from, *to_next;
320   ip_lookup_next_t next_index;
321   u32 processed = 0, cache_ts_added = 0;
322   u64 now;
323   u8 *rewrite = cm->rewrite;
324   u32 rewrite_length = vec_len (rewrite);
325   ioam_e2e_cache_option_t *e2e = 0;
326   u8 no_of_responses = cm->wait_for_responses;
327
328   from = vlib_frame_vector_args (frame);
329   n_left_from = frame->n_vectors;
330   next_index = node->cached_next_index;
331
332   while (n_left_from > 0)
333     {
334       u32 n_left_to_next;
335
336       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
337
338       now = vlib_time_now (vm);
339       while (n_left_from >= 4 && n_left_to_next >= 2)
340         {
341           u32 bi0, bi1;
342           vlib_buffer_t *b0, *b1;
343           u32 next0, next1;
344           ip6_header_t *ip0, *ip1;
345           tcp_header_t *tcp0, *tcp1;
346           u32 tcp_offset0, tcp_offset1;
347           ip6_hop_by_hop_header_t *hbh0, *hbh1;
348           u64 *copy_src0, *copy_dst0, *copy_src1, *copy_dst1;
349           u16 new_l0, new_l1;
350           u32 pool_index0 = 0, pool_index1 = 0;
351
352           next0 = next1 = IP6_IOAM_CACHE_TS_INPUT_NEXT_IP6_LOOKUP;
353           /* Prefetch next iteration. */
354           {
355             vlib_buffer_t *p2, *p3;
356
357             p2 = vlib_get_buffer (vm, from[2]);
358             p3 = vlib_get_buffer (vm, from[3]);
359
360             vlib_prefetch_buffer_header (p2, LOAD);
361             vlib_prefetch_buffer_header (p3, LOAD);
362             CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
363             CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
364           }
365
366
367           /* speculatively enqueue b0 to the current next frame */
368           to_next[0] = bi0 = from[0];
369           to_next[1] = bi1 = from[1];
370           from += 2;
371           to_next += 2;
372           n_left_from -= 2;
373           n_left_to_next -= 2;
374
375           b0 = vlib_get_buffer (vm, bi0);
376           b1 = vlib_get_buffer (vm, bi1);
377
378           ip0 = vlib_buffer_get_current (b0);
379           ip1 = vlib_buffer_get_current (b1);
380
381           if (IP_PROTOCOL_TCP !=
382               ip6_locate_header (b0, ip0, IP_PROTOCOL_TCP, &tcp_offset0))
383             {
384               goto NEXT00;
385             }
386           tcp0 = (tcp_header_t *) ((u8 *) ip0 + tcp_offset0);
387           if ((tcp0->flags & TCP_FLAG_SYN) == TCP_FLAG_SYN &&
388               (tcp0->flags & TCP_FLAG_ACK) == 0)
389             {
390               if (no_of_responses > 0)
391                 {
392                   /* Create TS select entry */
393                   if (0 == ioam_cache_ts_add (ip0,
394                                               clib_net_to_host_u16
395                                               (tcp0->src_port),
396                                               clib_net_to_host_u16
397                                               (tcp0->dst_port),
398                                               clib_net_to_host_u32
399                                               (tcp0->seq_number) + 1,
400                                               no_of_responses, now,
401                                               vm->cpu_index, &pool_index0))
402                     {
403                       cache_ts_added++;
404                     }
405                 }
406               copy_dst0 = (u64 *) (((u8 *) ip0) - rewrite_length);
407               copy_src0 = (u64 *) ip0;
408
409               copy_dst0[0] = copy_src0[0];
410               copy_dst0[1] = copy_src0[1];
411               copy_dst0[2] = copy_src0[2];
412               copy_dst0[3] = copy_src0[3];
413               copy_dst0[4] = copy_src0[4];
414
415               vlib_buffer_advance (b0, -(word) rewrite_length);
416               ip0 = vlib_buffer_get_current (b0);
417
418               hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
419               /* $$$ tune, rewrite_length is a multiple of 8 */
420               clib_memcpy (hbh0, rewrite, rewrite_length);
421               e2e =
422                 (ioam_e2e_cache_option_t *) ((u8 *) hbh0 +
423                                              cm->rewrite_pool_index_offset);
424               e2e->pool_id = (u8) vm->cpu_index;
425               e2e->pool_index = pool_index0;
426               ioam_e2e_id_rewrite_handler ((ioam_e2e_id_option_t *)
427                                            ((u8 *) e2e +
428                                             sizeof (ioam_e2e_cache_option_t)),
429                                            b0);
430               /* Patch the protocol chain, insert the h-b-h (type 0) header */
431               hbh0->protocol = ip0->protocol;
432               ip0->protocol = 0;
433               new_l0 =
434                 clib_net_to_host_u16 (ip0->payload_length) + rewrite_length;
435               ip0->payload_length = clib_host_to_net_u16 (new_l0);
436               processed++;
437             }
438
439         NEXT00:
440           if (IP_PROTOCOL_TCP !=
441               ip6_locate_header (b1, ip1, IP_PROTOCOL_TCP, &tcp_offset1))
442             {
443               goto TRACE00;
444             }
445           tcp1 = (tcp_header_t *) ((u8 *) ip1 + tcp_offset1);
446           if ((tcp1->flags & TCP_FLAG_SYN) == TCP_FLAG_SYN &&
447               (tcp1->flags & TCP_FLAG_ACK) == 0)
448             {
449               if (no_of_responses > 0)
450                 {
451                   /* Create TS select entry */
452                   if (0 == ioam_cache_ts_add (ip1,
453                                               clib_net_to_host_u16
454                                               (tcp1->src_port),
455                                               clib_net_to_host_u16
456                                               (tcp1->dst_port),
457                                               clib_net_to_host_u32
458                                               (tcp1->seq_number) + 1,
459                                               no_of_responses, now,
460                                               vm->cpu_index, &pool_index1))
461                     {
462                       cache_ts_added++;
463                     }
464                 }
465
466               copy_dst1 = (u64 *) (((u8 *) ip1) - rewrite_length);
467               copy_src1 = (u64 *) ip1;
468
469               copy_dst1[0] = copy_src1[0];
470               copy_dst1[1] = copy_src1[1];
471               copy_dst1[2] = copy_src1[2];
472               copy_dst1[3] = copy_src1[3];
473               copy_dst1[4] = copy_src1[4];
474
475               vlib_buffer_advance (b1, -(word) rewrite_length);
476               ip1 = vlib_buffer_get_current (b1);
477
478               hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
479               /* $$$ tune, rewrite_length is a multiple of 8 */
480               clib_memcpy (hbh1, rewrite, rewrite_length);
481               e2e =
482                 (ioam_e2e_cache_option_t *) ((u8 *) hbh1 +
483                                              cm->rewrite_pool_index_offset);
484               e2e->pool_id = (u8) vm->cpu_index;
485               e2e->pool_index = pool_index1;
486               ioam_e2e_id_rewrite_handler ((ioam_e2e_id_option_t *)
487                                            ((u8 *) e2e +
488                                             sizeof (ioam_e2e_cache_option_t)),
489                                            b1);
490               /* Patch the protocol chain, insert the h-b-h (type 0) header */
491               hbh1->protocol = ip1->protocol;
492               ip1->protocol = 0;
493               new_l1 =
494                 clib_net_to_host_u16 (ip1->payload_length) + rewrite_length;
495               ip1->payload_length = clib_host_to_net_u16 (new_l1);
496               processed++;
497             }
498
499         TRACE00:
500           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
501             {
502               if (b0->flags & VLIB_BUFFER_IS_TRACED)
503                 {
504                   ip6_reset_ts_hbh_trace_t *t =
505                     vlib_add_trace (vm, node, b0, sizeof (*t));
506                   t->next_index = next0;
507                 }
508               if (b1->flags & VLIB_BUFFER_IS_TRACED)
509                 {
510                   ip6_reset_ts_hbh_trace_t *t =
511                     vlib_add_trace (vm, node, b1, sizeof (*t));
512                   t->next_index = next1;
513                 }
514
515             }
516
517           /* verify speculative enqueue, maybe switch current next frame */
518           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
519                                            to_next, n_left_to_next,
520                                            bi0, bi1, next0, next1);
521         }
522       while (n_left_from > 0 && n_left_to_next > 0)
523         {
524           u32 bi0;
525           vlib_buffer_t *b0;
526           u32 next0;
527           ip6_header_t *ip0;
528           tcp_header_t *tcp0;
529           u32 tcp_offset0;
530           ip6_hop_by_hop_header_t *hbh0;
531           u64 *copy_src0, *copy_dst0;
532           u16 new_l0;
533           u32 pool_index0 = 0;
534
535           next0 = IP6_IOAM_CACHE_TS_INPUT_NEXT_IP6_LOOKUP;
536           /* speculatively enqueue b0 to the current next frame */
537           bi0 = from[0];
538           to_next[0] = bi0;
539           from += 1;
540           to_next += 1;
541           n_left_from -= 1;
542           n_left_to_next -= 1;
543
544           b0 = vlib_get_buffer (vm, bi0);
545
546           ip0 = vlib_buffer_get_current (b0);
547           if (IP_PROTOCOL_TCP !=
548               ip6_locate_header (b0, ip0, IP_PROTOCOL_TCP, &tcp_offset0))
549             {
550               goto TRACE0;
551             }
552           tcp0 = (tcp_header_t *) ((u8 *) ip0 + tcp_offset0);
553           if ((tcp0->flags & TCP_FLAG_SYN) == TCP_FLAG_SYN &&
554               (tcp0->flags & TCP_FLAG_ACK) == 0)
555             {
556               if (no_of_responses > 0)
557                 {
558                   /* Create TS select entry */
559                   if (0 == ioam_cache_ts_add (ip0,
560                                               clib_net_to_host_u16
561                                               (tcp0->src_port),
562                                               clib_net_to_host_u16
563                                               (tcp0->dst_port),
564                                               clib_net_to_host_u32
565                                               (tcp0->seq_number) + 1,
566                                               no_of_responses, now,
567                                               vm->cpu_index, &pool_index0))
568                     {
569                       cache_ts_added++;
570                     }
571                 }
572               copy_dst0 = (u64 *) (((u8 *) ip0) - rewrite_length);
573               copy_src0 = (u64 *) ip0;
574
575               copy_dst0[0] = copy_src0[0];
576               copy_dst0[1] = copy_src0[1];
577               copy_dst0[2] = copy_src0[2];
578               copy_dst0[3] = copy_src0[3];
579               copy_dst0[4] = copy_src0[4];
580
581               vlib_buffer_advance (b0, -(word) rewrite_length);
582               ip0 = vlib_buffer_get_current (b0);
583
584               hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
585               /* $$$ tune, rewrite_length is a multiple of 8 */
586               clib_memcpy (hbh0, rewrite, rewrite_length);
587               e2e =
588                 (ioam_e2e_cache_option_t *) ((u8 *) hbh0 +
589                                              cm->rewrite_pool_index_offset);
590               e2e->pool_id = (u8) vm->cpu_index;
591               e2e->pool_index = pool_index0;
592               ioam_e2e_id_rewrite_handler ((ioam_e2e_id_option_t *)
593                                            ((u8 *) e2e +
594                                             sizeof (ioam_e2e_cache_option_t)),
595                                            b0);
596               /* Patch the protocol chain, insert the h-b-h (type 0) header */
597               hbh0->protocol = ip0->protocol;
598               ip0->protocol = 0;
599               new_l0 =
600                 clib_net_to_host_u16 (ip0->payload_length) + rewrite_length;
601               ip0->payload_length = clib_host_to_net_u16 (new_l0);
602               processed++;
603             }
604         TRACE0:
605           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
606                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
607             {
608               ip6_reset_ts_hbh_trace_t *t =
609                 vlib_add_trace (vm, node, b0, sizeof (*t));
610               t->next_index = next0;
611             }
612
613           /* verify speculative enqueue, maybe switch current next frame */
614           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
615                                            to_next, n_left_to_next,
616                                            bi0, next0);
617         }
618
619       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
620     }
621
622   vlib_node_increment_counter (vm, ip6_reset_ts_hbh_node.index,
623                                IP6_RESET_TS_HBH_ERROR_PROCESSED, processed);
624   vlib_node_increment_counter (vm, ip6_reset_ts_hbh_node.index,
625                                IP6_RESET_TS_HBH_ERROR_SAVED, cache_ts_added);
626
627   return frame->n_vectors;
628 }
629
630 /* *INDENT-OFF* */
631 VLIB_REGISTER_NODE (ip6_reset_ts_hbh_node) =
632 {
633   .function = ip6_reset_ts_hbh_node_fn,
634   .name = "ip6-add-syn-hop-by-hop",
635   .vector_size = sizeof (u32),
636   .format_trace = format_ip6_reset_ts_hbh_trace,
637   .type = VLIB_NODE_TYPE_INTERNAL,
638   .n_errors = ARRAY_LEN (ip6_reset_ts_hbh_error_strings),
639   .error_strings =  ip6_reset_ts_hbh_error_strings,
640   /* See ip/lookup.h */
641   .n_next_nodes = IP6_IOAM_CACHE_TS_INPUT_N_NEXT,
642   .next_nodes =
643   {
644 #define _(s,n) [IP6_IOAM_CACHE_TS_INPUT_NEXT_##s] = n,
645     foreach_ip6_ioam_cache_ts_input_next
646 #undef _
647   },
648 };
649
650 VLIB_NODE_FUNCTION_MULTIARCH (ip6_reset_ts_hbh_node, ip6_reset_ts_hbh_node_fn)
651 /* *INDENT-ON* */
652
653 vlib_node_registration_t ioam_cache_ts_timer_tick_node;
654
655 typedef struct
656 {
657   u32 thread_index;
658 } ioam_cache_ts_timer_tick_trace_t;
659
660 /* packet trace format function */
661 static u8 *
662 format_ioam_cache_ts_timer_tick_trace (u8 * s, va_list * args)
663 {
664   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
665   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
666   ioam_cache_ts_timer_tick_trace_t *t =
667     va_arg (*args, ioam_cache_ts_timer_tick_trace_t *);
668
669   s = format (s, "IOAM_CACHE_TS_TIMER_TICK: thread index %d",
670               t->thread_index);
671   return s;
672 }
673
674 #define foreach_ioam_cache_ts_timer_tick_error                 \
675   _(TIMER, "Timer events")
676
677 typedef enum
678 {
679 #define _(sym,str) IOAM_CACHE_TS_TIMER_TICK_ERROR_##sym,
680   foreach_ioam_cache_ts_timer_tick_error
681 #undef _
682     IOAM_CACHE_TS_TIMER_TICK_N_ERROR,
683 } ioam_cache_ts_timer_tick_error_t;
684
685 static char *ioam_cache_ts_timer_tick_error_strings[] = {
686 #define _(sym,string) string,
687   foreach_ioam_cache_ts_timer_tick_error
688 #undef _
689 };
690
691 void
692 ioam_cache_ts_timer_node_enable (vlib_main_t * vm, u8 enable)
693 {
694   vlib_node_set_state (vm, ioam_cache_ts_timer_tick_node.index,
695                        enable ==
696                        0 ? VLIB_NODE_STATE_DISABLED :
697                        VLIB_NODE_STATE_POLLING);
698 }
699
700 void
701 expired_cache_ts_timer_callback (u32 * expired_timers)
702 {
703   ioam_cache_main_t *cm = &ioam_cache_main;
704   int i;
705   u32 pool_index;
706   u32 thread_index = os_get_cpu_number ();
707   u32 count = 0;
708
709   for (i = 0; i < vec_len (expired_timers); i++)
710     {
711       /* Get pool index and pool id */
712       pool_index = expired_timers[i] & 0x0FFFFFFF;
713
714       /* Handle expiration */
715       ioam_cache_ts_send (thread_index, pool_index);
716       count++;
717     }
718   vlib_node_increment_counter (cm->vlib_main,
719                                ioam_cache_ts_timer_tick_node.index,
720                                IOAM_CACHE_TS_TIMER_TICK_ERROR_TIMER, count);
721 }
722
723 static uword
724 ioam_cache_ts_timer_tick_node_fn (vlib_main_t * vm,
725                                   vlib_node_runtime_t * node,
726                                   vlib_frame_t * f)
727 {
728   ioam_cache_main_t *cm = &ioam_cache_main;
729   u32 my_thread_index = os_get_cpu_number ();
730   struct timespec ts, tsrem;
731
732   tw_timer_expire_timers_16t_2w_512sl (&cm->timer_wheels[my_thread_index],
733                                        vlib_time_now (vm));
734   ts.tv_sec = 0;
735   ts.tv_nsec = 1000 * 1000 * IOAM_CACHE_TS_TICK;
736   while (nanosleep (&ts, &tsrem) < 0)
737     {
738       ts = tsrem;
739     }
740
741   return 0;
742 }
743 /* *INDENT-OFF* */
744 VLIB_REGISTER_NODE (ioam_cache_ts_timer_tick_node) = {
745   .function = ioam_cache_ts_timer_tick_node_fn,
746   .name = "ioam-cache-ts-timer-tick",
747   .format_trace = format_ioam_cache_ts_timer_tick_trace,
748   .type = VLIB_NODE_TYPE_INPUT,
749
750   .n_errors = ARRAY_LEN(ioam_cache_ts_timer_tick_error_strings),
751   .error_strings = ioam_cache_ts_timer_tick_error_strings,
752
753   .n_next_nodes = 1,
754
755   .state = VLIB_NODE_STATE_DISABLED,
756
757   /* edit / add dispositions here */
758   .next_nodes = {
759     [0] = "error-drop",
760   },
761 };
762 /* *INDENT-ON* */
763
764 /*
765  * fd.io coding-style-patch-verification: ON
766  *
767  * Local Variables:
768  * eval: (c-set-style "gnu")
769  * End:
770  */