blob: 2a50adac5ddfd4898727b5e20b7c9a39fb3314ea [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
Florin Coras11166672020-04-13 01:20:25 +000055VLIB_CLI_COMMAND (show_session_dbg_clock_cycles_command, static) =
56{
57 .path = "show session dbg clock_cycles",
58 .short_help = "show session dbg clock_cycles",
59 .function = show_session_dbg_clock_cycles_fn,
60};
Florin Coras11166672020-04-13 01:20:25 +000061
Steven Luong10e5b4a2022-10-10 11:37:57 -070062static_always_inline f64
63session_dbg_time_now (u32 thread)
64{
65 vlib_main_t *vm = vlib_get_main_by_index (thread);
66
67 return clib_time_now (&vm->clib_time) + vm->time_offset;
68}
Florin Coras11166672020-04-13 01:20:25 +000069
70static clib_error_t *
71clear_session_dbg_clock_cycles_fn (vlib_main_t * vm, unformat_input_t * input,
72 vlib_cli_command_t * cmd)
73{
74 session_dbg_evts_t *sde;
75 u32 thread;
76
77 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
78 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
79 input);
80
81 for (thread = 0; thread < vec_len (session_dbg_main.wrk); thread++)
82 {
83 sde = &session_dbg_main.wrk[thread];
84 clib_memset (sde, 0, sizeof (session_dbg_evts_t));
Steven Luong10e5b4a2022-10-10 11:37:57 -070085 sde->last_time = session_dbg_time_now (thread);
Florin Coras11166672020-04-13 01:20:25 +000086 sde->start_time = sde->last_time;
87 }
88
89 return 0;
90}
91
92
Florin Coras11166672020-04-13 01:20:25 +000093VLIB_CLI_COMMAND (clear_session_clock_cycles_command, static) =
94{
95 .path = "clear session dbg clock_cycles",
96 .short_help = "clear session dbg clock_cycles",
97 .function = clear_session_dbg_clock_cycles_fn,
98};
Florin Coras11166672020-04-13 01:20:25 +000099
100void
101session_debug_init (void)
102{
103 vlib_thread_main_t *vtm = vlib_get_thread_main ();
104 session_dbg_main_t *sdm = &session_dbg_main;
105 u32 num_threads, thread;
106
107 num_threads = vtm->n_vlib_mains;
108
109 vec_validate_aligned (sdm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
110 for (thread = 0; thread < num_threads; thread++)
111 {
112 clib_memset (&sdm->wrk[thread], 0, sizeof (session_dbg_evts_t));
Steven Luong10e5b4a2022-10-10 11:37:57 -0700113 sdm->wrk[thread].start_time = session_dbg_time_now (thread);
Florin Coras11166672020-04-13 01:20:25 +0000114 }
115}
Steven Luong20de85b2022-10-17 10:39:06 -0700116
117static const char *session_evt_grp_str[] = {
118#define _(sym, str) str,
119 foreach_session_evt_grp
120#undef _
121};
122
123static void
124session_debug_show_groups (vlib_main_t *vm)
125{
126 session_dbg_main_t *sdm = &session_dbg_main;
127 int i = 0;
128
129 vlib_cli_output (vm, "%-10s%-30s%-10s", "Index", "Group", "Level");
130
131 for (i = 0; i < SESSION_EVT_N_GRP; i++)
132 vlib_cli_output (vm, "%-10d%-30s%-10d", i, session_evt_grp_str[i],
133 sdm->grp_dbg_lvl[i]);
134}
135
136static clib_error_t *
137session_debug_fn (vlib_main_t *vm, unformat_input_t *input,
138 vlib_cli_command_t *cmd)
139{
140 session_dbg_main_t *sdm = &session_dbg_main;
141 u32 group, level = ~0;
142 clib_error_t *error = 0;
143 u8 is_show = 0;
144 uword *bitmap = 0;
145
146 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
147 {
148 if (unformat (input, "show"))
149 is_show = 1;
150 else if (unformat (input, "group %U", unformat_bitmap_list, &bitmap))
151 ;
152 else if (unformat (input, "level %d", &level))
153 ;
154 else
155 {
156 error = clib_error_return (0, "unknown input `%U'",
157 format_unformat_error, input);
158 goto done;
159 }
160 }
161
162 if (is_show)
163 {
164 session_debug_show_groups (vm);
165 goto done;
166 }
167 if (level == ~0)
168 {
169 vlib_cli_output (vm, "level must be entered");
170 goto done;
171 }
172
173 group = clib_bitmap_last_set (bitmap);
174 if (group == ~0)
175 {
176 vlib_cli_output (vm, "group must be entered");
177 goto done;
178 }
179 if (group >= SESSION_EVT_N_GRP)
180 {
181 vlib_cli_output (vm, "group out of bounds");
182 goto done;
183 }
184 clib_bitmap_foreach (group, bitmap)
185 sdm->grp_dbg_lvl[group] = level;
186
187done:
188
189 clib_bitmap_free (bitmap);
190 return error;
191}
192
193VLIB_CLI_COMMAND (session_debug_command, static) = {
194 .path = "session debug",
195 .short_help = "session debug {show | debug group <list> level <n>}",
196 .function = session_debug_fn,
197 .is_mp_safe = 1,
198};
199
Florin Coras11166672020-04-13 01:20:25 +0000200#else
201void
202session_debug_init (void)
203{
204}
Steven Luong20de85b2022-10-17 10:39:06 -0700205#endif /* SESSION_DEBUG */
Florin Coras11166672020-04-13 01:20:25 +0000206
207void
208dump_thread_0_event_queue (void)
209{
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100210 vlib_main_t *vm = vlib_get_first_main ();
Florin Coras11166672020-04-13 01:20:25 +0000211 u32 my_thread_index = vm->thread_index;
212 session_event_t _e, *e = &_e;
Florin Coras86f12322021-01-22 15:05:14 -0800213 svm_msg_q_shared_queue_t *sq;
Florin Coras11166672020-04-13 01:20:25 +0000214 svm_msg_q_ring_t *ring;
215 session_t *s0;
216 svm_msg_q_msg_t *msg;
217 svm_msg_q_t *mq;
218 int i, index;
219
Florin Corasb4624182020-12-11 13:58:12 -0800220 mq = session_main_get_vpp_event_queue (my_thread_index);
Florin Coras86f12322021-01-22 15:05:14 -0800221 sq = mq->q.shr;
222 index = sq->head;
Florin Coras11166672020-04-13 01:20:25 +0000223
Florin Coras86f12322021-01-22 15:05:14 -0800224 for (i = 0; i < sq->cursize; i++)
Florin Coras11166672020-04-13 01:20:25 +0000225 {
Florin Coras86f12322021-01-22 15:05:14 -0800226 msg = (svm_msg_q_msg_t *) (&sq->data[0] + sq->elsize * index);
Florin Coras11166672020-04-13 01:20:25 +0000227 ring = svm_msg_q_ring (mq, msg->ring_index);
228 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
229
230 switch (e->event_type)
231 {
232 case SESSION_IO_EVT_TX:
233 s0 = session_get_if_valid (e->session_index, my_thread_index);
Florin Coras8f603182022-03-31 07:57:47 -0700234 if (!s0)
235 break;
Florin Coras11166672020-04-13 01:20:25 +0000236 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
237 break;
238
239 case SESSION_CTRL_EVT_CLOSE:
240 s0 = session_get_from_handle (e->session_handle);
241 fformat (stdout, "[%04d] disconnect session %d\n", i,
242 s0->session_index);
243 break;
244
245 case SESSION_IO_EVT_BUILTIN_RX:
246 s0 = session_get_if_valid (e->session_index, my_thread_index);
Florin Coras8f603182022-03-31 07:57:47 -0700247 if (!s0)
248 break;
Florin Coras11166672020-04-13 01:20:25 +0000249 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
250 break;
251
252 case SESSION_CTRL_EVT_RPC:
253 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
254 i, (u64) (uword) (e->rpc_args.fp),
255 (u64) (uword) (e->rpc_args.arg));
256 break;
257
258 default:
259 fformat (stdout, "[%04d] unhandled event type %d\n",
260 i, e->event_type);
261 break;
262 }
263
264 index++;
265
Florin Coras86f12322021-01-22 15:05:14 -0800266 if (index == sq->maxsize)
Florin Coras11166672020-04-13 01:20:25 +0000267 index = 0;
268 }
269}
270
271static u8
272session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
273{
Florin Coras11166672020-04-13 01:20:25 +0000274 switch (e->event_type)
275 {
276 case SESSION_IO_EVT_RX:
277 case SESSION_IO_EVT_TX:
278 case SESSION_IO_EVT_BUILTIN_RX:
Florin Corasaeb7c1c2023-03-10 10:22:21 -0800279 case SESSION_IO_EVT_TX_MAIN:
Florin Coras11166672020-04-13 01:20:25 +0000280 case SESSION_IO_EVT_TX_FLUSH:
Florin Corasc547e912020-12-08 17:50:45 -0800281 if (e->session_index == f->shr->master_session_index)
Florin Coras11166672020-04-13 01:20:25 +0000282 return 1;
283 break;
284 case SESSION_CTRL_EVT_CLOSE:
Florin Coras11166672020-04-13 01:20:25 +0000285 case SESSION_CTRL_EVT_RPC:
Florin Coras11166672020-04-13 01:20:25 +0000286 break;
287 default:
288 break;
289 }
290 return 0;
291}
292
293u8
294session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
295{
Florin Coras86f12322021-01-22 15:05:14 -0800296 svm_msg_q_shared_queue_t *sq;
Florin Coras11166672020-04-13 01:20:25 +0000297 session_evt_elt_t *elt;
298 session_worker_t *wrk;
299 int i, index, found = 0;
300 svm_msg_q_msg_t *msg;
Florin Coras11166672020-04-13 01:20:25 +0000301 svm_msg_q_t *mq;
302 u8 thread_index;
303
304 ASSERT (e);
305 thread_index = f->master_thread_index;
306 wrk = session_main_get_worker (thread_index);
307
308 /*
309 * Search evt queue
310 */
311 mq = wrk->vpp_event_queue;
Florin Coras86f12322021-01-22 15:05:14 -0800312 sq = mq->q.shr;
313 index = sq->head;
314 for (i = 0; i < sq->cursize; i++)
Florin Coras11166672020-04-13 01:20:25 +0000315 {
Florin Coras86f12322021-01-22 15:05:14 -0800316 msg = (svm_msg_q_msg_t *) (&sq->data[0] + sq->elsize * index);
Florin Corasaf2e88d2023-02-02 20:07:19 -0800317 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), sizeof (*e));
Florin Coras11166672020-04-13 01:20:25 +0000318 found = session_node_cmp_event (e, f);
319 if (found)
320 return 1;
Florin Coras86f12322021-01-22 15:05:14 -0800321 index = (index + 1) % sq->maxsize;
Florin Coras11166672020-04-13 01:20:25 +0000322 }
323 /*
324 * Search pending events vector
325 */
326
Florin Coras11166672020-04-13 01:20:25 +0000327 clib_llist_foreach (wrk->event_elts, evt_list,
328 pool_elt_at_index (wrk->event_elts, wrk->new_head),
329 elt, ({
330 found = session_node_cmp_event (&elt->evt, f);
331 if (found)
332 {
333 clib_memcpy_fast (e, &elt->evt, sizeof (*e));
334 goto done;
335 }
336 }));
Florin Coras11166672020-04-13 01:20:25 +0000337
Florin Coras11166672020-04-13 01:20:25 +0000338 clib_llist_foreach (wrk->event_elts, evt_list,
339 pool_elt_at_index (wrk->event_elts, wrk->old_head),
340 elt, ({
341 found = session_node_cmp_event (&elt->evt, f);
342 if (found)
343 {
344 clib_memcpy_fast (e, &elt->evt, sizeof (*e));
345 goto done;
346 }
347 }));
Florin Coras11166672020-04-13 01:20:25 +0000348
349done:
350 return found;
351}
352
353/*
354 * fd.io coding-style-patch-verification: ON
355 *
356 * Local Variables:
357 * eval: (c-set-style "gnu")
358 * End:
359 */