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