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\n", uc->c_c_index,
99 format_udp_cfg_flags, uc, format_udp_connection_flags, uc);
100 s = format (s, " fib_index: %u next_node: %u opaque: %u ", uc->c_fib_index);
101 if (!(uc->flags & UDP_CONN_F_LISTEN))
102 s = format (s, " sw_if_index: %d mss: %u\n", uc->sw_if_index, uc->mss);
104 s = format (s, "\n");
110 format_udp_connection (u8 * s, va_list * args)
112 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
113 u32 verbose = va_arg (*args, u32);
116 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_udp_connection_id, uc);
119 s = format (s, "%-" SESSION_CLI_STATE_LEN "s",
120 (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
122 s = format (s, "\n%U", format_udp_vars, uc);
127 static clib_error_t *
128 udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
130 udp_main_t *um = &udp_main;
133 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
135 if (unformat (input, "mtu %u", &tmp))
136 um->default_mtu = tmp;
137 else if (unformat (input, "icmp-unreachable-disabled"))
138 um->icmp_send_unreachable_disabled = 1;
139 else if (unformat (input, "no-csum-offload"))
140 um->csum_offload = 0;
142 return clib_error_return (0, "unknown input `%U'",
143 format_unformat_error, input);
148 VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
150 static clib_error_t *
151 show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
152 vlib_cli_command_t * cmd_arg)
154 udp_main_t *um = vnet_get_udp_main ();
156 clib_error_t *error = NULL;
158 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
159 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
162 udp_dst_port_info_t *port_info;
163 if (um->punt_unknown4)
165 vlib_cli_output (vm, "IPv4 UDP punt: enabled");
170 vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
172 if (udp_is_valid_dst_port (port_info->dst_port, 1))
174 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
177 s = format (s, "%c", 0);
178 vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
181 if (um->punt_unknown6)
183 vlib_cli_output (vm, "IPv6 UDP punt: enabled");
188 vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
190 if (udp_is_valid_dst_port (port_info->dst_port, 0))
192 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
195 s = format (s, "%c", 0);
196 vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
201 VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
203 .path = "show udp punt",
204 .short_help = "show udp punt [ipv4|ipv6]",
205 .function = show_udp_punt_fn,
209 table_format_udp_port_ (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
210 int port, int bind, int is_ip4)
212 const udp_dst_port_info_t *pi;
214 if (bind && !udp_is_valid_dst_port (port, is_ip4))
217 pi = udp_get_dst_port_info (um, port, is_ip4);
221 table_format_cell (t, *c, 0, "%d", pi->dst_port);
222 table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
223 table_format_cell (t, *c, 2, ~0 == pi->node_index ? "none" : "%U",
224 format_vlib_node_name, vm, pi->node_index);
225 table_format_cell (t, *c, 3, "%s", pi->name);
231 table_format_udp_port (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
232 int port, int bind, int ip4, int ip6)
235 table_format_udp_port_ (vm, um, t, c, port, bind, 1 /* is_ip4 */);
237 table_format_udp_port_ (vm, um, t, c, port, bind, 0 /* is_ip4 */);
240 static clib_error_t *
241 show_udp_ports (vlib_main_t *vm, unformat_input_t *input,
242 vlib_cli_command_t *cmd)
244 table_t table = {}, *t = &table;
245 udp_main_t *um = &udp_main;
246 clib_error_t *err = 0;
247 int ip4 = 1, ip6 = 1;
252 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
254 if (unformat (input, "ip4"))
256 else if (unformat (input, "ip6"))
258 else if (unformat (input, "bind"))
260 else if (unformat (input, "all"))
262 else if (unformat (input, "%d", &port))
266 err = clib_error_return (0, "unknown input `%U'",
267 format_unformat_error, input);
272 table_add_header_col (t, 4, "port", "proto", "node", "desc");
276 err = clib_error_return (0, "wrong port %d", port);
281 for (port = 0; port < 65536; port++)
282 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
286 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
289 vlib_cli_output (vm, "%U", format_table, t);
296 VLIB_CLI_COMMAND (show_udp_ports_cmd, static) = {
297 .path = "show udp ports",
298 .function = show_udp_ports,
299 .short_help = "show udp ports [ip4|ip6] [bind|all|<port>]",
304 table_format_udp_transport_port_ (vlib_main_t *vm, table_t *t, int *c,
305 int port, int is_ip4)
307 udp_main_t *um = &udp_main;
311 port_ne = clib_host_to_net_u16 (port);
312 refcnt = um->transport_ports_refcnt[is_ip4][port_ne];
316 if (!udp_is_valid_dst_port (port, is_ip4))
318 clib_warning ("Port %u is not registered refcnt %u!", port, refcnt);
322 table_format_cell (t, *c, 0, "%d", port);
323 table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
324 table_format_cell (t, *c, 2, "%d", refcnt);
330 table_format_udp_transport_port (vlib_main_t *vm, table_t *t, int *c, int port,
333 if (ipv == -1 || ipv == 0)
334 table_format_udp_transport_port_ (vm, t, c, port, 1 /* is_ip4 */);
335 if (ipv == -1 || ipv == 1)
336 table_format_udp_transport_port_ (vm, t, c, port, 0 /* is_ip4 */);
339 static clib_error_t *
340 show_udp_transport_ports (vlib_main_t *vm, unformat_input_t *input,
341 vlib_cli_command_t *cmd)
343 table_t table = {}, *t = &table;
344 int ipv = -1, port = -1, c = 0;
345 clib_error_t *err = 0;
347 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
349 if (unformat (input, "ip4"))
351 else if (unformat (input, "ip6"))
353 else if (unformat (input, "%d", &port))
357 err = clib_error_return (0, "unknown input `%U'",
358 format_unformat_error, input);
363 table_add_header_col (t, 3, "port", "proto", "ref-cnt");
367 err = clib_error_return (0, "wrong port %d", port);
373 for (port = 0; port < 65536; port++)
374 table_format_udp_transport_port (vm, t, &c, port, ipv);
378 table_format_udp_transport_port (vm, t, &c, port, ipv);
381 vlib_cli_output (vm, "%U\n", format_table, t);
388 VLIB_CLI_COMMAND (show_udp_transport_ports_cmd, static) = {
389 .path = "show udp transport ports",
390 .function = show_udp_transport_ports,
391 .short_help = "show udp transport ports [ip4|ip6] [<port>]",
396 * fd.io coding-style-patch-verification: ON
399 * eval: (c-set-style "gnu")