udp: add cli to dump registered ports 47/36347/2
authorBenoît Ganne <bganne@cisco.com>
Wed, 8 Jun 2022 08:23:43 +0000 (10:23 +0200)
committerFlorin Coras <florin.coras@gmail.com>
Wed, 8 Jun 2022 16:28:29 +0000 (16:28 +0000)
Type: improvement

Change-Id: Ic949e3136a7cf27011d098a50e91920f83226ea9
Signed-off-by: Benoît Ganne <bganne@cisco.com>
src/vnet/udp/udp_cli.c

index 97760f4..3b7bb38 100644 (file)
@@ -13,6 +13,9 @@
  * limitations under the License.
  */
 
+#include <vppinfra/error.h>
+#include <vppinfra/format.h>
+#include <vppinfra/format_table.h>
 #include <vnet/udp/udp.h>
 #include <vnet/session/session_types.h>
 
@@ -171,6 +174,96 @@ VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
 };
 /* *INDENT-ON* */
 
+static void
+table_format_udp_port_ (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
+                       int port, int bind, int is_ip4)
+{
+  const udp_dst_port_info_t *pi = udp_get_dst_port_info (um, port, is_ip4);
+  if (!pi)
+    return;
+  if (bind && ~0 == pi->node_index)
+    return;
+  table_format_cell (t, *c, 0, "%d", pi->dst_port);
+  table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
+  table_format_cell (t, *c, 2, ~0 == pi->node_index ? "none" : "%U",
+                    format_vlib_node_name, vm, pi->node_index);
+  table_format_cell (t, *c, 3, "%s", pi->name);
+  (*c)++;
+}
+
+static void
+table_format_udp_port (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
+                      int port, int bind, int ip4, int ip6)
+{
+  if (ip4)
+    table_format_udp_port_ (vm, um, t, c, port, bind, 1 /* is_ip4 */);
+  if (ip6)
+    table_format_udp_port_ (vm, um, t, c, port, bind, 0 /* is_ip4 */);
+}
+
+static clib_error_t *
+show_udp_ports (vlib_main_t *vm, unformat_input_t *input,
+               vlib_cli_command_t *cmd)
+{
+  table_t table = {}, *t = &table;
+  udp_main_t *um = &udp_main;
+  clib_error_t *err = 0;
+  int ip4 = 1, ip6 = 1;
+  int port = -1;
+  int bind = 1;
+  int c = 0;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "ip4"))
+       ip6 = 0;
+      else if (unformat (input, "ip6"))
+       ip4 = 0;
+      else if (unformat (input, "bind"))
+       bind = 1;
+      else if (unformat (input, "all"))
+       bind = 0;
+      else if (unformat (input, "%d", &port))
+       ;
+      else
+       {
+         err = clib_error_return (0, "unknown input `%U'",
+                                  format_unformat_error, input);
+         goto out;
+       }
+    }
+
+  table_add_header_col (t, 4, "port", "proto", "node", "desc");
+
+  if (port > 65535)
+    {
+      err = clib_error_return (0, "wrong port %d", port);
+      goto out;
+    }
+  else if (port < 0)
+    {
+      for (port = 0; port < 65536; port++)
+       table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
+    }
+  else
+    {
+      table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
+    }
+
+  vlib_cli_output (vm, "%U", format_table, t);
+
+out:
+  table_free (t);
+  return err;
+}
+
+VLIB_CLI_COMMAND (show_udp_ports_cmd, static) = {
+  .path = "show udp ports",
+  .function = show_udp_ports,
+  .short_help = "show udp ports [ip4|ip6] [bind|all|<port>]",
+  .is_mp_safe = 1,
+};
+
 /*
  * fd.io coding-style-patch-verification: ON
  *