VPP-598: tcp stack initial commit
[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
34   tp_vft = session_get_transport_vft (ss->session_type);
35
36   if (verbose)
37     str = format (0, "%-20llp%-20llp%-15lld", ss->server_rx_fifo,
38                   ss->server_tx_fifo, stream_session_get_index (ss));
39
40   if (ss->session_state == SESSION_STATE_READY)
41     {
42       s = format (s, "%-40U%v", tp_vft->format_connection,
43                   ss->connection_index, ss->thread_index, str);
44     }
45   else if (ss->session_state == SESSION_STATE_LISTENING)
46     {
47       s = format (s, "%-40U%v", tp_vft->format_listener, ss->connection_index,
48                   str);
49     }
50   else if (ss->session_state == SESSION_STATE_READY)
51     {
52       s =
53         format (s, "%-40U%v", tp_vft->format_half_open, ss->connection_index,
54                 str);
55     }
56   else if (ss->session_state == SESSION_STATE_CLOSED)
57     {
58       s = format (s, "[CL] %-40U%v", tp_vft->format_connection,
59                   ss->connection_index, ss->thread_index, str);
60     }
61   else
62     {
63       clib_warning ("Session in unknown state!");
64     }
65
66   vec_free (str);
67
68   return s;
69 }
70
71 static clib_error_t *
72 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
73                          vlib_cli_command_t * cmd)
74 {
75   session_manager_main_t *smm = &session_manager_main;
76   int verbose = 0, i;
77   stream_session_t *pool;
78   stream_session_t *s;
79   u8 *str = 0;
80
81   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
82     {
83       if (unformat (input, "verbose"))
84         verbose = 1;
85       else
86         break;
87     }
88
89   for (i = 0; i < vec_len (smm->sessions); i++)
90     {
91       u32 once_per_pool;
92       pool = smm->sessions[i];
93
94       once_per_pool = 1;
95
96       if (pool_elts (pool))
97         {
98
99           vlib_cli_output (vm, "Thread %d: %d active sessions",
100                            i, pool_elts (pool));
101           if (verbose)
102             {
103               if (once_per_pool)
104                 {
105                   str = format (str, "%-40s%-20s%-20s%-15s",
106                                 "Connection", "Rx fifo", "Tx fifo",
107                                 "Session Index");
108                   vlib_cli_output (vm, "%v", str);
109                   vec_reset_length (str);
110                   once_per_pool = 0;
111                 }
112
113               /* *INDENT-OFF* */
114               pool_foreach (s, pool,
115               ({
116                 vlib_cli_output (vm, "%U", format_stream_session, s, verbose);
117               }));
118               /* *INDENT-ON* */
119             }
120         }
121       else
122         vlib_cli_output (vm, "Thread %d: no active sessions", i);
123     }
124   vec_free (str);
125
126   return 0;
127 }
128
129 VLIB_CLI_COMMAND (show_uri_command, static) =
130 {
131 .path = "show session",.short_help = "show session [verbose]",.function =
132     show_session_command_fn,};
133
134
135 static clib_error_t *
136 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
137                           vlib_cli_command_t * cmd)
138 {
139   session_manager_main_t *smm = &session_manager_main;
140   u32 thread_index = 0;
141   u32 session_index = ~0;
142   stream_session_t *pool, *session;
143   application_t *server;
144
145   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
146     {
147       if (unformat (input, "thread %d", &thread_index))
148         ;
149       else if (unformat (input, "session %d", &session_index))
150         ;
151       else
152         return clib_error_return (0, "unknown input `%U'",
153                                   format_unformat_error, input);
154     }
155
156   if (session_index == ~0)
157     return clib_error_return (0, "session <nn> required, but not set.");
158
159   if (thread_index > vec_len (smm->sessions))
160     return clib_error_return (0, "thread %d out of range [0-%d]",
161                               thread_index, vec_len (smm->sessions));
162
163   pool = smm->sessions[thread_index];
164
165   if (pool_is_free_index (pool, session_index))
166     return clib_error_return (0, "session %d not active", session_index);
167
168   session = pool_elt_at_index (pool, session_index);
169   server = application_get (session->app_index);
170
171   /* Disconnect both app and transport */
172   server->cb_fns.session_disconnect_callback (session);
173
174   return 0;
175 }
176
177 VLIB_CLI_COMMAND (clear_uri_session_command, static) =
178 {
179 .path = "clear session",.short_help =
180     "clear session thread <thread> session <index>",.function =
181     clear_session_command_fn,};
182
183 /*
184  * fd.io coding-style-patch-verification: ON
185  *
186  * Local Variables:
187  * eval: (c-set-style "gnu")
188  * End:
189  */