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