2 * Copyright (c) 2020 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vppinfra/error.h>
17 #include <vppinfra/format.h>
18 #include <vppinfra/format_table.h>
19 #include <vnet/udp/udp.h>
20 #include <vnet/session/session_types.h>
23 format_udp_connection_id (u8 * s, va_list * args)
25 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
29 s = format (s, "[%u:%u][%s] %U:%d->%U:%d", uc->c_thread_index,
30 uc->c_s_index, "U", format_ip4_address, &uc->c_lcl_ip4,
31 clib_net_to_host_u16 (uc->c_lcl_port), format_ip4_address,
32 &uc->c_rmt_ip4, clib_net_to_host_u16 (uc->c_rmt_port));
34 s = format (s, "[%u:%u][%s] %U:%d->%U:%d", uc->c_thread_index,
35 uc->c_s_index, "U", format_ip6_address, &uc->c_lcl_ip6,
36 clib_net_to_host_u16 (uc->c_lcl_port), format_ip6_address,
37 &uc->c_rmt_ip6, clib_net_to_host_u16 (uc->c_rmt_port));
41 static const char *udp_connection_flags_str[] = {
42 #define _(sym, str) str,
43 foreach_udp_connection_flag
48 format_udp_connection_flags (u8 * s, va_list * args)
50 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
53 for (i = 0; i < UDP_CONN_N_FLAGS; i++)
54 if (uc->flags & (1 << i))
56 for (i = 0; i < last; i++)
58 if (uc->flags & (1 << i))
59 s = format (s, "%s, ", udp_connection_flags_str[i]);
62 s = format (s, "%s", udp_connection_flags_str[last]);
67 format_udp_vars (u8 * s, va_list * args)
69 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
70 s = format (s, " index %u flags: %U", uc->c_c_index,
71 format_udp_connection_flags, uc);
73 if (!(uc->flags & UDP_CONN_F_LISTEN))
79 format_udp_connection (u8 * s, va_list * args)
81 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
82 u32 verbose = va_arg (*args, u32);
85 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_udp_connection_id, uc);
88 s = format (s, "%-" SESSION_CLI_STATE_LEN "s",
89 (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
91 s = format (s, "\n%U", format_udp_vars, uc);
97 udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
99 udp_main_t *um = &udp_main;
102 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
104 if (unformat (input, "mtu %u", &tmp))
105 um->default_mtu = tmp;
106 else if (unformat (input, "icmp-unreachable-disabled"))
107 um->icmp_send_unreachable_disabled = 1;
109 return clib_error_return (0, "unknown input `%U'",
110 format_unformat_error, input);
115 VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
117 static clib_error_t *
118 show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
119 vlib_cli_command_t * cmd_arg)
121 udp_main_t *um = vnet_get_udp_main ();
123 clib_error_t *error = NULL;
125 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
126 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
129 udp_dst_port_info_t *port_info;
130 if (um->punt_unknown4)
132 vlib_cli_output (vm, "IPv4 UDP punt: enabled");
137 vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
139 if (udp_is_valid_dst_port (port_info->dst_port, 1))
141 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
144 s = format (s, "%c", 0);
145 vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
148 if (um->punt_unknown6)
150 vlib_cli_output (vm, "IPv6 UDP punt: enabled");
155 vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
157 if (udp_is_valid_dst_port (port_info->dst_port, 01))
159 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
162 s = format (s, "%c", 0);
163 vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
169 VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
171 .path = "show udp punt",
172 .short_help = "show udp punt [ipv4|ipv6]",
173 .function = show_udp_punt_fn,
178 table_format_udp_port_ (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
179 int port, int bind, int is_ip4)
181 const udp_dst_port_info_t *pi = udp_get_dst_port_info (um, port, is_ip4);
184 if (bind && ~0 == pi->node_index)
186 table_format_cell (t, *c, 0, "%d", pi->dst_port);
187 table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
188 table_format_cell (t, *c, 2, ~0 == pi->node_index ? "none" : "%U",
189 format_vlib_node_name, vm, pi->node_index);
190 table_format_cell (t, *c, 3, "%s", pi->name);
195 table_format_udp_port (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
196 int port, int bind, int ip4, int ip6)
199 table_format_udp_port_ (vm, um, t, c, port, bind, 1 /* is_ip4 */);
201 table_format_udp_port_ (vm, um, t, c, port, bind, 0 /* is_ip4 */);
204 static clib_error_t *
205 show_udp_ports (vlib_main_t *vm, unformat_input_t *input,
206 vlib_cli_command_t *cmd)
208 table_t table = {}, *t = &table;
209 udp_main_t *um = &udp_main;
210 clib_error_t *err = 0;
211 int ip4 = 1, ip6 = 1;
216 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
218 if (unformat (input, "ip4"))
220 else if (unformat (input, "ip6"))
222 else if (unformat (input, "bind"))
224 else if (unformat (input, "all"))
226 else if (unformat (input, "%d", &port))
230 err = clib_error_return (0, "unknown input `%U'",
231 format_unformat_error, input);
236 table_add_header_col (t, 4, "port", "proto", "node", "desc");
240 err = clib_error_return (0, "wrong port %d", port);
245 for (port = 0; port < 65536; port++)
246 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
250 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
253 vlib_cli_output (vm, "%U", format_table, t);
260 VLIB_CLI_COMMAND (show_udp_ports_cmd, static) = {
261 .path = "show udp ports",
262 .function = show_udp_ports,
263 .short_help = "show udp ports [ip4|ip6] [bind|all|<port>]",
268 * fd.io coding-style-patch-verification: ON
271 * eval: (c-set-style "gnu")