http_static: fix reply data leak
[vpp.git] / src / plugins / ioam / lib-trace / trace_util.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 #include <vnet/vnet.h>
16 #include <stdint.h>
17 #include <time.h>
18 #include <string.h>
19 #include <vppinfra/mem.h>
20 #include "trace_util.h"
21 #include "trace_config.h"
22
23 __clib_export trace_main_t trace_main;
24
25 static int
26 trace_profile_cleanup (trace_profile * profile)
27 {
28
29   clib_memset (profile, 0, sizeof (trace_profile));
30   profile->trace_tsp = TSP_MICROSECONDS;        /* Micro seconds */
31   ip6_trace_profile_cleanup (); /* lib-trace_TODO: Remove this once IOAM-IPv6 transport is a plugin */
32   return 0;
33
34 }
35
36 static int
37 trace_main_profiles_reset (void)
38 {
39   int rv;
40
41   trace_main_t *sm = &trace_main;
42   rv = trace_profile_cleanup (&(sm->profile));
43   return (rv);
44 }
45
46 int
47 trace_util_init (void)
48 {
49   int rv;
50
51   rv = trace_main_profiles_reset ();
52   return (rv);
53 }
54
55
56 int
57 trace_profile_create (trace_profile * profile, u8 trace_type, u8 num_elts,
58                       u32 trace_tsp, u32 node_id, u32 app_data)
59 {
60
61   if (!trace_type || !num_elts || !(node_id))
62     {
63       return (-1);
64     }
65   if (profile && !profile->valid)
66     {
67       //rv = trace_profile_cleanup (profile);
68       profile->trace_type = trace_type;
69       profile->num_elts = num_elts;
70       profile->trace_tsp = trace_tsp;
71       profile->node_id = node_id;
72       profile->app_data = app_data;
73       profile->valid = 1;
74
75       /* lib-trace_TODO: Remove this once IOAM-IPv6 transport is a plugin */
76       ip6_trace_profile_setup ();
77       return (0);
78     }
79
80   return (-1);
81 }
82
83
84
85 clib_error_t *
86 clear_trace_profile_command_fn (vlib_main_t * vm,
87                                 unformat_input_t * input,
88                                 vlib_cli_command_t * cmd)
89 {
90
91   trace_main_profiles_reset ();
92   return 0;
93 }
94
95 void
96 clear_trace_profiles (void)
97 {
98   clear_trace_profile_command_fn (0, 0, 0);
99 }
100
101 VLIB_CLI_COMMAND(clear_trace_profile_command) =
102 {
103 .path = "clear ioam-trace profile",
104 .short_help = "clear ioam-trace profile [<index>|all]",
105 .function = clear_trace_profile_command_fn,
106 };
107
108 static clib_error_t *
109 set_trace_profile_command_fn (vlib_main_t * vm,
110                               unformat_input_t * input,
111                               vlib_cli_command_t * cmd)
112 {
113   u8 trace_type = 0;
114   u8 num_elts = 0;
115   u32 node_id = 0;
116   u32 app_data = 0;
117   u32 trace_tsp = 0;
118   trace_profile *profile = NULL;
119   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
120     {
121       if (unformat (input, "trace-type 0x%x", &trace_type));
122       else if (unformat (input, "trace-elts %d", &num_elts));
123       else if (unformat (input, "trace-tsp %d", &trace_tsp));
124       else if (unformat (input, "node-id 0x%x", &node_id));
125       else if (unformat (input, "app-data 0x%x", &app_data));
126       else
127         break;
128     }
129   profile = trace_profile_find ();
130   if (profile)
131     {
132       trace_profile_create (profile, trace_type, num_elts, trace_tsp,
133                             node_id, app_data);
134     }
135   return 0;
136 }
137
138 VLIB_CLI_COMMAND (set_trace_profile_command, static) =
139 {
140 .path = "set ioam-trace profile",
141 .short_help = "set ioam-trace \
142              trace-type <0x1f|0x3|0x9|0x11|0x19> trace-elts <nn> trace-tsp <0|1|2|3> \
143              node-id <node id in hex> app-data <app_data in hex>",
144 .function = set_trace_profile_command_fn,
145 };
146
147 static clib_error_t *
148 show_trace_profile_command_fn (vlib_main_t * vm,
149                                unformat_input_t * input,
150                                vlib_cli_command_t * cmd)
151 {
152   trace_profile *p = NULL;
153   u8 *s = 0;
154   p = trace_profile_find ();
155   if (!(p && p->valid))
156     {
157       s = format (s, "\nTrace configuration not valid\n");
158       vlib_cli_output (vm, "%v", s);
159       vec_free (s);
160       return 0;
161     }
162   s = format (s, " HOP BY HOP OPTIONS - TRACE CONFIG - \n");
163   s = format (s, "                        Trace Type : 0x%x (%d)\n",
164               p->trace_type, p->trace_type);
165   s =
166     format (s, "         Trace timestamp precision : %d (%s)\n",
167             p->trace_tsp,
168             (p->trace_tsp ==
169              TSP_SECONDS) ? "Seconds" : ((p->trace_tsp ==
170                                           TSP_MILLISECONDS) ?
171                                          "Milliseconds"
172                                          : (((p->trace_tsp ==
173                                               TSP_MICROSECONDS) ?
174                                              "Microseconds" :
175                                              "Nanoseconds"))));
176   s = format (s, "                Num of trace nodes : %d\n", p->num_elts);
177   s =
178     format (s, "                           Node-id : 0x%x (%d)\n",
179             p->node_id, p->node_id);
180   s =
181     format (s, "                          App Data : 0x%x (%d)\n",
182             p->app_data, p->app_data);
183   vlib_cli_output (vm, "%v", s);
184   vec_free (s);
185   return 0;
186 }
187
188 VLIB_CLI_COMMAND (show_trace_profile_command, static) =
189 {
190 .path = "show ioam-trace profile",
191 .short_help = "show ioam-trace profile",
192 .function = show_trace_profile_command_fn,
193 };
194
195 /*
196  * fd.io coding-style-patch-verification: ON
197  *
198  * Local Variables:
199  * eval: (c-set-style "gnu")
200  * End:
201  */