misc: Purge unused pg includes
[vpp.git] / src / plugins / ioam / ip6 / ioam_cache_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  * This file implements caching of ioam header and reattaching
17  * it in response message by performing request-response matching.
18  *  Works for TCP SYN/SYN-ACK.
19  * This feature is used for anycast server selection.
20  * ioam data thus cached is used to measure and get complete round trip
21  * network path to help in server selection.
22  * There are 2 graph nodes defined to :
23  * 1. process packets that contain iOAM header and cache it
24  * 2. process TCP SYN-ACKs and reattach ioam header from the
25  *    cache corresponding to TCP-SYN
26  * These graph nodes are attached to the vnet graph based on
27  * ioam cache and classifier configs.
28  * e.g.
29  * If db06::06 is the anycast service IP6 address:
30  *
31  * set ioam ip6 cache
32  *
33  * Apply this classifier on interface where requests for anycast service are received:
34  * classify session acl-hit-next ip6-node ip6-lookup table-index 0 match l3 ip6 dst db06::06
35  *    ioam-decap anycast <<< ioam-decap is hooked to cache when set ioam ip6 cache is enabled
36  *
37  * Apply this classifier on interface where responses from anycast service are received:
38  * classify session acl-hit-next ip6-node ip6-add-from-cache-hop-by-hop table-index 0 match l3
39  *    ip6 src db06::06 ioam-encap anycast-response
40  *
41  */
42 #include <vlib/vlib.h>
43 #include <vnet/vnet.h>
44 #include <vppinfra/error.h>
45 #include <vnet/ip/ip.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_trace_t;
55
56 /* packet trace format function */
57 static u8 *
58 format_cache_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_trace_t *t = va_arg (*args, cache_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_error \
70 _(RECORDED, "ip6 iOAM headers cached")
71
72 typedef enum
73 {
74 #define _(sym,str) CACHE_ERROR_##sym,
75   foreach_cache_error
76 #undef _
77     CACHE_N_ERROR,
78 } cache_error_t;
79
80 static char *cache_error_strings[] = {
81 #define _(sym,string) string,
82   foreach_cache_error
83 #undef _
84 };
85
86 typedef enum
87 {
88   IOAM_CACHE_NEXT_POP_HBYH,
89   IOAM_CACHE_N_NEXT,
90 } cache_next_t;
91
92 static uword
93 ip6_ioam_cache_node_fn (vlib_main_t * vm,
94                         vlib_node_runtime_t * node, vlib_frame_t * frame)
95 {
96   u32 n_left_from, *from, *to_next;
97   cache_next_t next_index;
98   u32 recorded = 0;
99
100   from = vlib_frame_vector_args (frame);
101   n_left_from = frame->n_vectors;
102   next_index = node->cached_next_index;
103
104   while (n_left_from > 0)
105     {
106       u32 n_left_to_next;
107
108       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
109       // TODO: Dual loop
110       while (n_left_from > 0 && n_left_to_next > 0)
111         {
112           u32 bi0;
113           vlib_buffer_t *p0;
114           u32 next0 = IOAM_CACHE_NEXT_POP_HBYH;
115           ip6_header_t *ip0;
116           ip6_hop_by_hop_header_t *hbh0;
117           tcp_header_t *tcp0;
118           u32 tcp_offset0;
119
120           /* speculatively enqueue p0 to the current next frame */
121           bi0 = from[0];
122           to_next[0] = bi0;
123           from += 1;
124           to_next += 1;
125           n_left_from -= 1;
126           n_left_to_next -= 1;
127
128           p0 = vlib_get_buffer (vm, bi0);
129           ip0 = vlib_buffer_get_current (p0);
130           if (IP_PROTOCOL_TCP ==
131               ip6_locate_header (p0, ip0, IP_PROTOCOL_TCP, &tcp_offset0))
132             {
133               tcp0 = (tcp_header_t *) ((u8 *) ip0 + tcp_offset0);
134               if ((tcp0->flags & TCP_FLAG_SYN) == TCP_FLAG_SYN &&
135                   (tcp0->flags & TCP_FLAG_ACK) == 0)
136                 {
137                   /* Cache the ioam hbh header */
138                   hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
139                   if (0 == ioam_cache_add (p0,
140                                            ip0,
141                                            clib_net_to_host_u16
142                                            (tcp0->src_port),
143                                            clib_net_to_host_u16
144                                            (tcp0->dst_port), hbh0,
145                                            clib_net_to_host_u32
146                                            (tcp0->seq_number) + 1))
147                     {
148                       recorded++;
149                     }
150                 }
151             }
152           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
153             {
154               if (p0->flags & VLIB_BUFFER_IS_TRACED)
155                 {
156                   cache_trace_t *t =
157                     vlib_add_trace (vm, node, p0, sizeof (*t));
158                   t->flow_label =
159                     clib_net_to_host_u32
160                     (ip0->ip_version_traffic_class_and_flow_label);
161                   t->next_index = next0;
162                 }
163             }
164           /* verify speculative enqueue, maybe switch current next frame */
165           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
166                                            to_next, n_left_to_next,
167                                            bi0, next0);
168         }
169
170       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
171     }
172
173   vlib_node_increment_counter (vm, ioam_cache_node.index,
174                                CACHE_ERROR_RECORDED, recorded);
175   return frame->n_vectors;
176 }
177
178 /*
179  * Node for IP6 iOAM header cache
180  */
181 /* *INDENT-OFF* */
182 VLIB_REGISTER_NODE (ioam_cache_node) =
183 {
184   .function = ip6_ioam_cache_node_fn,
185   .name = "ip6-ioam-cache",
186   .vector_size = sizeof (u32),
187   .format_trace = format_cache_trace,
188   .type = VLIB_NODE_TYPE_INTERNAL,
189   .n_errors = ARRAY_LEN (cache_error_strings),
190   .error_strings = cache_error_strings,
191   .n_next_nodes = IOAM_CACHE_N_NEXT,
192   /* edit / add dispositions here */
193   .next_nodes =
194   {
195     [IOAM_CACHE_NEXT_POP_HBYH] = "ip6-pop-hop-by-hop"
196   },
197 };
198 /* *INDENT-ON* */
199
200 typedef struct
201 {
202   u32 next_index;
203 } ip6_add_from_cache_hbh_trace_t;
204
205 /* packet trace format function */
206 static u8 *
207 format_ip6_add_from_cache_hbh_trace (u8 * s, va_list * args)
208 {
209   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
210   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
211   ip6_add_from_cache_hbh_trace_t *t = va_arg (*args,
212                                               ip6_add_from_cache_hbh_trace_t
213                                               *);
214
215   s = format (s, "IP6_ADD_FROM_CACHE_HBH: next index %d", t->next_index);
216   return s;
217 }
218
219 #define foreach_ip6_add_from_cache_hbh_error \
220 _(PROCESSED, "Pkts w/ added ip6 hop-by-hop options")
221
222 typedef enum
223 {
224 #define _(sym,str) IP6_ADD_FROM_CACHE_HBH_ERROR_##sym,
225   foreach_ip6_add_from_cache_hbh_error
226 #undef _
227     IP6_ADD_FROM_CACHE_HBH_N_ERROR,
228 } ip6_add_from_cache_hbh_error_t;
229
230 static char *ip6_add_from_cache_hbh_error_strings[] = {
231 #define _(sym,string) string,
232   foreach_ip6_add_from_cache_hbh_error
233 #undef _
234 };
235
236 #define foreach_ip6_ioam_cache_input_next        \
237   _(IP6_LOOKUP, "ip6-lookup")                   \
238   _(DROP, "error-drop")
239
240 typedef enum
241 {
242 #define _(s,n) IP6_IOAM_CACHE_INPUT_NEXT_##s,
243   foreach_ip6_ioam_cache_input_next
244 #undef _
245     IP6_IOAM_CACHE_INPUT_N_NEXT,
246 } ip6_ioam_cache_input_next_t;
247
248
249 VLIB_NODE_FN (ip6_add_from_cache_hbh_node) (vlib_main_t * vm,
250                                             vlib_node_runtime_t * node,
251                                             vlib_frame_t * frame)
252 {
253   ioam_cache_main_t *cm = &ioam_cache_main;
254   u32 n_left_from, *from, *to_next;
255   ip_lookup_next_t next_index;
256   u32 processed = 0;
257   u8 *rewrite = 0;
258   u32 rewrite_len = 0;
259   u32 sr_rewrite_len = vec_len (cm->sr_rewrite_template);
260
261   from = vlib_frame_vector_args (frame);
262   n_left_from = frame->n_vectors;
263   next_index = node->cached_next_index;
264
265   while (n_left_from > 0)
266     {
267       u32 n_left_to_next;
268
269       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
270       // TODO: Dual loop
271       while (n_left_from > 0 && n_left_to_next > 0)
272         {
273           u32 bi0;
274           vlib_buffer_t *b0;
275           u32 next0;
276           ip6_header_t *ip0;
277           ip6_hop_by_hop_header_t *hbh0;
278           ip6_sr_header_t *srh0 = 0;
279           u64 *copy_src0, *copy_dst0;
280           u16 new_l0;
281           tcp_header_t *tcp0;
282           u32 tcp_offset0;
283           ioam_cache_entry_t *entry = 0;
284
285           next0 = IP6_IOAM_CACHE_INPUT_NEXT_IP6_LOOKUP;
286           /* speculatively enqueue b0 to the current next frame */
287           bi0 = from[0];
288           to_next[0] = bi0;
289           from += 1;
290           to_next += 1;
291           n_left_from -= 1;
292           n_left_to_next -= 1;
293
294           b0 = vlib_get_buffer (vm, bi0);
295
296           ip0 = vlib_buffer_get_current (b0);
297           if (IP_PROTOCOL_TCP !=
298               ip6_locate_header (b0, ip0, IP_PROTOCOL_TCP, &tcp_offset0))
299             {
300               goto TRACE0;
301             }
302           tcp0 = (tcp_header_t *) ((u8 *) ip0 + tcp_offset0);
303           if (((tcp0->flags & TCP_FLAG_SYN) == TCP_FLAG_SYN &&
304                (tcp0->flags & TCP_FLAG_ACK) == TCP_FLAG_ACK) ||
305               (tcp0->flags & TCP_FLAG_RST) == TCP_FLAG_RST)
306             {
307               if (0 != (entry = ioam_cache_lookup (ip0,
308                                                    clib_net_to_host_u16
309                                                    (tcp0->src_port),
310                                                    clib_net_to_host_u16
311                                                    (tcp0->dst_port),
312                                                    clib_net_to_host_u32
313                                                    (tcp0->ack_number))))
314                 {
315                   rewrite = entry->ioam_rewrite_string;
316                   rewrite_len = vec_len (rewrite);
317                 }
318               else
319                 {
320                   next0 = IP6_IOAM_CACHE_INPUT_NEXT_DROP;
321                   goto TRACE0;
322                 }
323             }
324           else
325             goto TRACE0;
326
327
328           /* Copy the ip header left by the required amount */
329           copy_dst0 = (u64 *) (((u8 *) ip0) - (rewrite_len + sr_rewrite_len));
330           copy_src0 = (u64 *) ip0;
331
332           copy_dst0[0] = copy_src0[0];
333           copy_dst0[1] = copy_src0[1];
334           copy_dst0[2] = copy_src0[2];
335           copy_dst0[3] = copy_src0[3];
336           copy_dst0[4] = copy_src0[4];
337           vlib_buffer_advance (b0, -(word) (rewrite_len + sr_rewrite_len));
338           ip0 = vlib_buffer_get_current (b0);
339
340           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
341           srh0 = (ip6_sr_header_t *) ((u8 *) hbh0 + rewrite_len);
342           /* $$$ tune, rewrite_len is a multiple of 8 */
343           clib_memcpy_fast (hbh0, rewrite, rewrite_len);
344           clib_memcpy_fast (srh0, cm->sr_rewrite_template, sr_rewrite_len);
345           /* Copy dst address into the DA slot in the segment list */
346           clib_memcpy_fast (srh0->segments, ip0->dst_address.as_u64,
347                             sizeof (ip6_address_t));
348           /* Rewrite the ip6 dst address with the first hop */
349           clib_memcpy_fast (ip0->dst_address.as_u64, entry->next_hop.as_u64,
350                             sizeof (ip6_address_t));
351           clib_memcpy_fast (&srh0->segments[1],
352                             (u8 *) hbh0 + entry->my_address_offset,
353                             sizeof (ip6_address_t));
354           ioam_cache_entry_free (entry);
355
356           /* Patch the protocol chain, insert the h-b-h (type 0) header */
357           srh0->protocol = ip0->protocol;
358           hbh0->protocol = IPPROTO_IPV6_ROUTE;
359           ip0->protocol = 0;
360           new_l0 =
361             clib_net_to_host_u16 (ip0->payload_length) + rewrite_len +
362             sr_rewrite_len;
363           ip0->payload_length = clib_host_to_net_u16 (new_l0);
364           processed++;
365         TRACE0:
366           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
367                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
368             {
369               ip6_add_from_cache_hbh_trace_t *t =
370                 vlib_add_trace (vm, node, b0, sizeof (*t));
371               t->next_index = next0;
372             }
373
374           /* verify speculative enqueue, maybe switch current next frame */
375           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
376                                            to_next, n_left_to_next,
377                                            bi0, next0);
378         }
379
380       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
381     }
382
383   vlib_node_increment_counter (vm, cm->ip6_add_from_cache_hbh_node_index,
384                                IP6_ADD_FROM_CACHE_HBH_ERROR_PROCESSED,
385                                processed);
386   return frame->n_vectors;
387 }
388 /* *INDENT-OFF* */
389 VLIB_REGISTER_NODE (ip6_add_from_cache_hbh_node) =
390 {
391   .name = "ip6-add-from-cache-hop-by-hop",
392   .vector_size = sizeof (u32),
393   .format_trace = format_ip6_add_from_cache_hbh_trace,
394   .type = VLIB_NODE_TYPE_INTERNAL,
395   .n_errors = ARRAY_LEN (ip6_add_from_cache_hbh_error_strings),
396   .error_strings =  ip6_add_from_cache_hbh_error_strings,
397   /* See ip/lookup.h */
398   .n_next_nodes = IP6_IOAM_CACHE_INPUT_N_NEXT,
399   .next_nodes =
400   {
401 #define _(s,n) [IP6_IOAM_CACHE_INPUT_NEXT_##s] = n,
402     foreach_ip6_ioam_cache_input_next
403 #undef _
404   },
405 };
406 /* *INDENT-ON* */
407
408 /*
409  * fd.io coding-style-patch-verification: ON
410  *
411  * Local Variables:
412  * eval: (c-set-style "gnu")
413  * End:
414  */