misc: add tracedump API plugin
[vpp.git] / src / plugins / tracedump / tracedump_test.c
1 /*
2  * tracedump.c - tracedump vpp-api-test plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21 #include <vnet/api_errno.h>
22 #include <stdbool.h>
23
24 #define __plugin_msg_base tracedump_test_main.msg_id_base
25 #include <vlibapi/vat_helper_macros.h>
26
27 /* Declare message IDs */
28 #include <tracedump/tracedump.api_enum.h>
29 #include <tracedump/tracedump.api_types.h>
30
31 typedef struct
32 {
33   /* API message ID base */
34   u16 msg_id_base;
35   vat_main_t *vat_main;
36 } tracedump_test_main_t;
37
38 tracedump_test_main_t tracedump_test_main;
39
40 static void
41 vl_api_trace_details_t_handler (vl_api_trace_details_t * dmp)
42 {
43   u32 thread_id, position;
44
45   thread_id = clib_net_to_host_u32 (dmp->thread_id);
46   position = clib_net_to_host_u32 (dmp->position);
47   fformat
48     (stdout,
49      "thread %d position %d more_this_thread %d more_threads %d done %d\n",
50      thread_id, position, (u32) dmp->more_this_thread,
51      (u32) dmp->more_threads, (u32) dmp->done);
52   fformat (stdout, "  %U\n", vl_api_format_string, (&dmp->trace_data));
53 }
54
55
56 static void
57 vl_api_trace_dump_reply_t_handler (vl_api_trace_dump_reply_t * rmp)
58 {
59   tracedump_test_main_t *ttm = &tracedump_test_main;
60   vat_main_t *vam = ttm->vat_main;
61   vl_api_trace_dump_t *mp;
62   i32 retval = (i32) clib_net_to_host_u32 (rmp->retval);
63   u32 thread_id, position;
64
65   if (retval != 0 || rmp->done)
66     {
67       vam->result_ready = 1;
68       vam->retval = retval;
69
70       /* Clear the cache */
71       if (retval == 0 && rmp->flush_only == 0)
72         {
73           M (TRACE_DUMP, mp);
74           mp->clear_cache = 1;
75           mp->thread_id = 0xFFFFFFFF;
76           mp->position = 0xFFFFFFFF;
77           S (mp);
78         }
79       return;
80     }
81
82   /* Figure out where the next batch starts */
83   thread_id = clib_host_to_net_u32 (rmp->last_thread_id);
84   position = clib_host_to_net_u32 (rmp->last_position);
85
86   if (rmp->more_threads)
87     {
88       position = 0;
89       thread_id++;
90     }
91   else
92     position++;
93
94   M (TRACE_DUMP, mp);
95   mp->clear_cache = 0;
96   mp->thread_id = clib_host_to_net_u32 (thread_id);
97   mp->position = clib_host_to_net_u32 (position);
98   mp->max_records = clib_host_to_net_u32 (10);
99   S (mp);
100 }
101
102 static int
103 api_trace_dump (vat_main_t * vam)
104 {
105   vl_api_trace_dump_t *mp;
106   int ret;
107
108   M (TRACE_DUMP, mp);
109   mp->clear_cache = 1;
110   mp->thread_id = 0;
111   mp->position = 0;
112   mp->max_records = clib_host_to_net_u32 (10);
113
114   S (mp);
115
116   W (ret);
117   return ret;
118 }
119
120 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
121 #define vl_endianfun
122 #include <tracedump/tracedump.api.h>
123 #undef vl_endianfun
124 #define vl_printfun
125 #include <tracedump/tracedump.api.h>
126 #undef vl_printfun
127
128 void
129 manual_setup_message_id_table (vat_main_t * vam)
130 {
131   vl_msg_api_set_handlers (VL_API_TRACE_DETAILS
132                            + tracedump_test_main.msg_id_base, "trace_details",
133                            vl_api_trace_details_t_handler, vl_noop_handler,
134                            vl_api_trace_details_t_endian,
135                            vl_api_trace_details_t_print,
136                            sizeof (vl_api_trace_details_t), 1);
137 }
138
139 #define VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE manual_setup_message_id_table
140 #define VL_API_TRACE_DUMP_REPLY_T_HANDLER
141
142 #include <tracedump/tracedump.api_test.c>
143
144 /*
145  * fd.io coding-style-patch-verification: ON
146  *
147  * Local Variables:
148  * eval: (c-set-style "gnu")
149  * End:
150  */