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 *);
71 s = format (s, " index %u flags: %U", uc->c_c_index,
72 format_udp_connection_flags, uc);
73 if (!(uc->flags & UDP_CONN_F_LISTEN))
74 s = format (s, " \n sw_if_index: %d, mss: %u\n", uc->sw_if_index, uc->mss);
80 format_udp_connection (u8 * s, va_list * args)
82 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
83 u32 verbose = va_arg (*args, u32);
86 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_udp_connection_id, uc);
89 s = format (s, "%-" SESSION_CLI_STATE_LEN "s",
90 (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
92 s = format (s, "\n%U", format_udp_vars, uc);
98 udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
100 udp_main_t *um = &udp_main;
103 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
105 if (unformat (input, "mtu %u", &tmp))
106 um->default_mtu = tmp;
107 else if (unformat (input, "icmp-unreachable-disabled"))
108 um->icmp_send_unreachable_disabled = 1;
110 return clib_error_return (0, "unknown input `%U'",
111 format_unformat_error, input);
116 VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
118 static clib_error_t *
119 show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
120 vlib_cli_command_t * cmd_arg)
122 udp_main_t *um = vnet_get_udp_main ();
124 clib_error_t *error = NULL;
126 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
127 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
130 udp_dst_port_info_t *port_info;
131 if (um->punt_unknown4)
133 vlib_cli_output (vm, "IPv4 UDP punt: enabled");
138 vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
140 if (udp_is_valid_dst_port (port_info->dst_port, 1))
142 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
145 s = format (s, "%c", 0);
146 vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
149 if (um->punt_unknown6)
151 vlib_cli_output (vm, "IPv6 UDP punt: enabled");
156 vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
158 if (udp_is_valid_dst_port (port_info->dst_port, 01))
160 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
163 s = format (s, "%c", 0);
164 vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
170 VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
172 .path = "show udp punt",
173 .short_help = "show udp punt [ipv4|ipv6]",
174 .function = show_udp_punt_fn,
179 table_format_udp_port_ (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
180 int port, int bind, int is_ip4)
182 const udp_dst_port_info_t *pi = udp_get_dst_port_info (um, port, is_ip4);
185 if (bind && ~0 == pi->node_index)
187 table_format_cell (t, *c, 0, "%d", pi->dst_port);
188 table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
189 table_format_cell (t, *c, 2, ~0 == pi->node_index ? "none" : "%U",
190 format_vlib_node_name, vm, pi->node_index);
191 table_format_cell (t, *c, 3, "%s", pi->name);
196 table_format_udp_port (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
197 int port, int bind, int ip4, int ip6)
200 table_format_udp_port_ (vm, um, t, c, port, bind, 1 /* is_ip4 */);
202 table_format_udp_port_ (vm, um, t, c, port, bind, 0 /* is_ip4 */);
205 static clib_error_t *
206 show_udp_ports (vlib_main_t *vm, unformat_input_t *input,
207 vlib_cli_command_t *cmd)
209 table_t table = {}, *t = &table;
210 udp_main_t *um = &udp_main;
211 clib_error_t *err = 0;
212 int ip4 = 1, ip6 = 1;
217 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
219 if (unformat (input, "ip4"))
221 else if (unformat (input, "ip6"))
223 else if (unformat (input, "bind"))
225 else if (unformat (input, "all"))
227 else if (unformat (input, "%d", &port))
231 err = clib_error_return (0, "unknown input `%U'",
232 format_unformat_error, input);
237 table_add_header_col (t, 4, "port", "proto", "node", "desc");
241 err = clib_error_return (0, "wrong port %d", port);
246 for (port = 0; port < 65536; port++)
247 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
251 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
254 vlib_cli_output (vm, "%U", format_table, t);
261 VLIB_CLI_COMMAND (show_udp_ports_cmd, static) = {
262 .path = "show udp ports",
263 .function = show_udp_ports,
264 .short_help = "show udp ports [ip4|ip6] [bind|all|<port>]",
269 * fd.io coding-style-patch-verification: ON
272 * eval: (c-set-style "gnu")