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_cfg_flags_str[] = {
42 #define _(sym, str) str,
48 format_udp_cfg_flags (u8 *s, va_list *args)
50 udp_connection_t *tc = va_arg (*args, udp_connection_t *);
53 for (i = 0; i < UDP_CFG_N_FLAG_BITS; i++)
54 if (tc->cfg_flags & (1 << i))
57 s = format (s, " cfg: ");
58 for (i = 0; i < last; i++)
60 if (tc->cfg_flags & (1 << i))
61 s = format (s, "%s, ", udp_cfg_flags_str[i]);
64 s = format (s, "%s", udp_cfg_flags_str[last]);
68 static const char *udp_connection_flags_str[] = {
69 #define _(sym, str) str,
70 foreach_udp_connection_flag
75 format_udp_connection_flags (u8 * s, va_list * args)
77 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
80 for (i = 0; i < UDP_CONN_N_FLAGS; i++)
81 if (uc->flags & (1 << i))
83 for (i = 0; i < last; i++)
85 if (uc->flags & (1 << i))
86 s = format (s, "%s, ", udp_connection_flags_str[i]);
89 s = format (s, "%s", udp_connection_flags_str[last]);
94 format_udp_vars (u8 * s, va_list * args)
96 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
98 s = format (s, " index %u%U flags: %U", uc->c_c_index, format_udp_cfg_flags,
99 uc, format_udp_connection_flags, uc);
100 if (!(uc->flags & UDP_CONN_F_LISTEN))
101 s = format (s, " \n sw_if_index: %d, mss: %u\n", uc->sw_if_index, uc->mss);
107 format_udp_connection (u8 * s, va_list * args)
109 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
110 u32 verbose = va_arg (*args, u32);
113 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_udp_connection_id, uc);
116 s = format (s, "%-" SESSION_CLI_STATE_LEN "s",
117 (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
119 s = format (s, "\n%U", format_udp_vars, uc);
124 static clib_error_t *
125 udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
127 udp_main_t *um = &udp_main;
130 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
132 if (unformat (input, "mtu %u", &tmp))
133 um->default_mtu = tmp;
134 else if (unformat (input, "icmp-unreachable-disabled"))
135 um->icmp_send_unreachable_disabled = 1;
136 else if (unformat (input, "no-csum-offload"))
137 um->csum_offload = 0;
139 return clib_error_return (0, "unknown input `%U'",
140 format_unformat_error, input);
145 VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
147 static clib_error_t *
148 show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
149 vlib_cli_command_t * cmd_arg)
151 udp_main_t *um = vnet_get_udp_main ();
153 clib_error_t *error = NULL;
155 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
156 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
159 udp_dst_port_info_t *port_info;
160 if (um->punt_unknown4)
162 vlib_cli_output (vm, "IPv4 UDP punt: enabled");
167 vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
169 if (udp_is_valid_dst_port (port_info->dst_port, 1))
171 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
174 s = format (s, "%c", 0);
175 vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
178 if (um->punt_unknown6)
180 vlib_cli_output (vm, "IPv6 UDP punt: enabled");
185 vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
187 if (udp_is_valid_dst_port (port_info->dst_port, 01))
189 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
192 s = format (s, "%c", 0);
193 vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
199 VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
201 .path = "show udp punt",
202 .short_help = "show udp punt [ipv4|ipv6]",
203 .function = show_udp_punt_fn,
208 table_format_udp_port_ (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
209 int port, int bind, int is_ip4)
211 const udp_dst_port_info_t *pi = udp_get_dst_port_info (um, port, is_ip4);
214 if (bind && ~0 == pi->node_index)
216 table_format_cell (t, *c, 0, "%d", pi->dst_port);
217 table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
218 table_format_cell (t, *c, 2, ~0 == pi->node_index ? "none" : "%U",
219 format_vlib_node_name, vm, pi->node_index);
220 table_format_cell (t, *c, 3, "%s", pi->name);
225 table_format_udp_port (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
226 int port, int bind, int ip4, int ip6)
229 table_format_udp_port_ (vm, um, t, c, port, bind, 1 /* is_ip4 */);
231 table_format_udp_port_ (vm, um, t, c, port, bind, 0 /* is_ip4 */);
234 static clib_error_t *
235 show_udp_ports (vlib_main_t *vm, unformat_input_t *input,
236 vlib_cli_command_t *cmd)
238 table_t table = {}, *t = &table;
239 udp_main_t *um = &udp_main;
240 clib_error_t *err = 0;
241 int ip4 = 1, ip6 = 1;
246 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
248 if (unformat (input, "ip4"))
250 else if (unformat (input, "ip6"))
252 else if (unformat (input, "bind"))
254 else if (unformat (input, "all"))
256 else if (unformat (input, "%d", &port))
260 err = clib_error_return (0, "unknown input `%U'",
261 format_unformat_error, input);
266 table_add_header_col (t, 4, "port", "proto", "node", "desc");
270 err = clib_error_return (0, "wrong port %d", port);
275 for (port = 0; port < 65536; port++)
276 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
280 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
283 vlib_cli_output (vm, "%U", format_table, t);
290 VLIB_CLI_COMMAND (show_udp_ports_cmd, static) = {
291 .path = "show udp ports",
292 .function = show_udp_ports,
293 .short_help = "show udp ports [ip4|ip6] [bind|all|<port>]",
298 * fd.io coding-style-patch-verification: ON
301 * eval: (c-set-style "gnu")