FD.io VPP  v21.06-1-gbb7418cf9
Vector Packet Processing
tracedump.c
Go to the documentation of this file.
1 /*
2  * tracedump.c - skeleton vpp engine 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 
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <tracedump/tracedump.h>
21 #include <vlib/trace.h>
22 
23 #include <vlibapi/api.h>
24 #include <vlibmemory/api.h>
25 #include <vpp/app/version.h>
26 #include <stdbool.h>
27 
28 #include <tracedump/tracedump.api_enum.h>
29 #include <tracedump/tracedump.api_types.h>
30 
31 #define REPLY_MSG_ID_BASE tdmp->msg_id_base
33 
35 
36 
37 static void
39 {
42  u32 node_index = clib_net_to_host_u32 (mp->node_index);
43  u32 flag = clib_net_to_host_u32 (mp->flag);
44  u32 count = clib_net_to_host_u32 (mp->count);
45  vl_api_trace_set_filters_reply_t *rmp;
46  int rv = 0;
47 
48  if (flag == TRACE_FF_NONE)
49  {
50  count = node_index = 0;
51  }
52  else if (flag != TRACE_FF_INCLUDE_NODE && flag != TRACE_FF_EXCLUDE_NODE)
53  {
54  rv = VNET_API_ERROR_INVALID_VALUE;
55  goto done;
56  }
57 
59  node = vlib_get_node (vm, node_index);
60  if (!node)
61  {
62  rv = VNET_API_ERROR_NO_SUCH_NODE;
63  goto done;
64  }
65 
66  trace_filter_set (node_index, flag, count);
67 
68 done:
69  REPLY_MACRO (VL_API_TRACE_SET_FILTERS_REPLY);
70 }
71 
72 
73 static void
75 {
78  u32 add = clib_net_to_host_u32 (mp->max_packets);
79  u32 node_index = clib_net_to_host_u32 (mp->node_index);
80  u8 filter = mp->use_filter;
81  u8 verbose = mp->verbose;
82  u8 pre_clear = mp->pre_capture_clear;
83  vl_api_trace_capture_packets_reply_t *rmp;
84  int rv = 0;
85 
89 
91  node = vlib_get_node (vm, node_index);
92  if (!node)
93  {
94  rv = VNET_API_ERROR_NO_SUCH_NODE;
95  goto done;
96  }
97 
98  if ((node->flags & VLIB_NODE_FLAG_TRACE_SUPPORTED) == 0)
99  {
100  /* FIXME: Make a new, better error like "UNSUPPORTED_NODE_OPERATION"? */
101  rv = VNET_API_ERROR_NO_SUCH_NODE;
102  goto done;
103  }
104 
105  if (pre_clear)
107 
108  trace_update_capture_options (add, node_index, filter, verbose);
109 
110 done:
111  REPLY_MACRO (VL_API_TRACE_CAPTURE_PACKETS_REPLY);
112 }
113 
114 
115 static void
117 {
118  vl_api_trace_clear_capture_reply_t *rmp;
120 
122 
123  int rv = 0;
124  REPLY_MACRO (VL_API_TRACE_CLEAR_CAPTURE_REPLY);
125 }
126 
127 
128 
129 static int
130 trace_cmp (void *a1, void *a2)
131 {
132  vlib_trace_header_t **t1 = a1;
133  vlib_trace_header_t **t2 = a2;
134  i64 dt = t1[0]->time - t2[0]->time;
135  return dt < 0 ? -1 : (dt > 0 ? +1 : 0);
136 }
137 
138 static void
139 toss_client_cache (tracedump_main_t * tdmp, u32 client_index,
140  vlib_trace_header_t *** client_trace_cache)
141 {
142  vlib_trace_header_t **th;
143  int i;
144 
145  /* Across each vlib main... */
146  for (i = 0; i < vec_len (client_trace_cache); i++)
147  {
148  th = client_trace_cache[i];
149  /* Toss the thread's cached data */
150  vec_free (th);
151  }
152  /* And toss the vector of threads */
153  vec_free (client_trace_cache);
154  tdmp->traces[client_index] = client_trace_cache;
155 }
156 
157 static clib_error_t *
159 {
161  vlib_trace_header_t ***client_trace_cache;
162 
163  /* Its likely that we won't have a cache entry */
164  if (client_index >= vec_len (tdmp->traces))
165  return 0;
166 
167  client_trace_cache = tdmp->traces[client_index];
168  toss_client_cache (tdmp, client_index, client_trace_cache);
169  return 0;
170 }
171 
173 
174 /* API message handler */
175 static void
177 {
182  vlib_trace_header_t ***client_trace_cache, **th;
183  int i, j;
184  u32 client_index;
185  u32 iterator_thread_id, iterator_position, max_records;
186  i32 retval = VNET_API_ERROR_NO_SUCH_ENTRY;
187  u32 last_thread_id = ~0, last_position = ~0;
188  u8 last_done = 0;
189  u8 last_more_this_thread = 0;
190  u8 last_more_threads = 0;
191  u8 *s = 0;
192 
194  if (rp == 0)
195  return;
196 
197  /* Use the registration pool index... */
198  client_index = rp->vl_api_registration_pool_index;
199 
200  vec_validate_init_empty (tdmp->traces, client_index, 0);
201 
202  client_trace_cache = tdmp->traces[client_index];
203 
204  /* Clear the per-client cache if requested */
205  if (mp->clear_cache)
206  {
207  toss_client_cache (tdmp, client_index, client_trace_cache);
208  client_trace_cache = 0;
209  }
210 
211  /* Now, where were we? */
212  iterator_thread_id = clib_net_to_host_u32 (mp->thread_id);
213  iterator_position = clib_net_to_host_u32 (mp->position);
214  max_records = clib_net_to_host_u32 (mp->max_records);
215 
216  /* Don't overflow the existing queue space. */
217  svm_queue_t *q = rp->vl_input_queue;
218  u32 queue_slots_available = q->maxsize - q->cursize;
219  int chunk = (queue_slots_available > 0) ? queue_slots_available - 1 : 0;
220  if (chunk < max_records)
221  max_records = chunk;
222 
223  /* Need a fresh cache for this client? */
224  if (vec_len (client_trace_cache) == 0
225  && (iterator_thread_id != ~0 || iterator_position != ~0))
226  {
228 
229  /* Make a slot for each worker thread */
230  vec_validate (client_trace_cache, vlib_get_n_threads () - 1);
231  i = 0;
232 
234  {
235  vlib_trace_main_t *tm = &this_vlib_main->trace_main;
236 
237  /* Filter as directed */
238  trace_apply_filter (this_vlib_main);
239 
241  {
242  vec_add1 (client_trace_cache[i], th[0]);
243  }
244 
245  /* Sort them by increasing time. */
246  if (vec_len (client_trace_cache[i]))
247  vec_sort_with_function (client_trace_cache[i], trace_cmp);
248 
249  i++;
250  }
252  }
253 
254  /* Save the cache, one way or the other */
255  tdmp->traces[client_index] = client_trace_cache;
256 
257  for (i = iterator_thread_id; i < vec_len (client_trace_cache); i++)
258  {
259  for (j = iterator_position; j < vec_len (client_trace_cache[i]); j++)
260  {
261  if (max_records == 0)
262  break;
263 
264  retval = 0;
265  th = &client_trace_cache[i][j];
266 
267  vec_reset_length (s);
268 
269  s =
270  format (s, "%U", format_vlib_trace, vlib_get_first_main (), th[0]);
271 
272  dmp = vl_msg_api_alloc (sizeof (*dmp) + vec_len (s));
273  dmp->_vl_msg_id =
274  htons (VL_API_TRACE_DETAILS + (tdmp->msg_id_base));
275  dmp->context = mp->context;
276  last_thread_id = dmp->thread_id = ntohl (i);
277  last_position = dmp->position = ntohl (j);
279  dmp->packet_number = htonl (j);
280  dmp->more_threads = 0;
281  dmp->more_this_thread = 0;
282 
283  /* Last record in the batch? */
284  if (max_records == 1)
285  {
286  /* More threads, but not more in this thread? */
287  if (j == (vec_len (client_trace_cache[i]) - 1))
288  dmp->more_threads = 1;
289  else
290  dmp->more_this_thread = 1;
291  }
292  /* Done, may or may not be at the end of a batch. */
293  dmp->done = 0;
294  if (i == (vec_len (client_trace_cache) - 1) &&
295  j == (vec_len (client_trace_cache[i]) - 1))
296  {
297  last_done = dmp->done = 1;
298  last_more_threads = dmp->more_threads = 0;
299  last_more_this_thread = dmp->more_this_thread = 0;
300  vl_api_send_msg (rp, (u8 *) dmp);
301  goto doublebreak;
302  }
303  last_done = dmp->done;
304  vl_api_send_msg (rp, (u8 *) dmp);
305 
306  max_records--;
307  }
308  iterator_position = 0;
309  }
310 
311 doublebreak:;
312 
313  rmp = vl_msg_api_alloc (sizeof (*rmp));
314  rmp->_vl_msg_id = htons (VL_API_TRACE_DUMP_REPLY + (tdmp->msg_id_base));
315  rmp->context = mp->context;
316  rmp->retval = clib_host_to_net_u32 (retval);
317  rmp->last_thread_id = last_thread_id;
318  rmp->last_position = last_position;
319  rmp->done = last_done;
320  rmp->more_this_thread = last_more_this_thread;
321  rmp->more_threads = last_more_threads;
322 
323  /* Tag cleanup flushes to make life easy for the client */
324  if (iterator_thread_id == ~0 && iterator_position == ~0)
325  {
326  rmp->retval = 0;
327  rmp->done = 1;
328  rmp->flush_only = 1;
329  }
330  vl_api_send_msg (rp, (u8 *) rmp);
331 
332  vec_free (s);
333 }
334 
335 /* API definitions */
336 #include <tracedump/tracedump.api.c>
337 
338 static clib_error_t *
340 {
343 
344  clib_error_t *error = 0;
345 
346  tdmp->vlib_main = vm;
347  tdmp->vnet_main = vnet_get_main ();
348 
349  /* Add our API messages to the global name_crc hash table */
351 
352  am->is_mp_safe[tdmp->msg_id_base + VL_API_TRACE_DUMP] = 1;
353 
354  return error;
355 }
356 
358 /* *INDENT-OFF* */
360 {
361  .version = VPP_BUILD_VER,
362  .description = "Streaming packet trace dump plugin",
363 };
364 /* *INDENT-ON* */
365 
366 /*
367  * fd.io coding-style-patch-verification: ON
368  *
369  * Local Variables:
370  * eval: (c-set-style "gnu")
371  * End:
372  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:524
static void vl_api_trace_dump_t_handler(vl_api_trace_dump_t *mp)
Definition: tracedump.c:176
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
#define VLIB_NODE_FLAG_TRACE_SUPPORTED
Definition: node.h:296
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
vlib_main_t * vlib_main
Definition: tracedump.h:40
u16 flags
Definition: node.h:279
VLIB_PLUGIN_REGISTER()
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:607
void trace_filter_set(u32 node_index, u32 flag, u32 count)
Definition: trace.c:528
trace_clear_capture
Definition: tracedump.api:86
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:535
void * vl_msg_api_alloc(int nbytes)
void trace_update_capture_options(u32 add, u32 node_index, u32 filter, u8 verbose)
Definition: trace.c:385
u32 node_index[default=0xffffffff]
Definition: tracedump.api:54
unsigned char u8
Definition: types.h:56
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:194
unsigned int u32
Definition: types.h:88
#define foreach_vlib_main()
Definition: threads.h:237
vlib_trace_header_t ** trace_buffer_pool
Definition: trace.h:86
static clib_error_t * tracedump_init(vlib_main_t *vm)
Definition: tracedump.c:339
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
description fragment has unexpected format
Definition: map.api:433
tracedump_main_t tracedump_main
Definition: tracedump.c:34
vnet_main_t * vnet_get_main(void)
int __clib_unused rv
Definition: application.c:491
u8 * vnet_trace_placeholder
Definition: trace.c:44
vlib_trace_header_t **** traces
Definition: tracedump.h:37
vnet_main_t * vnet_main
Definition: tracedump.h:41
Definition: cJSON.c:88
format_function_t format_vlib_trace
Definition: trace.h:114
svm_queue_t * vl_input_queue
shared memory only: pointer to client input queue
Definition: api_common.h:63
signed long i64
Definition: types.h:78
VL_MSG_API_REAPER_FUNCTION(tracedump_cache_reaper)
static void vl_api_trace_set_filters_t_handler(vl_api_trace_set_filters_t *mp)
Definition: tracedump.c:38
static void vl_api_trace_clear_capture_t_handler(vl_api_trace_clear_capture_t *mp)
Definition: tracedump.c:116
#define REPLY_MACRO(t)
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
void vlib_trace_stop_and_clear(void)
Definition: trace.c:377
vl_api_trace_filter_flag_t flag
Definition: tracedump.api:52
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:228
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
static void setup_message_id_table(api_main_t *am)
Definition: bfd_api.c:451
static void toss_client_cache(tracedump_main_t *tdmp, u32 client_index, vlib_trace_header_t ***client_trace_cache)
Definition: tracedump.c:139
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:395
trace_capture_packets
Definition: tracedump.api:69
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
static clib_error_t * tracedump_cache_reaper(u32 client_index)
Definition: tracedump.c:158
static vlib_main_t * vlib_get_first_main(void)
Definition: global_funcs.h:44
signed int i32
Definition: types.h:77
trace_set_filters
Definition: tracedump.api:48
static u32 vlib_get_n_threads()
Definition: global_funcs.h:23
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:1098
u32 vl_api_registration_pool_index
Index in VLIB&#39;s brain (not shared memory).
Definition: api_common.h:52
node node_index
static void vl_api_trace_capture_packets_t_handler(vl_api_trace_capture_packets_t *mp)
Definition: tracedump.c:74
struct _svm_queue svm_queue_t
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:390
static int trace_cmp(void *a1, void *a2)
Definition: tracedump.c:130
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1386
u8 * is_mp_safe
Message is mp safe vector.
Definition: api_common.h:251
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:86
u8 count
Definition: dhcp.api:208
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:571
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
app_main_t * am
Definition: application.c:489
void trace_apply_filter(vlib_main_t *vm)
Definition: trace.c:240
int vl_api_vec_to_api_string(const u8 *vec, vl_api_string_t *str)
Definition: api_shared.c:1175