Repair Doxygen build infrastructure
[vpp.git] / src / plugins / ioam / encap / ip6_ioam_e2e.c
1 /*
2  * Copyright (c) 2016 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 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vnet/pg/pg.h>
19 #include <vppinfra/error.h>
20
21 #include <vnet/ip/ip.h>
22
23 #include <vppinfra/hash.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/elog.h>
26
27 #include <vnet/ip/ip6_hop_by_hop.h>
28 #include <vnet/plugin/plugin.h>
29
30 #include "ip6_ioam_e2e.h"
31
32 ioam_e2e_main_t ioam_e2e_main;
33
34 static u8 * ioam_e2e_trace_handler (u8 * s,
35                                     ip6_hop_by_hop_option_t *opt)
36 {
37   ioam_e2e_option_t * e2e = (ioam_e2e_option_t *)opt;
38   u32 seqno = 0;
39
40   if (e2e)
41     {
42       seqno = clib_net_to_host_u32 (e2e->e2e_data);
43     }
44
45   s = format (s, "SeqNo = 0x%Lx", seqno);
46   return s;
47 }
48
49 int 
50 ioam_e2e_config_handler (void *data, u8 disable)
51 {
52   int *analyse = data;
53
54   /* Register hanlders if enabled */
55   if (!disable)
56     {
57       /* If encap node register for encap handler */
58       if (0 == *analyse)
59         {
60           if (ip6_hbh_register_option(HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE,
61                                       ioam_seqno_encap_handler,
62                                       ioam_e2e_trace_handler) < 0)
63             {
64               return (-1);
65             }
66         }
67       /* If analyze node then register for decap handler */
68       else
69         {
70           if (ip6_hbh_pop_register_option(HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE,
71                                           ioam_seqno_decap_handler) < 0)
72             {
73               return (-1);
74             }
75         }
76       return 0;
77     }
78
79   /* UnRegister handlers */
80   (void) ip6_hbh_unregister_option(HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE);
81   (void) ip6_hbh_pop_unregister_option(HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE);
82   return 0;
83 }
84
85 int
86 ioam_e2e_rewrite_handler (u8 *rewrite_string,
87                           u8 *rewrite_size)
88 {
89   ioam_e2e_option_t *e2e_option;
90
91   if (rewrite_string && *rewrite_size == sizeof(ioam_e2e_option_t))
92     {
93       e2e_option = (ioam_e2e_option_t *)rewrite_string;
94       e2e_option->hdr.type = HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE
95           | HBH_OPTION_TYPE_SKIP_UNKNOWN;
96       e2e_option->hdr.length = sizeof (ioam_e2e_option_t) -
97           sizeof (ip6_hop_by_hop_option_t);
98       return(0);
99     }
100   return(-1);
101 }
102
103 u32
104 ioam_e2e_flow_handler (u32 ctx, u8 add)
105 {
106   ioam_e2e_data_t *data;
107   u16 i;
108
109   if (add)
110     {
111       pool_get(ioam_e2e_main.e2e_data, data);
112       data->flow_ctx =  ctx;
113       ioam_seqno_init_bitmap(&data->seqno_data);
114       return ((u32) (data - ioam_e2e_main.e2e_data));
115     }
116
117   /* Delete case */
118   for (i = 0; i < vec_len(ioam_e2e_main.e2e_data); i++)
119     {
120       if (pool_is_free_index(ioam_e2e_main.e2e_data, i))
121         continue;
122
123       data = pool_elt_at_index(ioam_e2e_main.e2e_data, i);
124       if (data && (data->flow_ctx == ctx))
125         {
126           pool_put_index(ioam_e2e_main.e2e_data, i);
127           return (0);
128         }
129     }
130   return 0;
131 }
132
133 static clib_error_t *
134 ioam_show_e2e_cmd_fn (vlib_main_t * vm,
135                       unformat_input_t * input,
136                       vlib_cli_command_t * cmd)
137 {
138   ioam_e2e_data_t *e2e_data;
139   u8 *s = 0;
140   int i;
141
142   vec_reset_length(s);
143
144   s = format(0, "IOAM E2E information: \n");
145   for (i = 0; i < vec_len(ioam_e2e_main.e2e_data); i++)
146     {
147       if (pool_is_free_index(ioam_e2e_main.e2e_data, i))
148         continue;
149
150       e2e_data = pool_elt_at_index(ioam_e2e_main.e2e_data, i);
151       s = format(s, "Flow name: %s\n", get_flow_name_from_flow_ctx(e2e_data->flow_ctx));
152
153       s = show_ioam_seqno_cmd_fn(s,
154                                  &e2e_data->seqno_data,
155                                  !IOAM_DEAP_ENABLED(e2e_data->flow_ctx));
156     }
157
158   vlib_cli_output(vm, "%v", s);
159   return 0;
160 }
161
162
163 VLIB_CLI_COMMAND (ioam_show_e2e_cmd, static) = {
164     .path = "show ioam e2e ",
165     .short_help = "show ioam e2e information",
166     .function = ioam_show_e2e_cmd_fn,
167 };
168
169 /*
170  * This routine exists to convince the vlib plugin framework that
171  * we haven't accidentally copied a random .dll into the plugin directory.
172  *
173  * Also collects global variable pointers passed from the vpp engine
174  */
175 clib_error_t *
176 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
177                       int from_early_init)
178 {
179   clib_error_t * error = 0;
180
181   ioam_e2e_main.vlib_main = vm;
182   ioam_e2e_main.vnet_main = h->vnet_main;
183   return error;
184 }
185
186 /*
187  * Init handler E2E headet handling.
188  * Init hanlder registers encap, decap, trace and Rewrite handlers.
189  */
190 static clib_error_t *
191 ioam_e2e_init (vlib_main_t * vm)
192 {
193   clib_error_t * error;
194
195   if ((error = vlib_call_init_function (vm, ip6_hop_by_hop_ioam_init)))
196     {
197       return(error);
198     }
199
200   /*
201    * As of now we have only PPC under E2E header.
202    */
203   if (ip6_hbh_config_handler_register(HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE,
204                                       ioam_e2e_config_handler) < 0)
205     {
206       return (clib_error_create("Registration of "
207           "HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE for rewrite failed"));
208     }
209
210   if (ip6_hbh_add_register_option(HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE,
211                                   sizeof(ioam_e2e_option_t),
212                                   ioam_e2e_rewrite_handler) < 0)
213     {
214       return (clib_error_create("Registration of "
215           "HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE for rewrite failed"));
216     }
217
218   if (ip6_hbh_flow_handler_register(HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE,
219                                     ioam_e2e_flow_handler) < 0)
220     {
221       return (clib_error_create("Registration of "
222           "HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE Flow handler failed"));
223     }
224
225   return (0);
226 }
227
228 /*
229  * Init function for the E2E lib.
230  * ip6_hop_by_hop_ioam_e2e_init gets called during init.
231  */
232 VLIB_INIT_FUNCTION (ioam_e2e_init);