FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
session_cli.c
Go to the documentation of this file.
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  */
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 state: %d!", ss->session_state);
64  }
65 
66  vec_free (str);
67 
68  return s;
69 }
70 
71 static clib_error_t *
73  vlib_cli_command_t * cmd)
74 {
76  int verbose = 0, i;
77  stream_session_t *pool;
79  u8 *str = 0;
80 
81  if (!smm->is_enabled)
82  {
83  clib_error_return (0, "session layer is not enabled");
84  }
85 
87  {
88  if (unformat (input, "verbose"))
89  verbose = 1;
90  else
91  break;
92  }
93 
94  for (i = 0; i < vec_len (smm->sessions); i++)
95  {
96  u32 once_per_pool;
97  pool = smm->sessions[i];
98 
99  once_per_pool = 1;
100 
101  if (pool_elts (pool))
102  {
103 
104  vlib_cli_output (vm, "Thread %d: %d active sessions",
105  i, pool_elts (pool));
106  if (verbose)
107  {
108  if (once_per_pool)
109  {
110  str = format (str, "%-40s%-20s%-20s%-15s",
111  "Connection", "Rx fifo", "Tx fifo",
112  "Session Index");
113  vlib_cli_output (vm, "%v", str);
114  vec_reset_length (str);
115  once_per_pool = 0;
116  }
117 
118  /* *INDENT-OFF* */
119  pool_foreach (s, pool,
120  ({
121  vlib_cli_output (vm, "%U", format_stream_session, s, verbose);
122  }));
123  /* *INDENT-ON* */
124  }
125  }
126  else
127  vlib_cli_output (vm, "Thread %d: no active sessions", i);
128  }
129  vec_free (str);
130 
131  return 0;
132 }
133 
134 /* *INDENT-OFF* */
135 VLIB_CLI_COMMAND (show_session_command, static) =
136 {
137  .path = "show session",
138  .short_help = "show session [verbose]",
139  .function = show_session_command_fn,
140 };
141 /* *INDENT-ON* */
142 
143 static clib_error_t *
145  vlib_cli_command_t * cmd)
146 {
148  u32 thread_index = 0;
149  u32 session_index = ~0;
150  stream_session_t *pool, *session;
151  application_t *server;
152 
153  if (!smm->is_enabled)
154  {
155  clib_error_return (0, "session layer is not enabled");
156  }
157 
159  {
160  if (unformat (input, "thread %d", &thread_index))
161  ;
162  else if (unformat (input, "session %d", &session_index))
163  ;
164  else
165  return clib_error_return (0, "unknown input `%U'",
166  format_unformat_error, input);
167  }
168 
169  if (session_index == ~0)
170  return clib_error_return (0, "session <nn> required, but not set.");
171 
172  if (thread_index > vec_len (smm->sessions))
173  return clib_error_return (0, "thread %d out of range [0-%d]",
174  thread_index, vec_len (smm->sessions));
175 
176  pool = smm->sessions[thread_index];
177 
178  if (pool_is_free_index (pool, session_index))
179  return clib_error_return (0, "session %d not active", session_index);
180 
181  session = pool_elt_at_index (pool, session_index);
182  server = application_get (session->app_index);
183 
184  /* Disconnect both app and transport */
185  server->cb_fns.session_disconnect_callback (session);
186 
187  return 0;
188 }
189 
190 /* *INDENT-OFF* */
191 VLIB_CLI_COMMAND (clear_session_command, static) =
192 {
193  .path = "clear session",
194  .short_help = "clear session thread <thread> session <index>",
195  .function = clear_session_command_fn,
196 };
197 /* *INDENT-ON* */
198 
199 static clib_error_t *
201  vlib_cli_command_t * cmd)
202 {
203  u8 is_en = 1;
204 
206  {
207  if (unformat (input, "enable"))
208  is_en = 1;
209  else if (unformat (input, "disable"))
210  is_en = 0;
211  else
212  return clib_error_return (0, "unknown input `%U'",
213  format_unformat_error, input);
214  }
215 
216  return vnet_session_enable_disable (vm, is_en);
217 }
218 
219 /* *INDENT-OFF* */
220 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
221 {
222  .path = "session",
223  .short_help = "session [enable|disable]",
224  .function = session_enable_disable_fn,
225 };
226 /* *INDENT-ON* */
227 
228 /*
229  * fd.io coding-style-patch-verification: ON
230  *
231  * Local Variables:
232  * eval: (c-set-style "gnu")
233  * End:
234  */
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
session_manager_main_t session_manager_main
Definition: session.c:33
static clib_error_t * clear_session_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: session_cli.c:144
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
struct _stream_session_t stream_session_t
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:376
#define clib_error_return(e, args...)
Definition: error.h:111
static clib_error_t * show_session_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: session_cli.c:72
u8 * format_stream_session(u8 *s, va_list *args)
Format stream session as per the following format.
Definition: session_cli.c:27
struct _transport_proto_vft transport_proto_vft_t
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
struct _unformat_input_t unformat_input_t
struct _session_manager_main session_manager_main_t
Definition: session.h:157
clib_error_t * vnet_session_enable_disable(vlib_main_t *vm, u8 is_en)
Definition: session.c:1306
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
vlib_main_t * vm
Definition: buffer.c:276
static u32 stream_session_get_index(stream_session_t *s)
Definition: session.h:327
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
#define clib_warning(format, args...)
Definition: error.h:59
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:238
struct _application application_t
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
unsigned int u32
Definition: types.h:88
transport_proto_vft_t * session_get_transport_vft(u8 type)
Definition: session.c:1236
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
application_t * application_get(u32 index)
Definition: application.c:168
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static clib_error_t * session_enable_disable_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: session_cli.c:200
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:577
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:971
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:109