FD.io VPP  v18.07-34-g55fbdb9
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 u8 *
19 format_stream_session_fifos (u8 * s, va_list * args)
20 {
21  stream_session_t *ss = va_arg (*args, stream_session_t *);
22  int verbose = va_arg (*args, int);
23  session_fifo_event_t _e, *e = &_e;
24  u8 found;
25 
26  if (!ss->server_rx_fifo || !ss->server_tx_fifo)
27  return s;
28 
29  s = format (s, " Rx fifo: %U", format_svm_fifo, ss->server_rx_fifo, 1);
30  if (verbose > 2 && ss->server_rx_fifo->has_event)
31  {
32  found = session_node_lookup_fifo_event (ss->server_rx_fifo, e);
33  s = format (s, " session node event: %s\n",
34  found ? "found" : "not found");
35  }
36  s = format (s, " Tx fifo: %U", format_svm_fifo, ss->server_tx_fifo, 1);
37  if (verbose > 2 && ss->server_tx_fifo->has_event)
38  {
39  found = session_node_lookup_fifo_event (ss->server_tx_fifo, e);
40  s = format (s, " session node event: %s\n",
41  found ? "found" : "not found");
42  }
43  return s;
44 }
45 
46 /**
47  * Format stream session as per the following format
48  *
49  * verbose:
50  * "Connection", "Rx fifo", "Tx fifo", "Session Index"
51  * non-verbose:
52  * "Connection"
53  */
54 u8 *
55 format_stream_session (u8 * s, va_list * args)
56 {
57  stream_session_t *ss = va_arg (*args, stream_session_t *);
58  int verbose = va_arg (*args, int);
59  transport_proto_vft_t *tp_vft;
60  u8 *str = 0;
62 
63  if (verbose == 1 && ss->session_state >= SESSION_STATE_ACCEPTING)
64  str = format (0, "%-10u%-10u%-10lld",
65  svm_fifo_max_dequeue (ss->server_rx_fifo),
66  svm_fifo_max_enqueue (ss->server_tx_fifo),
67  session_get_index (ss));
68 
69  if (ss->session_state >= SESSION_STATE_ACCEPTING)
70  {
71  s = format (s, "%U", tp_vft->format_connection, ss->connection_index,
72  ss->thread_index, verbose);
73  if (verbose == 1)
74  s = format (s, "%v", str);
75  if (verbose > 1)
76  s = format (s, "%U", format_stream_session_fifos, ss, verbose);
77  }
78  else if (ss->session_state == SESSION_STATE_LISTENING)
79  {
80  s = format (s, "%-40U%v", tp_vft->format_listener, ss->connection_index,
81  str);
82  if (verbose > 1)
83  s = format (s, "\n%U", format_stream_session_fifos, ss, verbose);
84  }
85  else if (ss->session_state == SESSION_STATE_CONNECTING)
86  {
87  s = format (s, "%-40U%v", tp_vft->format_half_open,
88  ss->connection_index, str);
89  }
90  else
91  {
92  clib_warning ("Session in state: %d!", ss->session_state);
93  }
94  vec_free (str);
95 
96  return s;
97 }
98 
99 uword
101 {
102  u8 *proto = va_arg (*args, u8 *);
103  ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
104  ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
105  u16 *lcl_port = va_arg (*args, u16 *);
106  u16 *rmt_port = va_arg (*args, u16 *);
107  u8 *is_ip4 = va_arg (*args, u8 *);
108  u8 tuple_is_set = 0;
109 
110  memset (lcl, 0, sizeof (*lcl));
111  memset (rmt, 0, sizeof (*rmt));
112 
113  if (unformat (input, "tcp"))
114  {
115  *proto = TRANSPORT_PROTO_TCP;
116  }
117  if (unformat (input, "udp"))
118  {
119  *proto = TRANSPORT_PROTO_UDP;
120  }
121  if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
122  lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
123  {
124  *is_ip4 = 1;
125  tuple_is_set = 1;
126  }
127  else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
128  lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
129  {
130  *is_ip4 = 0;
131  tuple_is_set = 1;
132  }
133 
134  return tuple_is_set;
135 }
136 
137 uword
138 unformat_stream_session (unformat_input_t * input, va_list * args)
139 {
140  stream_session_t **result = va_arg (*args, stream_session_t **);
141  stream_session_t *s;
142  u8 proto = ~0;
143  ip46_address_t lcl, rmt;
144  u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
145  u8 is_ip4 = 0;
146 
147  if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
148  &lcl_port, &rmt_port, &is_ip4))
149  return 0;
150 
151  if (is_ip4)
152  s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
153  clib_host_to_net_u16 (lcl_port),
154  clib_host_to_net_u16 (rmt_port), proto);
155  else
156  s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
157  clib_host_to_net_u16 (lcl_port),
158  clib_host_to_net_u16 (rmt_port), proto);
159  if (s)
160  {
161  *result = s;
162  session_pool_remove_peeker (s->thread_index);
163  return 1;
164  }
165  return 0;
166 }
167 
168 uword
170 {
171  transport_connection_t **result = va_arg (*args, transport_connection_t **);
172  u32 suggested_proto = va_arg (*args, u32);
174  u8 proto = ~0;
175  ip46_address_t lcl, rmt;
176  u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
177  u8 is_ip4 = 0;
178 
179  if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
180  &lcl_port, &rmt_port, &is_ip4))
181  return 0;
182 
183  proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
184  if (proto == (u8) ~ 0)
185  return 0;
186  if (is_ip4)
187  tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
188  clib_host_to_net_u16 (lcl_port),
189  clib_host_to_net_u16 (rmt_port), proto);
190  else
191  tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
192  clib_host_to_net_u16 (lcl_port),
193  clib_host_to_net_u16 (rmt_port), proto);
194 
195  if (tc)
196  {
197  *result = tc;
198  return 1;
199  }
200  return 0;
201 }
202 
203 static clib_error_t *
205  vlib_cli_command_t * cmd)
206 {
208  u8 *str = 0, one_session = 0, do_listeners = 0, sst, *app_name;
209  int verbose = 0, i;
210  stream_session_t *pool, *s;
211  u32 transport_proto = ~0;
212 
213  if (!smm->is_enabled)
214  {
215  return clib_error_return (0, "session layer is not enabled");
216  }
217 
219  {
220  if (unformat (input, "verbose %d", &verbose))
221  ;
222  else if (unformat (input, "verbose"))
223  verbose = 1;
224  else if (unformat (input, "listeners %U", unformat_transport_proto,
225  &transport_proto))
226  do_listeners = 1;
227  else if (unformat (input, "%U", unformat_stream_session, &s))
228  {
229  one_session = 1;
230  }
231  else
232  return clib_error_return (0, "unknown input `%U'",
233  format_unformat_error, input);
234  }
235 
236  if (one_session)
237  {
238  vlib_cli_output (vm, "%U", format_stream_session, s, 3);
239  return 0;
240  }
241 
242  if (do_listeners)
243  {
244  sst = session_type_from_proto_and_ip (transport_proto, 1);
245  vlib_cli_output (vm, "%-40s%-24s%-10s", "Listener", "App", "S-idx");
246  /* *INDENT-OFF* */
247  pool_foreach (s, smm->sessions[0], ({
248  if (s->session_state != SESSION_STATE_LISTENING
249  || s->session_type != sst)
250  continue;
251  app_name = application_name_from_index (s->app_index);
252  vlib_cli_output (vm, "%U%-25v%-10u", format_stream_session, s, 1,
253  app_name, s->session_index);
254  vec_free (app_name);
255  }));
256  /* *INDENT-ON* */
257  return 0;
258  }
259 
260  for (i = 0; i < vec_len (smm->sessions); i++)
261  {
262  u32 once_per_pool;
263  pool = smm->sessions[i];
264 
265  once_per_pool = 1;
266 
267  if (pool_elts (pool))
268  {
269 
270  vlib_cli_output (vm, "Thread %d: %d active sessions",
271  i, pool_elts (pool));
272  if (verbose)
273  {
274  if (once_per_pool && verbose == 1)
275  {
276  str = format (str, "%-50s%-15s%-10s%-10s%-10s",
277  "Connection", "State", "Rx-f", "Tx-f",
278  "S-idx");
279  vlib_cli_output (vm, "%v", str);
280  vec_reset_length (str);
281  once_per_pool = 0;
282  }
283 
284  /* *INDENT-OFF* */
285  pool_foreach (s, pool,
286  ({
287  vec_reset_length (str);
288  str = format (str, "%U", format_stream_session, s, verbose);
289  vlib_cli_output (vm, "%v", str);
290  }));
291  /* *INDENT-ON* */
292  }
293  }
294  else
295  vlib_cli_output (vm, "Thread %d: no active sessions", i);
296  vec_reset_length (str);
297  }
298  vec_free (str);
299 
300  return 0;
301 }
302 
303 /* *INDENT-OFF* */
304 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
305 {
306  .path = "show session",
307  .short_help = "show session [verbose [nnn]]",
308  .function = show_session_command_fn,
309 };
310 /* *INDENT-ON* */
311 
312 static int
314 {
315  application_t *server = application_get (s->app_index);
316  server->cb_fns.session_disconnect_callback (s);
317  return 0;
318 }
319 
320 static clib_error_t *
322  vlib_cli_command_t * cmd)
323 {
325  u32 thread_index = 0, clear_all = 0;
326  u32 session_index = ~0;
327  stream_session_t **pool, *session;
328 
329  if (!smm->is_enabled)
330  {
331  return clib_error_return (0, "session layer is not enabled");
332  }
333 
335  {
336  if (unformat (input, "thread %d", &thread_index))
337  ;
338  else if (unformat (input, "session %d", &session_index))
339  ;
340  else if (unformat (input, "all"))
341  clear_all = 1;
342  else
343  return clib_error_return (0, "unknown input `%U'",
344  format_unformat_error, input);
345  }
346 
347  if (!clear_all && session_index == ~0)
348  return clib_error_return (0, "session <nn> required, but not set.");
349 
350  if (session_index != ~0)
351  {
352  session = session_get_if_valid (session_index, thread_index);
353  if (!session)
354  return clib_error_return (0, "no session %d on thread %d",
355  session_index, thread_index);
356  clear_session (session);
357  }
358 
359  if (clear_all)
360  {
361  /* *INDENT-OFF* */
362  vec_foreach (pool, smm->sessions)
363  {
364  pool_foreach(session, *pool, ({
365  clear_session (session);
366  }));
367  };
368  /* *INDENT-ON* */
369  }
370 
371  return 0;
372 }
373 
374 /* *INDENT-OFF* */
375 VLIB_CLI_COMMAND (clear_session_command, static) =
376 {
377  .path = "clear session",
378  .short_help = "clear session thread <thread> session <index>",
379  .function = clear_session_command_fn,
380 };
381 /* *INDENT-ON* */
382 
383 static clib_error_t *
385  unformat_input_t * input,
386  vlib_cli_command_t * cmd)
387 {
388  stream_session_t *s = 0;
389  u8 is_rx = 0, *str = 0;
390 
392  {
393  if (unformat (input, "%U", unformat_stream_session, &s))
394  ;
395  else if (unformat (input, "rx"))
396  is_rx = 1;
397  else if (unformat (input, "tx"))
398  is_rx = 0;
399  else
400  return clib_error_return (0, "unknown input `%U'",
401  format_unformat_error, input);
402  }
403 
404  if (!SVM_FIFO_TRACE)
405  {
406  vlib_cli_output (vm, "fifo tracing not enabled");
407  return 0;
408  }
409 
410  if (!s)
411  {
412  vlib_cli_output (vm, "could not find session");
413  return 0;
414  }
415 
416  str = is_rx ?
417  svm_fifo_dump_trace (str, s->server_rx_fifo) :
418  svm_fifo_dump_trace (str, s->server_tx_fifo);
419 
420  vlib_cli_output (vm, "%v", str);
421  return 0;
422 }
423 
424 /* *INDENT-OFF* */
425 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
426 {
427  .path = "show session fifo trace",
428  .short_help = "show session fifo trace <session>",
430 };
431 /* *INDENT-ON* */
432 
433 static clib_error_t *
435  vlib_cli_command_t * cmd)
436 {
437  stream_session_t *s = 0;
438  u8 is_rx = 0, *str = 0;
439 
441  {
442  if (unformat (input, "%U", unformat_stream_session, &s))
443  ;
444  else if (unformat (input, "rx"))
445  is_rx = 1;
446  else
447  return clib_error_return (0, "unknown input `%U'",
448  format_unformat_error, input);
449  }
450 
451  if (!SVM_FIFO_TRACE)
452  {
453  vlib_cli_output (vm, "fifo tracing not enabled");
454  return 0;
455  }
456 
457  if (!s)
458  {
459  vlib_cli_output (vm, "could not find session");
460  return 0;
461  }
462 
463  str = is_rx ?
464  svm_fifo_replay (str, s->server_rx_fifo, 0, 1) :
465  svm_fifo_replay (str, s->server_tx_fifo, 0, 1);
466 
467  vlib_cli_output (vm, "%v", str);
468  return 0;
469 }
470 
471 /* *INDENT-OFF* */
472 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
473 {
474  .path = "session replay fifo",
475  .short_help = "session replay fifo <session>",
476  .function = session_replay_fifo_command_fn,
477 };
478 /* *INDENT-ON* */
479 
480 static clib_error_t *
482  vlib_cli_command_t * cmd)
483 {
484  unformat_input_t _line_input, *line_input = &_line_input;
485  u8 is_en = 1;
486  clib_error_t *error;
487 
488  if (!unformat_user (input, unformat_line_input, line_input))
489  return clib_error_return (0, "expected enable | disable");
490 
491  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
492  {
493  if (unformat (line_input, "enable"))
494  is_en = 1;
495  else if (unformat (line_input, "disable"))
496  is_en = 0;
497  else
498  {
499  error = clib_error_return (0, "unknown input `%U'",
500  format_unformat_error, line_input);
501  unformat_free (line_input);
502  return error;
503  }
504  }
505 
506  unformat_free (line_input);
507  return vnet_session_enable_disable (vm, is_en);
508 }
509 
510 /* *INDENT-OFF* */
511 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
512 {
513  .path = "session",
514  .short_help = "session [enable|disable]",
515  .function = session_enable_disable_fn,
516 };
517 /* *INDENT-ON* */
518 
519 /*
520  * fd.io coding-style-patch-verification: ON
521  *
522  * Local Variables:
523  * eval: (c-set-style "gnu")
524  * End:
525  */
struct _transport_connection transport_connection_t
format_function_t format_svm_fifo
Definition: svm_fifo.h:167
session_manager_main_t session_manager_main
Definition: session.c:27
struct _transport_proto_vft transport_proto_vft_t
int i
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
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:321
static u32 svm_fifo_max_enqueue(svm_fifo_t *f)
Definition: svm_fifo.h:111
static void session_pool_remove_peeker(u32 thread_index)
Definition: session.h:421
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
unsigned char u8
Definition: types.h:56
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
transport_connection_t * session_lookup_connection6(u32 fib_index, ip6_address_t *lcl, ip6_address_t *rmt, u16 lcl_port, u16 rmt_port, u8 proto)
Lookup connection with ip6 and transport layer information.
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:443
unformat_function_t unformat_ip4_address
Definition: format.h:76
static u32 svm_fifo_max_dequeue(svm_fifo_t *f)
Definition: svm_fifo.h:105
#define clib_error_return(e, args...)
Definition: error.h:99
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:204
unsigned int u32
Definition: types.h:88
struct _stream_session_t stream_session_t
u8 * format_stream_session(u8 *s, va_list *args)
Format stream session as per the following format.
Definition: session_cli.c:55
u8 session_node_lookup_fifo_event(svm_fifo_t *f, session_fifo_event_t *e)
Definition: session_node.c:844
transport_connection_t * session_lookup_connection4(u32 fib_index, ip4_address_t *lcl, ip4_address_t *rmt, u16 lcl_port, u16 rmt_port, u8 proto)
Lookup connection with ip4 and transport layer information.
unformat_function_t unformat_line_input
Definition: format.h:282
transport_proto_vft_t * transport_protocol_get_vft(transport_proto_t transport_proto)
Get transport virtual function table.
Definition: transport.c:191
static transport_proto_t session_get_transport_proto(stream_session_t *s)
Definition: session.h:373
struct _unformat_input_t unformat_input_t
static u32 session_get_index(stream_session_t *s)
Definition: session.h:474
unsigned short u16
Definition: types.h:57
stream_session_t * session_lookup_safe6(u32 fib_index, ip6_address_t *lcl, ip6_address_t *rmt, u16 lcl_port, u16 rmt_port, u8 proto)
Lookup session with ip6 and transport layer information.
uword unformat_transport_connection(unformat_input_t *input, va_list *args)
Definition: session_cli.c:169
static stream_session_t * session_get_if_valid(u64 si, u32 thread_index)
Definition: session.h:297
struct _session_manager_main session_manager_main_t
Definition: session.h:153
u8 * svm_fifo_replay(u8 *s, svm_fifo_t *f, u8 no_read, u8 verbose)
Definition: svm_fifo.c:90
clib_error_t * vnet_session_enable_disable(vlib_main_t *vm, u8 is_en)
Definition: session.c:1470
uword unformat_stream_session_id(unformat_input_t *input, va_list *args)
Definition: session_cli.c:100
static int clear_session(stream_session_t *s)
Definition: session_cli.c:313
unformat_function_t unformat_ip6_address
Definition: format.h:97
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
static session_type_t session_type_from_proto_and_ip(transport_proto_t proto, u8 is_ip4)
Definition: session.h:386
vlib_main_t * vm
Definition: buffer.c:294
uword unformat_stream_session(unformat_input_t *input, va_list *args)
Definition: session_cli.c:138
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
u8 * format_stream_session_fifos(u8 *s, va_list *args)
Definition: session_cli.c:19
#define clib_warning(format, args...)
Definition: error.h:59
stream_session_t * session_lookup_safe4(u32 fib_index, ip4_address_t *lcl, ip4_address_t *rmt, u16 lcl_port, u16 rmt_port, u8 proto)
Lookup session with ip4 and transport layer information.
struct _application application_t
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define SVM_FIFO_TRACE
Definition: svm_fifo.h:39
uword unformat_transport_proto(unformat_input_t *input, va_list *args)
Definition: transport.c:90
u8 * svm_fifo_dump_trace(u8 *s, svm_fifo_t *f)
Definition: svm_fifo.c:66
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
application_t * application_get(u32 index)
Definition: application.c:386
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:162
static clib_error_t * session_replay_fifo_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: session_cli.c:434
static clib_error_t * show_session_fifo_trace_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: session_cli.c:384
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
#define vec_foreach(var, vec)
Vector iterator.
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:481
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:681
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128