First commit SR MPLS
[vpp.git] / src / plugins / ioam / ip6 / ioam_cache.h
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 #ifndef __included_ioam_cache_h__
16 #define __included_ioam_cache_h__
17
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/ip_packet.h>
21 #include <vnet/ip/ip4_packet.h>
22 #include <vnet/ip/ip6_packet.h>
23 #include <vnet/srv6/sr.h>
24
25 #include <vppinfra/pool.h>
26 #include <vppinfra/hash.h>
27 #include <vppinfra/error.h>
28 #include <vppinfra/elog.h>
29 #include <vppinfra/bihash_8_8.h>
30 #include <ioam/analyse/ip6/ip6_ioam_analyse.h>
31 #include <vppinfra/tw_timer_16t_2w_512sl.h>
32 /*
33  * ioam_cache.h
34  * This header contains routines for caching of ioam header and
35  * buffer:
36  * 1 - On application facing node: to cache ioam header recvd
37  *     in request and reattach in response to provide round
38  *     trip path visibility. Since request response matching
39  *     is needed works with TCP and relies on (5 tuples,seq no)
40  * 2 - On M-Anycast server node: This node replicates requests
41  *    towards multiple anycast service nodes serving anycast
42  *    IP6 address. It evaluates response and forwards the best
43  *    response towards the client of requesting the service.
44  *    Again since request-response matching is needed, works
45  *    with TCP  and relies on (5 tuples,seq no) for matching.
46  *    To do this it caches SYN-ACK responses for a short time to
47  *    evaluate multiple responses received before the selected
48  *    SYN-ACK response is forwared and others dropped.
49  *
50  * M-Anycast server cache:
51  *   - There is a pool of cache entries per worker thread.
52  *   - Cache entry is created when SYN is received expected
53  *     number of responses are marked based on number of
54  *     SR tunnels for the anycast destination address
55  *   - The pool/thread id and pool index are attached in the
56  *    message as an ioam option for quick look up.
57  *   - When is received SYN-ACK the ioam option containing
58  *     thread id + pool index of the cache entry is used to
59  *     look up cache entry.
60  *   - Cache synchronization:
61  *      - This is achieved by cache entry add/del/update all handled
62  *        by the same worker/main thread
63  *      - Packets from client to threads - syn packets, can be disctributed
64  *        based on incoming interface affinity to the cpu core pinned to
65  *        the thread or a simple sequence number based distribution
66  *        if thread per interface is not scaling
67  *      - Response packets from server towards clients - syn-acks, are
68  *        forced to the same thread that created the cache entry
69  *        using SR and the destination of SR v6 address assigned
70  *        to the core/thread. This adderss is sent as an ioam option
71  *        in the syn that can be then used on the other side to
72  *        populate v6 dst address in the response
73  *      - Timeout: timer wheel per thread is used to track the syn-ack wait
74  *        time. The timer wheel tick is updated via an input node per thread.
75  *
76  * Application facing node/Service side cache:
77  *  - Single pool of cache entries.
78  *  - Cache entry is created when SYN is received. Caches the ioam
79  *    header. Hash table entry is created based on 5 tuple and
80  *    TCP seq no to pool index
81  *  - Response SYN-ACK processed by looking up pool index in hash table
82  *    and cache entry in the pool is used to get the ioam header rewrite
83  *    string. Entry is freed from pool and hash table after use.
84  *  - Locking/Synchronization: Currently this functionality is deployed
85  *    with main/single thread only. Hence no locking is used.
86  *  - Deployment: A VPP node per application server servicing anycast
87  *    address is expected. Locking/synchronization needed when the server
88  *    /application facing node is started with multiple worker threads.
89  *
90  */
91
92 /*
93  * Application facing server side caching:
94  * Cache entry for ioam header
95  * Currently caters to TCP and relies on
96  * TCP - 5 tuples + seqno to cache and reinsert
97  * ioam header b/n TCP request response
98  */
99 typedef struct
100 {
101   ip6_address_t src_address;
102   ip6_address_t dst_address;
103   u16 src_port;
104   u16 dst_port;
105   u8 protocol;
106   u32 seq_no;
107   ip6_address_t next_hop;
108   u16 my_address_offset;
109   u8 *ioam_rewrite_string;
110 } ioam_cache_entry_t;
111
112 /*
113  * Cache entry for anycast server selection
114  * Works for TCP as 5 tuple + sequence number
115  * is required for request response matching
116  * max_responses expected is set based on number
117  *              of SR tunnels for the dst_address
118  * Timeout or all response_received = max_responses
119  *            will clear the entry
120  * buffer_index index of the response msg vlib buffer
121  *           that is currently the best response
122  */
123 typedef struct
124 {
125   u32 pool_id;
126   u32 pool_index;
127   ip6_address_t src_address;
128   ip6_address_t dst_address;
129   u16 src_port;
130   u16 dst_port;
131   u8 protocol;
132   u32 seq_no;
133   u32 buffer_index;
134   ip6_hop_by_hop_header_t *hbh; //pointer to hbh header in the buffer
135   u64 created_at;
136   u8 response_received;
137   u8 max_responses;
138   u32 stop_timer_handle;
139   /** Handle returned from tw_start_timer */
140   u32 timer_handle;
141   /** entry should expire at this clock tick */
142   u32 expected_to_expire;
143 } ioam_cache_ts_entry_t;
144
145 /*
146  * Per thread tunnel selection cache stats
147  */
148 typedef struct
149 {
150   u64 inuse;
151   u64 add_failed;
152 } ioam_cache_ts_pool_stats_t;
153
154 /* Server side: iOAM header caching */
155 #define MAX_CACHE_ENTRIES 4096
156 /* M-Anycast: Cache for SR tunnel selection */
157 #define MAX_CACHE_TS_ENTRIES 1048576
158
159 #define IOAM_CACHE_TABLE_DEFAULT_HASH_NUM_BUCKETS (4 * 1024)
160 #define IOAM_CACHE_TABLE_DEFAULT_HASH_MEMORY_SIZE (2<<20)
161
162 typedef struct
163 {
164   /* API message ID base */
165   u16 msg_id_base;
166
167   /* Pool of ioam_cache_buffer_t */
168   ioam_cache_entry_t *ioam_rewrite_pool;
169
170   /* For steering packets ioam cache entry is followed by
171    * SR header. This is the SR rewrite template */
172   u8 *sr_rewrite_template;
173   /* The current rewrite string being used */
174   u8 *rewrite;
175   u8 rewrite_pool_index_offset;
176
177   u64 lookup_table_nbuckets;
178   u64 lookup_table_size;
179   clib_bihash_8_8_t ioam_rewrite_cache_table;
180
181   /* M-Anycast: Pool of ioam_cache_ts_entry_t per thread */
182   ioam_cache_ts_entry_t **ioam_ts_pool;
183   ioam_cache_ts_pool_stats_t *ts_stats;
184   /** per thread single-wheel */
185   tw_timer_wheel_16t_2w_512sl_t *timer_wheels;
186
187   /*
188    * Selection criteria: oneway delay: Server to M-Anycast
189    * or RTT
190    */
191   bool criteria_oneway;
192   u8 wait_for_responses;
193
194   /* convenience */
195   vlib_main_t *vlib_main;
196
197   uword cache_hbh_slot;
198   uword ts_hbh_slot;
199   u32 ip6_hbh_pop_node_index;
200   u32 error_node_index;
201   u32 cleanup_process_node_index;
202 } ioam_cache_main_t;
203
204 ioam_cache_main_t ioam_cache_main;
205
206 extern vlib_node_registration_t ioam_cache_node;
207 extern vlib_node_registration_t ioam_cache_ts_node;
208
209 /*  Compute flow hash.  We'll use it to select which Sponge to use for this
210  *  flow.  And other things.
211  *  ip6_compute_flow_hash in ip6.h doesnt locate tcp/udp when
212  *  ext headers are present. While it could be made to it will be a
213  *  performance hit for ECMP flows.
214  *  HEnce this function here, with L4 information directly input
215  *  Useful when tcp/udp headers are already located in presence of
216  *  ext headers
217  */
218 always_inline u32
219 ip6_compute_flow_hash_ext (const ip6_header_t * ip,
220                            u8 protocol,
221                            u16 src_port,
222                            u16 dst_port, flow_hash_config_t flow_hash_config)
223 {
224   u64 a, b, c;
225   u64 t1, t2;
226
227   t1 = (ip->src_address.as_u64[0] ^ ip->src_address.as_u64[1]);
228   t1 = (flow_hash_config & IP_FLOW_HASH_SRC_ADDR) ? t1 : 0;
229
230   t2 = (ip->dst_address.as_u64[0] ^ ip->dst_address.as_u64[1]);
231   t2 = (flow_hash_config & IP_FLOW_HASH_DST_ADDR) ? t2 : 0;
232
233   a = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t2 : t1;
234   b = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t1 : t2;
235   b ^= (flow_hash_config & IP_FLOW_HASH_PROTO) ? protocol : 0;
236
237   t1 = src_port;
238   t2 = dst_port;
239
240   t1 = (flow_hash_config & IP_FLOW_HASH_SRC_PORT) ? t1 : 0;
241   t2 = (flow_hash_config & IP_FLOW_HASH_DST_PORT) ? t2 : 0;
242
243   c = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ?
244     ((t1 << 16) | t2) : ((t2 << 16) | t1);
245
246   hash_mix64 (a, b, c);
247   return (u32) c;
248 }
249
250
251 /* 2 new ioam E2E options :
252  * 1. HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID: IP6 address
253  *                of ioam node that inserted ioam header
254  * 2. HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID: Pool id and index
255  *                   to look up tunnel select cache entry
256  */
257 #define HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID 30
258 #define HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID 31
259
260 typedef CLIB_PACKED (struct
261                      {
262                      ip6_hop_by_hop_option_t hdr; u8 e2e_type; u8 reserved[5];
263                      ip6_address_t id;
264                      }) ioam_e2e_id_option_t;
265
266 typedef CLIB_PACKED (struct
267                      {
268                      ip6_hop_by_hop_option_t hdr; u8 e2e_type; u8 pool_id;
269                      u32 pool_index;
270                      }) ioam_e2e_cache_option_t;
271
272 #define IOAM_E2E_ID_OPTION_RND ((sizeof(ioam_e2e_id_option_t) + 7) & ~7)
273 #define IOAM_E2E_ID_HBH_EXT_LEN (IOAM_E2E_ID_OPTION_RND >> 3)
274 #define IOAM_E2E_CACHE_OPTION_RND ((sizeof(ioam_e2e_cache_option_t) + 7) & ~7)
275 #define IOAM_E2E_CACHE_HBH_EXT_LEN (IOAM_E2E_CACHE_OPTION_RND >> 3)
276
277 static inline void
278 ioam_e2e_id_rewrite_handler (ioam_e2e_id_option_t * e2e_option,
279                              vlib_buffer_t * b0)
280 {
281   ip6_main_t *im = &ip6_main;
282   ip6_address_t *my_address = 0;
283   my_address =
284     ip6_interface_first_address (im, vnet_buffer (b0)->sw_if_index[VLIB_RX]);
285   if (my_address)
286     {
287       e2e_option->id.as_u64[0] = my_address->as_u64[0];
288       e2e_option->id.as_u64[1] = my_address->as_u64[1];
289     }
290 }
291
292 /* Following functions are for the caching of ioam header
293  * to enable reattaching it for a complete request-response
294  * message exchange */
295 inline static void
296 ioam_cache_entry_free (ioam_cache_entry_t * entry)
297 {
298   ioam_cache_main_t *cm = &ioam_cache_main;
299   if (entry)
300     {
301       vec_free (entry->ioam_rewrite_string);
302       memset (entry, 0, sizeof (*entry));
303       pool_put (cm->ioam_rewrite_pool, entry);
304     }
305 }
306
307 inline static ioam_cache_entry_t *
308 ioam_cache_entry_cleanup (u32 pool_index)
309 {
310   ioam_cache_main_t *cm = &ioam_cache_main;
311   ioam_cache_entry_t *entry = 0;
312
313   entry = pool_elt_at_index (cm->ioam_rewrite_pool, pool_index);
314   ioam_cache_entry_free (entry);
315   return (0);
316 }
317
318 inline static ioam_cache_entry_t *
319 ioam_cache_lookup (ip6_header_t * ip0, u16 src_port, u16 dst_port, u32 seq_no)
320 {
321   ioam_cache_main_t *cm = &ioam_cache_main;
322   u32 flow_hash = ip6_compute_flow_hash_ext (ip0, ip0->protocol,
323                                              src_port, dst_port,
324                                              IP_FLOW_HASH_DEFAULT |
325                                              IP_FLOW_HASH_REVERSE_SRC_DST);
326   clib_bihash_kv_8_8_t kv, value;
327
328   kv.key = (u64) flow_hash << 32 | seq_no;
329   kv.value = 0;
330   value.key = 0;
331   value.value = 0;
332
333   if (clib_bihash_search_8_8 (&cm->ioam_rewrite_cache_table, &kv, &value) >=
334       0)
335     {
336       ioam_cache_entry_t *entry = 0;
337
338       entry = pool_elt_at_index (cm->ioam_rewrite_pool, value.value);
339       /* match */
340       if (ip6_address_compare (&ip0->src_address, &entry->dst_address) == 0 &&
341           ip6_address_compare (&ip0->dst_address, &entry->src_address) == 0 &&
342           entry->src_port == dst_port &&
343           entry->dst_port == src_port && entry->seq_no == seq_no)
344         {
345           /* If lookup is successful remove it from the hash */
346           clib_bihash_add_del_8_8 (&cm->ioam_rewrite_cache_table, &kv, 0);
347           return (entry);
348         }
349       else
350         return (0);
351
352     }
353   return (0);
354 }
355
356 /*
357  * Caches ioam hbh header
358  * Extends the hbh header with option to contain IP6 address of the node
359  * that caches it
360  */
361 inline static int
362 ioam_cache_add (vlib_buffer_t * b0,
363                 ip6_header_t * ip0,
364                 u16 src_port,
365                 u16 dst_port, ip6_hop_by_hop_header_t * hbh0, u32 seq_no)
366 {
367   ioam_cache_main_t *cm = &ioam_cache_main;
368   ioam_cache_entry_t *entry = 0;
369   u32 rewrite_len = 0, e2e_id_offset = 0;
370   u32 pool_index = 0;
371   ioam_e2e_id_option_t *e2e = 0;
372
373   pool_get_aligned (cm->ioam_rewrite_pool, entry, CLIB_CACHE_LINE_BYTES);
374   memset (entry, 0, sizeof (*entry));
375   pool_index = entry - cm->ioam_rewrite_pool;
376
377   clib_memcpy (entry->dst_address.as_u64, ip0->dst_address.as_u64,
378                sizeof (ip6_address_t));
379   clib_memcpy (entry->src_address.as_u64, ip0->src_address.as_u64,
380                sizeof (ip6_address_t));
381   entry->src_port = src_port;
382   entry->dst_port = dst_port;
383   entry->seq_no = seq_no;
384   rewrite_len = ((hbh0->length + 1) << 3);
385   vec_validate (entry->ioam_rewrite_string, rewrite_len - 1);
386   e2e = ip6_ioam_find_hbh_option (hbh0, HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID);
387   if (e2e)
388     {
389       entry->next_hop.as_u64[0] = e2e->id.as_u64[0];
390       entry->next_hop.as_u64[1] = e2e->id.as_u64[1];
391     }
392   else
393     {
394       return (-1);
395     }
396   e2e_id_offset = (u8 *) e2e - (u8 *) hbh0;
397   /* setup e2e id option to insert v6 address of the node caching it */
398   clib_memcpy (entry->ioam_rewrite_string, hbh0, rewrite_len);
399   hbh0 = (ip6_hop_by_hop_header_t *) entry->ioam_rewrite_string;
400
401   /* suffix rewrite string with e2e ID option */
402   e2e = (ioam_e2e_id_option_t *) (entry->ioam_rewrite_string + e2e_id_offset);
403   ioam_e2e_id_rewrite_handler (e2e, b0);
404   entry->my_address_offset = (u8 *) (&e2e->id) - (u8 *) hbh0;
405
406   /* add it to hash, replacing and freeing any collision for now */
407   u32 flow_hash =
408     ip6_compute_flow_hash_ext (ip0, hbh0->protocol, src_port, dst_port,
409                                IP_FLOW_HASH_DEFAULT);
410   clib_bihash_kv_8_8_t kv, value;
411   kv.key = (u64) flow_hash << 32 | seq_no;
412   kv.value = 0;
413   if (clib_bihash_search_8_8 (&cm->ioam_rewrite_cache_table, &kv, &value) >=
414       0)
415     {
416       /* replace */
417       ioam_cache_entry_cleanup (value.value);
418     }
419   kv.value = pool_index;
420   clib_bihash_add_del_8_8 (&cm->ioam_rewrite_cache_table, &kv, 1);
421   return (0);
422 }
423
424 /* Creates SR rewrite string
425  * This is appended with ioam header on the server facing
426  * node.
427  * This SR header is necessary to attract packets towards
428  * selected Anycast server.
429  */
430 inline static void
431 ioam_cache_sr_rewrite_template_create (void)
432 {
433   ioam_cache_main_t *cm = &ioam_cache_main;
434   ip6_address_t *segments = 0;
435   ip6_address_t *this_seg = 0;
436
437   /* This nodes address and the original dest will be
438    * filled when the packet is processed */
439   vec_add2 (segments, this_seg, 1);
440   memset (this_seg, 0xfe, sizeof (ip6_address_t));
441   cm->sr_rewrite_template = ip6_sr_compute_rewrite_string_insert (segments);
442   vec_free (segments);
443 }
444
445 inline static int
446 ioam_cache_table_init (vlib_main_t * vm)
447 {
448   ioam_cache_main_t *cm = &ioam_cache_main;
449
450   pool_alloc_aligned (cm->ioam_rewrite_pool,
451                       MAX_CACHE_ENTRIES, CLIB_CACHE_LINE_BYTES);
452   cm->lookup_table_nbuckets = IOAM_CACHE_TABLE_DEFAULT_HASH_NUM_BUCKETS;
453   cm->lookup_table_nbuckets = 1 << max_log2 (cm->lookup_table_nbuckets);
454   cm->lookup_table_size = IOAM_CACHE_TABLE_DEFAULT_HASH_MEMORY_SIZE;
455
456   clib_bihash_init_8_8 (&cm->ioam_rewrite_cache_table,
457                         "ioam rewrite cache table",
458                         cm->lookup_table_nbuckets, cm->lookup_table_size);
459   /* Create SR rewrite template */
460   ioam_cache_sr_rewrite_template_create ();
461   return (1);
462 }
463
464 inline static int
465 ioam_cache_table_destroy (vlib_main_t * vm)
466 {
467   ioam_cache_main_t *cm = &ioam_cache_main;
468   ioam_cache_entry_t *entry = 0;
469   /* free pool and hash table */
470   clib_bihash_free_8_8 (&cm->ioam_rewrite_cache_table);
471   pool_foreach (entry, cm->ioam_rewrite_pool, (
472                                                 {
473                                                 ioam_cache_entry_free (entry);
474                                                 }));
475   pool_free (cm->ioam_rewrite_pool);
476   cm->ioam_rewrite_pool = 0;
477   vec_free (cm->sr_rewrite_template);
478   cm->sr_rewrite_template = 0;
479   return (0);
480 }
481
482 inline static u8 *
483 format_ioam_cache_entry (u8 * s, va_list * args)
484 {
485   ioam_cache_entry_t *e = va_arg (*args, ioam_cache_entry_t *);
486   ioam_cache_main_t *cm = &ioam_cache_main;
487   int rewrite_len = vec_len (e->ioam_rewrite_string);
488
489   s = format (s, "%d: %U:%d to  %U:%d seq_no %lu\n",
490               (e - cm->ioam_rewrite_pool),
491               format_ip6_address, &e->src_address,
492               e->src_port,
493               format_ip6_address, &e->dst_address, e->dst_port, e->seq_no);
494
495   if (rewrite_len)
496     {
497       s = format (s, "  %U",
498                   format_ip6_hop_by_hop_ext_hdr,
499                   (ip6_hop_by_hop_header_t *) e->ioam_rewrite_string,
500                   rewrite_len - 1);
501     }
502   return s;
503 }
504
505 void ioam_cache_ts_timer_node_enable (vlib_main_t * vm, u8 enable);
506
507 #define IOAM_CACHE_TS_TIMEOUT 1.0       //SYN timeout 1 sec
508 #define IOAM_CACHE_TS_TICK 100e-3
509 /* Timer delays as multiples of 100ms */
510 #define IOAM_CACHE_TS_TIMEOUT_TICKS IOAM_CACHE_TS_TICK*9
511 #define TIMER_HANDLE_INVALID ((u32) ~0)
512
513
514 void expired_cache_ts_timer_callback (u32 * expired_timers);
515
516 /*
517  * Following functions are to manage M-Anycast server selection
518  * cache
519  * There is a per worker thread pool to create a cache entry
520  * for a TCP SYN received. TCP SYN-ACK contians ioam header
521  * with HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID option to point to the
522  * entry.
523  */
524 inline static int
525 ioam_cache_ts_table_init (vlib_main_t * vm)
526 {
527   ioam_cache_main_t *cm = &ioam_cache_main;
528   int no_of_threads = vec_len (vlib_worker_threads);
529   int i;
530
531   vec_validate_aligned (cm->ioam_ts_pool, no_of_threads - 1,
532                         CLIB_CACHE_LINE_BYTES);
533   vec_validate_aligned (cm->ts_stats, no_of_threads - 1,
534                         CLIB_CACHE_LINE_BYTES);
535   vec_validate_aligned (cm->timer_wheels, no_of_threads - 1,
536                         CLIB_CACHE_LINE_BYTES);
537   cm->lookup_table_nbuckets = IOAM_CACHE_TABLE_DEFAULT_HASH_NUM_BUCKETS;
538   cm->lookup_table_nbuckets = 1 << max_log2 (cm->lookup_table_nbuckets);
539   cm->lookup_table_size = IOAM_CACHE_TABLE_DEFAULT_HASH_MEMORY_SIZE;
540   for (i = 0; i < no_of_threads; i++)
541     {
542       pool_alloc_aligned (cm->ioam_ts_pool[i],
543                           MAX_CACHE_TS_ENTRIES, CLIB_CACHE_LINE_BYTES);
544       memset (&cm->ts_stats[i], 0, sizeof (ioam_cache_ts_pool_stats_t));
545       tw_timer_wheel_init_16t_2w_512sl (&cm->timer_wheels[i],
546                                         expired_cache_ts_timer_callback,
547                                         IOAM_CACHE_TS_TICK
548                                         /* timer period 100ms */ ,
549                                         10e4);
550       cm->timer_wheels[i].last_run_time = vlib_time_now (vm);
551     }
552   ioam_cache_ts_timer_node_enable (vm, 1);
553   return (1);
554 }
555
556 always_inline void
557 ioam_cache_ts_timer_set (ioam_cache_main_t * cm,
558                          ioam_cache_ts_entry_t * entry, u32 interval)
559 {
560   entry->timer_handle
561     = tw_timer_start_16t_2w_512sl (&cm->timer_wheels[entry->pool_id],
562                                    entry->pool_index, 1, interval);
563 }
564
565 always_inline void
566 ioam_cache_ts_timer_reset (ioam_cache_main_t * cm,
567                            ioam_cache_ts_entry_t * entry)
568 {
569   tw_timer_stop_16t_2w_512sl (&cm->timer_wheels[entry->pool_id],
570                               entry->timer_handle);
571   entry->timer_handle = TIMER_HANDLE_INVALID;
572 }
573
574 inline static void
575 ioam_cache_ts_entry_free (u32 thread_id,
576                           ioam_cache_ts_entry_t * entry, u32 node_index)
577 {
578   ioam_cache_main_t *cm = &ioam_cache_main;
579   vlib_main_t *vm = cm->vlib_main;
580   vlib_frame_t *nf = 0;
581   u32 *to_next;
582
583   if (entry)
584     {
585       if (entry->hbh != 0)
586         {
587           nf = vlib_get_frame_to_node (vm, node_index);
588           nf->n_vectors = 0;
589           to_next = vlib_frame_vector_args (nf);
590           nf->n_vectors = 1;
591           to_next[0] = entry->buffer_index;
592           vlib_put_frame_to_node (vm, node_index, nf);
593         }
594       pool_put (cm->ioam_ts_pool[thread_id], entry);
595       cm->ts_stats[thread_id].inuse--;
596       memset (entry, 0, sizeof (*entry));
597     }
598 }
599
600 inline static int
601 ioam_cache_ts_table_destroy (vlib_main_t * vm)
602 {
603   ioam_cache_main_t *cm = &ioam_cache_main;
604   ioam_cache_ts_entry_t *entry = 0;
605   int no_of_threads = vec_len (vlib_worker_threads);
606   int i;
607
608   /* free pool and hash table */
609   for (i = 0; i < no_of_threads; i++)
610     {
611       pool_foreach (entry, cm->ioam_ts_pool[i], (
612                                                   {
613                                                   ioam_cache_ts_entry_free (i,
614                                                                             entry,
615                                                                             cm->error_node_index);
616                                                   }
617                     ));
618       pool_free (cm->ioam_ts_pool[i]);
619       cm->ioam_ts_pool = 0;
620       tw_timer_wheel_free_16t_2w_512sl (&cm->timer_wheels[i]);
621     }
622   vec_free (cm->ioam_ts_pool);
623   return (0);
624 }
625
626 inline static int
627 ioam_cache_ts_entry_cleanup (u32 thread_id, u32 pool_index)
628 {
629   ioam_cache_main_t *cm = &ioam_cache_main;
630   ioam_cache_ts_entry_t *entry = 0;
631
632   entry = pool_elt_at_index (cm->ioam_ts_pool[thread_id], pool_index);
633   ioam_cache_ts_entry_free (thread_id, entry, cm->error_node_index);
634   return (0);
635 }
636
637 /*
638  * Caches buffer for ioam SR tunnel select for Anycast service
639  */
640 inline static int
641 ioam_cache_ts_add (ip6_header_t * ip0,
642                    u16 src_port,
643                    u16 dst_port,
644                    u32 seq_no,
645                    u8 max_responses, u64 now, u32 thread_id, u32 * pool_index)
646 {
647   ioam_cache_main_t *cm = &ioam_cache_main;
648   ioam_cache_ts_entry_t *entry = 0;
649
650   if (cm->ts_stats[thread_id].inuse == MAX_CACHE_TS_ENTRIES)
651     {
652       cm->ts_stats[thread_id].add_failed++;
653       return (-1);
654     }
655
656   pool_get_aligned (cm->ioam_ts_pool[thread_id], entry,
657                     CLIB_CACHE_LINE_BYTES);
658   memset (entry, 0, sizeof (*entry));
659   *pool_index = entry - cm->ioam_ts_pool[thread_id];
660
661   clib_memcpy (entry->dst_address.as_u64, ip0->dst_address.as_u64,
662                sizeof (ip6_address_t));
663   clib_memcpy (entry->src_address.as_u64, ip0->src_address.as_u64,
664                sizeof (ip6_address_t));
665   entry->src_port = src_port;
666   entry->dst_port = dst_port;
667   entry->seq_no = seq_no;
668   entry->response_received = 0;
669   entry->max_responses = max_responses;
670   entry->created_at = now;
671   entry->hbh = 0;
672   entry->buffer_index = 0;
673   entry->pool_id = thread_id;
674   entry->pool_index = *pool_index;
675   ioam_cache_ts_timer_set (cm, entry, IOAM_CACHE_TS_TIMEOUT);
676   cm->ts_stats[thread_id].inuse++;
677   return (0);
678 }
679
680 inline static void
681 ioam_cache_ts_send (u32 thread_id, i32 pool_index)
682 {
683   ioam_cache_main_t *cm = &ioam_cache_main;
684   ioam_cache_ts_entry_t *entry = 0;
685
686   entry = pool_elt_at_index (cm->ioam_ts_pool[thread_id], pool_index);
687   if (!pool_is_free (cm->ioam_ts_pool[thread_id], entry) && entry)
688     {
689       /* send and free pool entry */
690       ioam_cache_ts_entry_free (thread_id, entry, cm->ip6_hbh_pop_node_index);
691     }
692 }
693
694 inline static void
695 ioam_cache_ts_check_and_send (u32 thread_id, i32 pool_index)
696 {
697   ioam_cache_main_t *cm = &ioam_cache_main;
698   ioam_cache_ts_entry_t *entry = 0;
699   entry = pool_elt_at_index (cm->ioam_ts_pool[thread_id], pool_index);
700   if (entry && entry->hbh)
701     {
702       if (entry->response_received == entry->max_responses ||
703           entry->created_at + IOAM_CACHE_TS_TIMEOUT <=
704           vlib_time_now (cm->vlib_main))
705         {
706           ioam_cache_ts_timer_reset (cm, entry);
707           ioam_cache_ts_send (thread_id, pool_index);
708         }
709     }
710 }
711
712 inline static int
713 ioam_cache_ts_update (u32 thread_id,
714                       i32 pool_index,
715                       u32 buffer_index, ip6_hop_by_hop_header_t * hbh)
716 {
717   ioam_cache_main_t *cm = &ioam_cache_main;
718   ioam_cache_ts_entry_t *entry = 0;
719   vlib_main_t *vm = cm->vlib_main;
720   vlib_frame_t *nf = 0;
721   u32 *to_next;
722
723   entry = pool_elt_at_index (cm->ioam_ts_pool[thread_id], pool_index);
724   if (!pool_is_free (cm->ioam_ts_pool[thread_id], entry) && entry)
725     {
726       /* drop existing buffer */
727       if (entry->hbh != 0)
728         {
729           nf = vlib_get_frame_to_node (vm, cm->error_node_index);
730           nf->n_vectors = 0;
731           to_next = vlib_frame_vector_args (nf);
732           nf->n_vectors = 1;
733           to_next[0] = entry->buffer_index;
734           vlib_put_frame_to_node (vm, cm->error_node_index, nf);
735         }
736       /* update */
737       entry->buffer_index = buffer_index;
738       entry->hbh = hbh;
739       /* check and send */
740       ioam_cache_ts_check_and_send (thread_id, pool_index);
741       return (0);
742     }
743   return (-1);
744 }
745
746 /*
747  * looks up the entry based on the e2e option pool index
748  * result = 0 found the entry
749  * result < 0 indicates failture to find an entry
750  */
751 inline static int
752 ioam_cache_ts_lookup (ip6_header_t * ip0,
753                       u8 protocol,
754                       u16 src_port,
755                       u16 dst_port,
756                       u32 seq_no,
757                       ip6_hop_by_hop_header_t ** hbh,
758                       u32 * pool_index, u8 * thread_id, u8 response_seen)
759 {
760   ioam_cache_main_t *cm = &ioam_cache_main;
761   ip6_hop_by_hop_header_t *hbh0 = 0;
762   ioam_e2e_cache_option_t *e2e = 0;
763
764   hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
765   e2e =
766     (ioam_e2e_cache_option_t *) ((u8 *) hbh0 + cm->rewrite_pool_index_offset);
767   if ((u8 *) e2e < ((u8 *) hbh0 + ((hbh0->length + 1) << 3))
768       && e2e->hdr.type == HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID)
769     {
770       ioam_cache_ts_entry_t *entry = 0;
771       *pool_index = e2e->pool_index;
772       *thread_id = e2e->pool_id;
773       entry = pool_elt_at_index (cm->ioam_ts_pool[*thread_id], *pool_index);
774       /* match */
775       if (entry &&
776           ip6_address_compare (&ip0->src_address, &entry->dst_address) == 0 &&
777           ip6_address_compare (&ip0->dst_address, &entry->src_address) == 0 &&
778           entry->src_port == dst_port &&
779           entry->dst_port == src_port && entry->seq_no == seq_no)
780         {
781           *hbh = entry->hbh;
782           entry->response_received += response_seen;
783           return (0);
784         }
785       else if (entry)
786         {
787           return (-1);
788         }
789     }
790   return (-1);
791 }
792
793 inline static u8 *
794 format_ioam_cache_ts_entry (u8 * s, va_list * args)
795 {
796   ioam_cache_ts_entry_t *e = va_arg (*args, ioam_cache_ts_entry_t *);
797   u32 thread_id = va_arg (*args, u32);
798   ioam_cache_main_t *cm = &ioam_cache_main;
799   ioam_e2e_id_option_t *e2e = 0;
800   vlib_main_t *vm = cm->vlib_main;
801   clib_time_t *ct = &vm->clib_time;
802
803   if (!e)
804     goto end;
805
806   if (e->hbh)
807     {
808       e2e =
809         ip6_ioam_find_hbh_option (e->hbh,
810                                   HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID);
811
812       s =
813         format (s,
814                 "%d: %U:%d to  %U:%d seq_no %u buffer %u %U \n\t\tCreated at %U Received %d\n",
815                 (e - cm->ioam_ts_pool[thread_id]), format_ip6_address,
816                 &e->src_address, e->src_port, format_ip6_address,
817                 &e->dst_address, e->dst_port, e->seq_no, e->buffer_index,
818                 format_ip6_address, e2e ? &e2e->id : 0, format_time_interval,
819                 "h:m:s:u",
820                 (e->created_at -
821                  vm->cpu_time_main_loop_start) * ct->seconds_per_clock,
822                 e->response_received);
823     }
824   else
825     {
826       s =
827         format (s,
828                 "%d: %U:%d to  %U:%d seq_no %u Buffer %u \n\t\tCreated at %U Received %d\n",
829                 (e - cm->ioam_ts_pool[thread_id]), format_ip6_address,
830                 &e->src_address, e->src_port, format_ip6_address,
831                 &e->dst_address, e->dst_port, e->seq_no, e->buffer_index,
832                 format_time_interval, "h:m:s:u",
833                 (e->created_at -
834                  vm->cpu_time_main_loop_start) * ct->seconds_per_clock,
835                 e->response_received);
836     }
837
838 end:
839   return s;
840 }
841
842 /*
843  * Get extended rewrite string for iOAM data in v6
844  * This makes space for an e2e options to carry cache pool info
845  * and manycast server address.
846  * It set the rewrite string per configs in ioam ip6 + new option
847  * for cache along with offset to the option to populate cache
848  * pool id and index
849  */
850 static inline int
851 ip6_ioam_ts_cache_set_rewrite (void)
852 {
853   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
854   ioam_cache_main_t *cm = &ioam_cache_main;
855   ip6_hop_by_hop_header_t *hbh;
856   u32 rewrite_len = 0;
857   ioam_e2e_cache_option_t *e2e = 0;
858   ioam_e2e_id_option_t *e2e_id = 0;
859
860   vec_free (cm->rewrite);
861   ip6_ioam_set_rewrite (&(cm->rewrite), hm->has_trace_option,
862                         hm->has_pot_option, hm->has_seqno_option);
863   hbh = (ip6_hop_by_hop_header_t *) cm->rewrite;
864   rewrite_len = ((hbh->length + 1) << 3);
865   vec_validate (cm->rewrite,
866                 rewrite_len - 1 + IOAM_E2E_CACHE_OPTION_RND +
867                 IOAM_E2E_ID_OPTION_RND);
868   hbh = (ip6_hop_by_hop_header_t *) cm->rewrite;
869   /* setup e2e id option to insert pool id and index of the node caching it */
870   hbh->length += IOAM_E2E_CACHE_HBH_EXT_LEN + IOAM_E2E_ID_HBH_EXT_LEN;
871   cm->rewrite_pool_index_offset = rewrite_len;
872   e2e = (ioam_e2e_cache_option_t *) (cm->rewrite + rewrite_len);
873   e2e->hdr.type = HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID
874     | HBH_OPTION_TYPE_SKIP_UNKNOWN;
875   e2e->hdr.length = sizeof (ioam_e2e_cache_option_t) -
876     sizeof (ip6_hop_by_hop_option_t);
877   e2e->e2e_type = 2;
878   e2e_id =
879     (ioam_e2e_id_option_t *) ((u8 *) e2e + sizeof (ioam_e2e_cache_option_t));
880   e2e_id->hdr.type =
881     HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID | HBH_OPTION_TYPE_SKIP_UNKNOWN;
882   e2e_id->hdr.length =
883     sizeof (ioam_e2e_id_option_t) - sizeof (ip6_hop_by_hop_option_t);
884   e2e_id->e2e_type = 1;
885
886   return (0);
887 }
888
889 static inline int
890 ip6_ioam_ts_cache_cleanup_rewrite (void)
891 {
892   ioam_cache_main_t *cm = &ioam_cache_main;
893
894   vec_free (cm->rewrite);
895   cm->rewrite = 0;
896   cm->rewrite_pool_index_offset = 0;
897   return (0);
898 }
899 #endif /* __included_ioam_cache_h__ */
900
901 /*
902  * fd.io coding-style-patch-verification: ON
903  *
904  * Local Variables:
905  * eval: (c-set-style "gnu")
906  * End:
907  */