Improve session debugging
[vpp.git] / src / vnet / session / session_cli.c
1 /*
2  * Copyright (c) 2017 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 #include <vnet/session/application.h>
16 #include <vnet/session/session.h>
17
18 /**
19  * Format stream session as per the following format
20  *
21  * verbose:
22  *   "Connection", "Rx fifo", "Tx fifo", "Session Index"
23  * non-verbose:
24  *   "Connection"
25  */
26 u8 *
27 format_stream_session (u8 * s, va_list * args)
28 {
29   stream_session_t *ss = va_arg (*args, stream_session_t *);
30   int verbose = va_arg (*args, int);
31   transport_proto_vft_t *tp_vft;
32   u8 *str = 0;
33   tp_vft = session_get_transport_vft (ss->session_type);
34
35   if (verbose == 1)
36     str = format (0, "%-10u%-10u%-10lld",
37                   svm_fifo_max_dequeue (ss->server_rx_fifo),
38                   svm_fifo_max_enqueue (ss->server_tx_fifo),
39                   stream_session_get_index (ss));
40
41   if (ss->session_state == SESSION_STATE_READY)
42     {
43       s = format (s, "%U", tp_vft->format_connection, ss->connection_index,
44                   ss->thread_index, verbose);
45       if (verbose == 1)
46         s = format (s, "%v", str);
47     }
48   else if (ss->session_state == SESSION_STATE_LISTENING)
49     {
50       s = format (s, "%-40U%v", tp_vft->format_listener, ss->connection_index,
51                   str);
52     }
53   else if (ss->session_state == SESSION_STATE_CONNECTING)
54     {
55       s = format (s, "%-40U%v", tp_vft->format_half_open,
56                   ss->connection_index, str);
57     }
58   else if (ss->session_state == SESSION_STATE_CLOSED)
59     {
60       s = format (s, "[CL] %-40U%v", tp_vft->format_connection,
61                   ss->connection_index, ss->thread_index, verbose, str);
62     }
63   else
64     {
65       clib_warning ("Session in state: %d!", ss->session_state);
66     }
67
68   vec_free (str);
69
70   return s;
71 }
72
73 static clib_error_t *
74 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
75                          vlib_cli_command_t * cmd)
76 {
77   session_manager_main_t *smm = &session_manager_main;
78   int verbose = 0, i;
79   stream_session_t *pool;
80   stream_session_t *s;
81   u8 *str = 0;
82
83   if (!smm->is_enabled)
84     {
85       return clib_error_return (0, "session layer is not enabled");
86     }
87
88   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
89     {
90       if (unformat (input, "verbose %d", &verbose))
91         ;
92       else if (unformat (input, "verbose"))
93         verbose = 1;
94       else
95         break;
96     }
97
98   for (i = 0; i < vec_len (smm->sessions); i++)
99     {
100       u32 once_per_pool;
101       pool = smm->sessions[i];
102
103       once_per_pool = 1;
104
105       if (pool_elts (pool))
106         {
107
108           vlib_cli_output (vm, "Thread %d: %d active sessions",
109                            i, pool_elts (pool));
110           if (verbose)
111             {
112               if (once_per_pool && verbose == 1)
113                 {
114                   str =
115                     format (str, "%-50s%-15s%-10s%-10s%-10s", "Connection",
116                             "State", "Rx-f", "Tx-f", "S-idx");
117                   vlib_cli_output (vm, "%v", str);
118                   vec_reset_length (str);
119                   once_per_pool = 0;
120                 }
121
122               /* *INDENT-OFF* */
123               pool_foreach (s, pool,
124               ({
125                 vec_reset_length (str);
126                 str = format (str, "%U", format_stream_session, s, verbose);
127                 if (verbose > 1)
128                   {
129                     str = format (str, " Rx fifo: %U", format_svm_fifo,
130                                   s->server_rx_fifo, 1);
131                     str = format (str, " Tx fifo: %U", format_svm_fifo,
132                                   s->server_tx_fifo, 1);
133                   }
134                 vlib_cli_output (vm, "%v", str);
135               }));
136               /* *INDENT-ON* */
137             }
138         }
139       else
140         vlib_cli_output (vm, "Thread %d: no active sessions", i);
141     }
142   vec_free (str);
143
144   return 0;
145 }
146
147 /* *INDENT-OFF* */
148 VLIB_CLI_COMMAND (show_session_command, static) =
149 {
150   .path = "show session",
151   .short_help = "show session [verbose]",
152   .function = show_session_command_fn,
153 };
154 /* *INDENT-ON* */
155
156 static clib_error_t *
157 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
158                           vlib_cli_command_t * cmd)
159 {
160   session_manager_main_t *smm = &session_manager_main;
161   u32 thread_index = 0;
162   u32 session_index = ~0;
163   stream_session_t *pool, *session;
164   application_t *server;
165
166   if (!smm->is_enabled)
167     {
168       return clib_error_return (0, "session layer is not enabled");
169     }
170
171   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
172     {
173       if (unformat (input, "thread %d", &thread_index))
174         ;
175       else if (unformat (input, "session %d", &session_index))
176         ;
177       else
178         return clib_error_return (0, "unknown input `%U'",
179                                   format_unformat_error, input);
180     }
181
182   if (session_index == ~0)
183     return clib_error_return (0, "session <nn> required, but not set.");
184
185   if (thread_index > vec_len (smm->sessions))
186     return clib_error_return (0, "thread %d out of range [0-%d]",
187                               thread_index, vec_len (smm->sessions));
188
189   pool = smm->sessions[thread_index];
190
191   if (pool_is_free_index (pool, session_index))
192     return clib_error_return (0, "session %d not active", session_index);
193
194   session = pool_elt_at_index (pool, session_index);
195   server = application_get (session->app_index);
196
197   /* Disconnect both app and transport */
198   server->cb_fns.session_disconnect_callback (session);
199
200   return 0;
201 }
202
203 /* *INDENT-OFF* */
204 VLIB_CLI_COMMAND (clear_session_command, static) =
205 {
206   .path = "clear session",
207   .short_help = "clear session thread <thread> session <index>",
208   .function = clear_session_command_fn,
209 };
210 /* *INDENT-ON* */
211
212 static clib_error_t *
213 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
214                            vlib_cli_command_t * cmd)
215 {
216   u8 is_en = 1;
217
218   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
219     {
220       if (unformat (input, "enable"))
221         is_en = 1;
222       else if (unformat (input, "disable"))
223         is_en = 0;
224       else
225         return clib_error_return (0, "unknown input `%U'",
226                                   format_unformat_error, input);
227     }
228
229   return vnet_session_enable_disable (vm, is_en);
230 }
231
232 /* *INDENT-OFF* */
233 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
234 {
235   .path = "session",
236   .short_help = "session [enable|disable]",
237   .function = session_enable_disable_fn,
238 };
239 /* *INDENT-ON* */
240
241 /*
242  * fd.io coding-style-patch-verification: ON
243  *
244  * Local Variables:
245  * eval: (c-set-style "gnu")
246  * End:
247  */