2 * tracedump.c - skeleton vpp engine plug-in
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <tracedump/tracedump.h>
21 #include <vlib/trace.h>
23 #include <vlibapi/api.h>
24 #include <vlibmemory/api.h>
25 #include <vpp/app/version.h>
28 #include <tracedump/tracedump.api_enum.h>
29 #include <tracedump/tracedump.api_types.h>
31 #define REPLY_MSG_ID_BASE tdmp->msg_id_base
32 #include <vlibapi/api_helper_macros.h>
34 tracedump_main_t tracedump_main;
37 trace_cmp (void *a1, void *a2)
39 vlib_trace_header_t **t1 = a1;
40 vlib_trace_header_t **t2 = a2;
41 i64 dt = t1[0]->time - t2[0]->time;
42 return dt < 0 ? -1 : (dt > 0 ? +1 : 0);
46 toss_client_cache (tracedump_main_t * tdmp, u32 client_index,
47 vlib_trace_header_t *** client_trace_cache)
49 vlib_trace_header_t **th;
52 /* Across each vlib main... */
53 for (i = 0; i < vec_len (client_trace_cache); i++)
55 th = client_trace_cache[i];
56 /* Toss the thread's cached data */
59 /* And toss the vector of threads */
60 vec_free (client_trace_cache);
61 tdmp->traces[client_index] = client_trace_cache;
65 tracedump_cache_reaper (u32 client_index)
67 tracedump_main_t *tdmp = &tracedump_main;
68 vlib_trace_header_t ***client_trace_cache;
70 /* Its likely that we won't have a cache entry */
71 if (client_index >= vec_len (tdmp->traces))
74 client_trace_cache = tdmp->traces[client_index];
75 toss_client_cache (tdmp, client_index, client_trace_cache);
79 VL_MSG_API_REAPER_FUNCTION (tracedump_cache_reaper);
81 /* API message handler */
83 vl_api_trace_dump_t_handler (vl_api_trace_dump_t * mp)
85 vl_api_registration_t *rp;
86 vl_api_trace_dump_reply_t *rmp;
87 vl_api_trace_details_t *dmp;
88 tracedump_main_t *tdmp = &tracedump_main;
89 vlib_trace_header_t ***client_trace_cache, **th;
92 u32 iterator_thread_id, iterator_position, max_records;
93 i32 retval = VNET_API_ERROR_NO_SUCH_ENTRY;
94 u32 last_thread_id = ~0, last_position = ~0;
96 u8 last_more_this_thread = 0;
97 u8 last_more_threads = 0;
100 rp = vl_api_client_index_to_registration (mp->client_index);
104 /* Use the registration pool index... */
105 client_index = rp->vl_api_registration_pool_index;
107 vec_validate_init_empty (tdmp->traces, client_index, 0);
109 client_trace_cache = tdmp->traces[client_index];
111 /* Clear the per-client cache if requested */
114 toss_client_cache (tdmp, client_index, client_trace_cache);
115 client_trace_cache = 0;
118 /* Now, where were we? */
119 iterator_thread_id = clib_net_to_host_u32 (mp->thread_id);
120 iterator_position = clib_net_to_host_u32 (mp->position);
121 max_records = clib_net_to_host_u32 (mp->max_records);
123 /* Need a fresh cache for this client? */
124 if (vec_len (client_trace_cache) == 0
125 && (iterator_thread_id != ~0 || iterator_position != ~0))
127 vlib_worker_thread_barrier_sync (&vlib_global_main);
129 /* Make a slot for each worker thread */
130 vec_validate (client_trace_cache, vec_len (vlib_mains) - 1);
136 vlib_trace_main_t *tm = &this_vlib_main->trace_main;
138 /* Filter as directed */
139 trace_apply_filter(this_vlib_main);
141 pool_foreach (th, tm->trace_buffer_pool,
143 vec_add1 (client_trace_cache[i], th[0]);
146 /* Sort them by increasing time. */
147 if (vec_len (client_trace_cache[i]))
148 vec_sort_with_function (client_trace_cache[i], trace_cmp);
153 vlib_worker_thread_barrier_release (&vlib_global_main);
156 /* Save the cache, one way or the other */
157 tdmp->traces[client_index] = client_trace_cache;
159 for (i = iterator_thread_id; i < vec_len (client_trace_cache); i++)
161 for (j = iterator_position; j < vec_len (client_trace_cache[i]); j++)
163 if (max_records == 0)
167 th = &client_trace_cache[i][j];
169 vec_reset_length (s);
171 s = format (s, "Packet %d\n%U\n\n", j + 1, format_vlib_trace,
172 &vlib_global_main, th[0]);
174 dmp = vl_msg_api_alloc (sizeof (*dmp) + vec_len (s));
176 htons (VL_API_TRACE_DETAILS + (tdmp->msg_id_base));
177 dmp->context = mp->context;
178 last_thread_id = dmp->thread_id = ntohl (i);
179 last_position = dmp->position = ntohl (j);
180 vl_api_vec_to_api_string (s, &dmp->trace_data);
181 dmp->more_threads = 0;
182 dmp->more_this_thread = 0;
184 /* Last record in the batch? */
185 if (max_records == 1)
187 /* More threads, but not more in this thread? */
188 if (j == (vec_len (client_trace_cache[i]) - 1))
189 dmp->more_threads = 1;
191 dmp->more_this_thread = 1;
193 /* Done, may or may not be at the end of a batch. */
195 if (i == (vec_len (client_trace_cache) - 1) &&
196 j == (vec_len (client_trace_cache[i]) - 1))
198 last_done = dmp->done = 1;
199 last_more_threads = dmp->more_threads = 0;
200 last_more_this_thread = dmp->more_this_thread = 0;
201 vl_api_send_msg (rp, (u8 *) dmp);
204 last_done = dmp->done;
205 vl_api_send_msg (rp, (u8 *) dmp);
209 iterator_position = 0;
214 rmp = vl_msg_api_alloc (sizeof (*rmp));
215 rmp->_vl_msg_id = htons (VL_API_TRACE_DUMP_REPLY + (tdmp->msg_id_base));
216 rmp->context = mp->context;
217 rmp->retval = clib_host_to_net_u32 (retval);
218 rmp->last_thread_id = last_thread_id;
219 rmp->last_position = last_position;
220 rmp->done = last_done;
221 rmp->more_this_thread = last_more_this_thread;
222 rmp->more_threads = last_more_threads;
224 /* Tag cleanup flushes to make life easy for the client */
225 if (iterator_thread_id == ~0 && iterator_position == ~0)
231 vl_api_send_msg (rp, (u8 *) rmp);
236 /* API definitions */
237 #include <tracedump/tracedump.api.c>
239 static clib_error_t *
240 tracedump_init (vlib_main_t * vm)
242 tracedump_main_t *tdmp = &tracedump_main;
243 api_main_t *am = vlibapi_get_main ();
245 clib_error_t *error = 0;
247 tdmp->vlib_main = vm;
248 tdmp->vnet_main = vnet_get_main ();
250 /* Add our API messages to the global name_crc hash table */
251 tdmp->msg_id_base = setup_message_id_table ();
253 am->is_mp_safe[tdmp->msg_id_base + VL_API_TRACE_DUMP] = 1;
258 VLIB_INIT_FUNCTION (tracedump_init);
260 VLIB_PLUGIN_REGISTER () =
262 .version = VPP_BUILD_VER,
263 .description = "Streaming packet trace dump plugin",
268 * fd.io coding-style-patch-verification: ON
271 * eval: (c-set-style "gnu")