udp: fix csum computation when offload disabled
[vpp.git] / src / vnet / session / session_debug.c
1 /*
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
21 session_dbg_main_t session_dbg_main;
22
23 static clib_error_t *
24 show_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 VLIB_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 };
61
62 static_always_inline f64
63 session_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 }
69
70 static clib_error_t *
71 clear_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));
85       sde->last_time = session_dbg_time_now (thread);
86       sde->start_time = sde->last_time;
87     }
88
89   return 0;
90 }
91
92
93 VLIB_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 };
99
100 void
101 session_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));
113       sdm->wrk[thread].start_time = session_dbg_time_now (thread);
114     }
115 }
116
117 static const char *session_evt_grp_str[] = {
118 #define _(sym, str) str,
119   foreach_session_evt_grp
120 #undef _
121 };
122
123 static void
124 session_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
136 static clib_error_t *
137 session_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
187 done:
188
189   clib_bitmap_free (bitmap);
190   return error;
191 }
192
193 VLIB_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
200 #else
201 void
202 session_debug_init (void)
203 {
204 }
205 #endif /* SESSION_DEBUG */
206
207 void
208 dump_thread_0_event_queue (void)
209 {
210   vlib_main_t *vm = vlib_get_first_main ();
211   u32 my_thread_index = vm->thread_index;
212   session_event_t _e, *e = &_e;
213   svm_msg_q_shared_queue_t *sq;
214   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
220   mq = session_main_get_vpp_event_queue (my_thread_index);
221   sq = mq->q.shr;
222   index = sq->head;
223
224   for (i = 0; i < sq->cursize; i++)
225     {
226       msg = (svm_msg_q_msg_t *) (&sq->data[0] + sq->elsize * index);
227       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);
234           if (!s0)
235             break;
236           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);
247           if (!s0)
248             break;
249           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
266       if (index == sq->maxsize)
267         index = 0;
268     }
269 }
270
271 static u8
272 session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
273 {
274   switch (e->event_type)
275     {
276     case SESSION_IO_EVT_RX:
277     case SESSION_IO_EVT_TX:
278     case SESSION_IO_EVT_BUILTIN_RX:
279     case SESSION_IO_EVT_TX_MAIN:
280     case SESSION_IO_EVT_TX_FLUSH:
281       if (e->session_index == f->shr->master_session_index)
282         return 1;
283       break;
284     case SESSION_CTRL_EVT_CLOSE:
285     case SESSION_CTRL_EVT_RPC:
286       break;
287     default:
288       break;
289     }
290   return 0;
291 }
292
293 u8
294 session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
295 {
296   svm_msg_q_shared_queue_t *sq;
297   session_evt_elt_t *elt;
298   session_worker_t *wrk;
299   int i, index, found = 0;
300   svm_msg_q_msg_t *msg;
301   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;
312   sq = mq->q.shr;
313   index = sq->head;
314   for (i = 0; i < sq->cursize; i++)
315     {
316       msg = (svm_msg_q_msg_t *) (&sq->data[0] + sq->elsize * index);
317       clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), sizeof (*e));
318       found = session_node_cmp_event (e, f);
319       if (found)
320         return 1;
321       index = (index + 1) % sq->maxsize;
322     }
323   /*
324    * Search pending events vector
325    */
326
327   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   }));
337
338   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   }));
348
349 done:
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  */