blob: 39d1752bdc6b89a4ec93e2ca016896822f033a89 [file] [log] [blame]
Florin Coras11166672020-04-13 01:20:25 +00001/*
2 * Copyright (c) 2020 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 <vnet/session/session_debug.h>
17#include <vnet/session/session.h>
18
19#if SESSION_DEBUG > 0
20
21session_dbg_main_t session_dbg_main;
22
23static clib_error_t *
24show_session_dbg_clock_cycles_fn (vlib_main_t * vm, unformat_input_t * input,
25 vlib_cli_command_t * cmd)
26{
27 u32 thread;
28
29 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
30 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
31 input);
32
33 for (thread = 0; thread < vec_len (session_dbg_main.wrk); thread++)
34 {
35 vlib_cli_output (vm, "Threads %u:\n", thread);
36 session_dbg_evts_t *sdm = &session_dbg_main.wrk[thread];
37
38#define _(sym, disp, type, str) \
39 if(disp) \
40 { \
41 if (!type) \
42 vlib_cli_output (vm, "\t %25s : %12lu ", str, \
43 sdm->counters[SESS_Q_##sym].u64); \
44 else \
45 vlib_cli_output (vm, "\t %25s : %12.3f ", str, \
46 sdm->counters[SESS_Q_##sym].f64); \
47 }
48 foreach_session_events
49#undef _
50 }
51 return 0;
52}
53
54
55/* *INDENT-OFF* */
56VLIB_CLI_COMMAND (show_session_dbg_clock_cycles_command, static) =
57{
58 .path = "show session dbg clock_cycles",
59 .short_help = "show session dbg clock_cycles",
60 .function = show_session_dbg_clock_cycles_fn,
61};
62/* *INDENT-ON* */
63
Steven Luong10e5b4a2022-10-10 11:37:57 -070064static_always_inline f64
65session_dbg_time_now (u32 thread)
66{
67 vlib_main_t *vm = vlib_get_main_by_index (thread);
68
69 return clib_time_now (&vm->clib_time) + vm->time_offset;
70}
Florin Coras11166672020-04-13 01:20:25 +000071
72static clib_error_t *
73clear_session_dbg_clock_cycles_fn (vlib_main_t * vm, unformat_input_t * input,
74 vlib_cli_command_t * cmd)
75{
76 session_dbg_evts_t *sde;
77 u32 thread;
78
79 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
80 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
81 input);
82
83 for (thread = 0; thread < vec_len (session_dbg_main.wrk); thread++)
84 {
85 sde = &session_dbg_main.wrk[thread];
86 clib_memset (sde, 0, sizeof (session_dbg_evts_t));
Steven Luong10e5b4a2022-10-10 11:37:57 -070087 sde->last_time = session_dbg_time_now (thread);
Florin Coras11166672020-04-13 01:20:25 +000088 sde->start_time = sde->last_time;
89 }
90
91 return 0;
92}
93
94
95/* *INDENT-OFF* */
96VLIB_CLI_COMMAND (clear_session_clock_cycles_command, static) =
97{
98 .path = "clear session dbg clock_cycles",
99 .short_help = "clear session dbg clock_cycles",
100 .function = clear_session_dbg_clock_cycles_fn,
101};
102/* *INDENT-ON* */
103
104void
105session_debug_init (void)
106{
107 vlib_thread_main_t *vtm = vlib_get_thread_main ();
108 session_dbg_main_t *sdm = &session_dbg_main;
109 u32 num_threads, thread;
110
111 num_threads = vtm->n_vlib_mains;
112
113 vec_validate_aligned (sdm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
114 for (thread = 0; thread < num_threads; thread++)
115 {
116 clib_memset (&sdm->wrk[thread], 0, sizeof (session_dbg_evts_t));
Steven Luong10e5b4a2022-10-10 11:37:57 -0700117 sdm->wrk[thread].start_time = session_dbg_time_now (thread);
Florin Coras11166672020-04-13 01:20:25 +0000118 }
119}
Steven Luong20de85b2022-10-17 10:39:06 -0700120
121static const char *session_evt_grp_str[] = {
122#define _(sym, str) str,
123 foreach_session_evt_grp
124#undef _
125};
126
127static void
128session_debug_show_groups (vlib_main_t *vm)
129{
130 session_dbg_main_t *sdm = &session_dbg_main;
131 int i = 0;
132
133 vlib_cli_output (vm, "%-10s%-30s%-10s", "Index", "Group", "Level");
134
135 for (i = 0; i < SESSION_EVT_N_GRP; i++)
136 vlib_cli_output (vm, "%-10d%-30s%-10d", i, session_evt_grp_str[i],
137 sdm->grp_dbg_lvl[i]);
138}
139
140static clib_error_t *
141session_debug_fn (vlib_main_t *vm, unformat_input_t *input,
142 vlib_cli_command_t *cmd)
143{
144 session_dbg_main_t *sdm = &session_dbg_main;
145 u32 group, level = ~0;
146 clib_error_t *error = 0;
147 u8 is_show = 0;
148 uword *bitmap = 0;
149
150 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
151 {
152 if (unformat (input, "show"))
153 is_show = 1;
154 else if (unformat (input, "group %U", unformat_bitmap_list, &bitmap))
155 ;
156 else if (unformat (input, "level %d", &level))
157 ;
158 else
159 {
160 error = clib_error_return (0, "unknown input `%U'",
161 format_unformat_error, input);
162 goto done;
163 }
164 }
165
166 if (is_show)
167 {
168 session_debug_show_groups (vm);
169 goto done;
170 }
171 if (level == ~0)
172 {
173 vlib_cli_output (vm, "level must be entered");
174 goto done;
175 }
176
177 group = clib_bitmap_last_set (bitmap);
178 if (group == ~0)
179 {
180 vlib_cli_output (vm, "group must be entered");
181 goto done;
182 }
183 if (group >= SESSION_EVT_N_GRP)
184 {
185 vlib_cli_output (vm, "group out of bounds");
186 goto done;
187 }
188 clib_bitmap_foreach (group, bitmap)
189 sdm->grp_dbg_lvl[group] = level;
190
191done:
192
193 clib_bitmap_free (bitmap);
194 return error;
195}
196
197VLIB_CLI_COMMAND (session_debug_command, static) = {
198 .path = "session debug",
199 .short_help = "session debug {show | debug group <list> level <n>}",
200 .function = session_debug_fn,
201 .is_mp_safe = 1,
202};
203
Florin Coras11166672020-04-13 01:20:25 +0000204#else
205void
206session_debug_init (void)
207{
208}
Steven Luong20de85b2022-10-17 10:39:06 -0700209#endif /* SESSION_DEBUG */
Florin Coras11166672020-04-13 01:20:25 +0000210
211void
212dump_thread_0_event_queue (void)
213{
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100214 vlib_main_t *vm = vlib_get_first_main ();
Florin Coras11166672020-04-13 01:20:25 +0000215 u32 my_thread_index = vm->thread_index;
216 session_event_t _e, *e = &_e;
Florin Coras86f12322021-01-22 15:05:14 -0800217 svm_msg_q_shared_queue_t *sq;
Florin Coras11166672020-04-13 01:20:25 +0000218 svm_msg_q_ring_t *ring;
219 session_t *s0;
220 svm_msg_q_msg_t *msg;
221 svm_msg_q_t *mq;
222 int i, index;
223
Florin Corasb4624182020-12-11 13:58:12 -0800224 mq = session_main_get_vpp_event_queue (my_thread_index);
Florin Coras86f12322021-01-22 15:05:14 -0800225 sq = mq->q.shr;
226 index = sq->head;
Florin Coras11166672020-04-13 01:20:25 +0000227
Florin Coras86f12322021-01-22 15:05:14 -0800228 for (i = 0; i < sq->cursize; i++)
Florin Coras11166672020-04-13 01:20:25 +0000229 {
Florin Coras86f12322021-01-22 15:05:14 -0800230 msg = (svm_msg_q_msg_t *) (&sq->data[0] + sq->elsize * index);
Florin Coras11166672020-04-13 01:20:25 +0000231 ring = svm_msg_q_ring (mq, msg->ring_index);
232 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
233
234 switch (e->event_type)
235 {
236 case SESSION_IO_EVT_TX:
237 s0 = session_get_if_valid (e->session_index, my_thread_index);
Florin Coras8f603182022-03-31 07:57:47 -0700238 if (!s0)
239 break;
Florin Coras11166672020-04-13 01:20:25 +0000240 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
241 break;
242
243 case SESSION_CTRL_EVT_CLOSE:
244 s0 = session_get_from_handle (e->session_handle);
245 fformat (stdout, "[%04d] disconnect session %d\n", i,
246 s0->session_index);
247 break;
248
249 case SESSION_IO_EVT_BUILTIN_RX:
250 s0 = session_get_if_valid (e->session_index, my_thread_index);
Florin Coras8f603182022-03-31 07:57:47 -0700251 if (!s0)
252 break;
Florin Coras11166672020-04-13 01:20:25 +0000253 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
254 break;
255
256 case SESSION_CTRL_EVT_RPC:
257 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
258 i, (u64) (uword) (e->rpc_args.fp),
259 (u64) (uword) (e->rpc_args.arg));
260 break;
261
262 default:
263 fformat (stdout, "[%04d] unhandled event type %d\n",
264 i, e->event_type);
265 break;
266 }
267
268 index++;
269
Florin Coras86f12322021-01-22 15:05:14 -0800270 if (index == sq->maxsize)
Florin Coras11166672020-04-13 01:20:25 +0000271 index = 0;
272 }
273}
274
275static u8
276session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
277{
Florin Coras11166672020-04-13 01:20:25 +0000278 switch (e->event_type)
279 {
280 case SESSION_IO_EVT_RX:
281 case SESSION_IO_EVT_TX:
282 case SESSION_IO_EVT_BUILTIN_RX:
Florin Corasaeb7c1c2023-03-10 10:22:21 -0800283 case SESSION_IO_EVT_TX_MAIN:
Florin Coras11166672020-04-13 01:20:25 +0000284 case SESSION_IO_EVT_TX_FLUSH:
Florin Corasc547e912020-12-08 17:50:45 -0800285 if (e->session_index == f->shr->master_session_index)
Florin Coras11166672020-04-13 01:20:25 +0000286 return 1;
287 break;
288 case SESSION_CTRL_EVT_CLOSE:
Florin Coras11166672020-04-13 01:20:25 +0000289 case SESSION_CTRL_EVT_RPC:
Florin Coras11166672020-04-13 01:20:25 +0000290 break;
291 default:
292 break;
293 }
294 return 0;
295}
296
297u8
298session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
299{
Florin Coras86f12322021-01-22 15:05:14 -0800300 svm_msg_q_shared_queue_t *sq;
Florin Coras11166672020-04-13 01:20:25 +0000301 session_evt_elt_t *elt;
302 session_worker_t *wrk;
303 int i, index, found = 0;
304 svm_msg_q_msg_t *msg;
Florin Coras11166672020-04-13 01:20:25 +0000305 svm_msg_q_t *mq;
306 u8 thread_index;
307
308 ASSERT (e);
309 thread_index = f->master_thread_index;
310 wrk = session_main_get_worker (thread_index);
311
312 /*
313 * Search evt queue
314 */
315 mq = wrk->vpp_event_queue;
Florin Coras86f12322021-01-22 15:05:14 -0800316 sq = mq->q.shr;
317 index = sq->head;
318 for (i = 0; i < sq->cursize; i++)
Florin Coras11166672020-04-13 01:20:25 +0000319 {
Florin Coras86f12322021-01-22 15:05:14 -0800320 msg = (svm_msg_q_msg_t *) (&sq->data[0] + sq->elsize * index);
Florin Corasaf2e88d2023-02-02 20:07:19 -0800321 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), sizeof (*e));
Florin Coras11166672020-04-13 01:20:25 +0000322 found = session_node_cmp_event (e, f);
323 if (found)
324 return 1;
Florin Coras86f12322021-01-22 15:05:14 -0800325 index = (index + 1) % sq->maxsize;
Florin Coras11166672020-04-13 01:20:25 +0000326 }
327 /*
328 * Search pending events vector
329 */
330
331 /* *INDENT-OFF* */
332 clib_llist_foreach (wrk->event_elts, evt_list,
333 pool_elt_at_index (wrk->event_elts, wrk->new_head),
334 elt, ({
335 found = session_node_cmp_event (&elt->evt, f);
336 if (found)
337 {
338 clib_memcpy_fast (e, &elt->evt, sizeof (*e));
339 goto done;
340 }
341 }));
342 /* *INDENT-ON* */
343
344 /* *INDENT-OFF* */
345 clib_llist_foreach (wrk->event_elts, evt_list,
346 pool_elt_at_index (wrk->event_elts, wrk->old_head),
347 elt, ({
348 found = session_node_cmp_event (&elt->evt, f);
349 if (found)
350 {
351 clib_memcpy_fast (e, &elt->evt, sizeof (*e));
352 goto done;
353 }
354 }));
355 /* *INDENT-ON* */
356
357done:
358 return found;
359}
360
361/*
362 * fd.io coding-style-patch-verification: ON
363 *
364 * Local Variables:
365 * eval: (c-set-style "gnu")
366 * End:
367 */