svm: allow mq attachments at random offsets
[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 /* *INDENT-OFF* */
56 VLIB_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
64
65 static clib_error_t *
66 clear_session_dbg_clock_cycles_fn (vlib_main_t * vm, unformat_input_t * input,
67                                    vlib_cli_command_t * cmd)
68 {
69   session_dbg_evts_t *sde;
70   u32 thread;
71
72   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
73     return clib_error_return (0, "unknown input `%U'", format_unformat_error,
74                               input);
75
76   for (thread = 0; thread < vec_len (session_dbg_main.wrk); thread++)
77     {
78       sde = &session_dbg_main.wrk[thread];
79       clib_memset (sde, 0, sizeof (session_dbg_evts_t));
80       sde->last_time = vlib_time_now (vlib_mains[thread]);
81       sde->start_time = sde->last_time;
82     }
83
84   return 0;
85 }
86
87
88 /* *INDENT-OFF* */
89 VLIB_CLI_COMMAND (clear_session_clock_cycles_command, static) =
90 {
91   .path = "clear session dbg clock_cycles",
92   .short_help = "clear session dbg clock_cycles",
93   .function = clear_session_dbg_clock_cycles_fn,
94 };
95 /* *INDENT-ON* */
96
97 void
98 session_debug_init (void)
99 {
100   vlib_thread_main_t *vtm = vlib_get_thread_main ();
101   session_dbg_main_t *sdm = &session_dbg_main;
102   u32 num_threads, thread;
103
104   num_threads = vtm->n_vlib_mains;
105
106   vec_validate_aligned (sdm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
107   for (thread = 0; thread < num_threads; thread++)
108     {
109       clib_memset (&sdm->wrk[thread], 0, sizeof (session_dbg_evts_t));
110       sdm->wrk[thread].start_time = vlib_time_now (vlib_mains[thread]);
111     }
112 }
113 #else
114 void
115 session_debug_init (void)
116 {
117 }
118 #endif
119
120 void
121 dump_thread_0_event_queue (void)
122 {
123   vlib_main_t *vm = &vlib_global_main;
124   u32 my_thread_index = vm->thread_index;
125   session_event_t _e, *e = &_e;
126   svm_msg_q_ring_t *ring;
127   session_t *s0;
128   svm_msg_q_msg_t *msg;
129   svm_msg_q_t *mq;
130   int i, index;
131
132   mq = session_main_get_vpp_event_queue (my_thread_index);
133   index = mq->q->head;
134
135   for (i = 0; i < mq->q->cursize; i++)
136     {
137       msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
138       ring = svm_msg_q_ring (mq, msg->ring_index);
139       clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
140
141       switch (e->event_type)
142         {
143         case SESSION_IO_EVT_TX:
144           s0 = session_get_if_valid (e->session_index, my_thread_index);
145           fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
146           break;
147
148         case SESSION_CTRL_EVT_CLOSE:
149           s0 = session_get_from_handle (e->session_handle);
150           fformat (stdout, "[%04d] disconnect session %d\n", i,
151                    s0->session_index);
152           break;
153
154         case SESSION_IO_EVT_BUILTIN_RX:
155           s0 = session_get_if_valid (e->session_index, my_thread_index);
156           fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
157           break;
158
159         case SESSION_CTRL_EVT_RPC:
160           fformat (stdout, "[%04d] RPC call %llx with %llx\n",
161                    i, (u64) (uword) (e->rpc_args.fp),
162                    (u64) (uword) (e->rpc_args.arg));
163           break;
164
165         default:
166           fformat (stdout, "[%04d] unhandled event type %d\n",
167                    i, e->event_type);
168           break;
169         }
170
171       index++;
172
173       if (index == mq->q->maxsize)
174         index = 0;
175     }
176 }
177
178 static u8
179 session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
180 {
181   session_t *s;
182   switch (e->event_type)
183     {
184     case SESSION_IO_EVT_RX:
185     case SESSION_IO_EVT_TX:
186     case SESSION_IO_EVT_BUILTIN_RX:
187     case SESSION_IO_EVT_BUILTIN_TX:
188     case SESSION_IO_EVT_TX_FLUSH:
189       if (e->session_index == f->shr->master_session_index)
190         return 1;
191       break;
192     case SESSION_CTRL_EVT_CLOSE:
193       break;
194     case SESSION_CTRL_EVT_RPC:
195       s = session_get_from_handle (e->session_handle);
196       if (!s)
197         {
198           clib_warning ("session has event but doesn't exist!");
199           break;
200         }
201       if (s->rx_fifo == f || s->tx_fifo == f)
202         return 1;
203       break;
204     default:
205       break;
206     }
207   return 0;
208 }
209
210 u8
211 session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
212 {
213   session_evt_elt_t *elt;
214   session_worker_t *wrk;
215   int i, index, found = 0;
216   svm_msg_q_msg_t *msg;
217   svm_msg_q_ring_t *ring;
218   svm_msg_q_t *mq;
219   u8 thread_index;
220
221   ASSERT (e);
222   thread_index = f->master_thread_index;
223   wrk = session_main_get_worker (thread_index);
224
225   /*
226    * Search evt queue
227    */
228   mq = wrk->vpp_event_queue;
229   index = mq->q->head;
230   for (i = 0; i < mq->q->cursize; i++)
231     {
232       msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
233       ring = svm_msg_q_ring (mq, msg->ring_index);
234       clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
235       found = session_node_cmp_event (e, f);
236       if (found)
237         return 1;
238       index = (index + 1) % mq->q->maxsize;
239     }
240   /*
241    * Search pending events vector
242    */
243
244   /* *INDENT-OFF* */
245   clib_llist_foreach (wrk->event_elts, evt_list,
246                       pool_elt_at_index (wrk->event_elts, wrk->new_head),
247                       elt, ({
248     found = session_node_cmp_event (&elt->evt, f);
249     if (found)
250       {
251         clib_memcpy_fast (e, &elt->evt, sizeof (*e));
252         goto done;
253       }
254   }));
255   /* *INDENT-ON* */
256
257   /* *INDENT-OFF* */
258   clib_llist_foreach (wrk->event_elts, evt_list,
259                       pool_elt_at_index (wrk->event_elts, wrk->old_head),
260                       elt, ({
261     found = session_node_cmp_event (&elt->evt, f);
262     if (found)
263       {
264         clib_memcpy_fast (e, &elt->evt, sizeof (*e));
265         goto done;
266       }
267   }));
268   /* *INDENT-ON* */
269
270 done:
271   return found;
272 }
273
274 /*
275  * fd.io coding-style-patch-verification: ON
276  *
277  * Local Variables:
278  * eval: (c-set-style "gnu")
279  * End:
280  */