blob: 89cea6892516c8689cd9b9a42fb50a84df86091b [file] [log] [blame]
Florin Corasc98ef752020-04-07 17:30:13 +00001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vnet/udp/udp.h>
17
18u8 *
19format_udp_connection_id (u8 * s, va_list * args)
20{
21 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
22 if (!uc)
23 return s;
24 if (uc->c_is_ip4)
25 s = format (s, "[%u:%u][%s] %U:%d->%U:%d", uc->c_thread_index,
26 uc->c_s_index, "U", format_ip4_address, &uc->c_lcl_ip4,
27 clib_net_to_host_u16 (uc->c_lcl_port), format_ip4_address,
28 &uc->c_rmt_ip4, clib_net_to_host_u16 (uc->c_rmt_port));
29 else
30 s = format (s, "[%u:%u][%s] %U:%d->%U:%d", uc->c_thread_index,
31 uc->c_s_index, "U", format_ip6_address, &uc->c_lcl_ip6,
32 clib_net_to_host_u16 (uc->c_lcl_port), format_ip6_address,
33 &uc->c_rmt_ip6, clib_net_to_host_u16 (uc->c_rmt_port));
34 return s;
35}
36
37static const char *udp_connection_flags_str[] = {
38#define _(sym, str) str,
39 foreach_udp_connection_flag
40#undef _
41};
42
43static u8 *
44format_udp_connection_flags (u8 * s, va_list * args)
45{
46 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
47 int i, last = -1;
48
49 for (i = 0; i < UDP_CONN_N_FLAGS; i++)
50 if (uc->flags & (1 << i))
51 last = i;
52 for (i = 0; i < last; i++)
53 {
54 if (uc->flags & (1 << i))
55 s = format (s, "%s, ", udp_connection_flags_str[i]);
56 }
57 if (last >= 0)
58 s = format (s, "%s", udp_connection_flags_str[last]);
59 return s;
60}
61
62static u8 *
63format_udp_vars (u8 * s, va_list * args)
64{
65 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
66 s = format (s, " index %u flags: %U", uc->c_c_index,
67 format_udp_connection_flags, uc);
68
69 if (!(uc->flags & UDP_CONN_F_LISTEN))
70 s = format (s, "\n");
71 return s;
72}
73
74u8 *
75format_udp_connection (u8 * s, va_list * args)
76{
77 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
78 u32 verbose = va_arg (*args, u32);
79 if (!uc)
80 return s;
81 s = format (s, "%-50U", format_udp_connection_id, uc);
82 if (verbose)
83 {
84 s = format (s, "%-15s",
85 (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
86 if (verbose > 1)
87 s = format (s, "\n%U", format_udp_vars, uc);
88 }
89 return s;
90}
91
92static clib_error_t *
93udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
94{
95 udp_main_t *um = &udp_main;
96 u32 tmp;
97
98 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
99 {
100 if (unformat (input, "mtu %u", &tmp))
101 um->default_mtu = tmp;
102 else
103 return clib_error_return (0, "unknown input `%U'",
104 format_unformat_error, input);
105 }
106 return 0;
107}
108
109VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
110
111static clib_error_t *
112show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
113 vlib_cli_command_t * cmd_arg)
114{
115 udp_main_t *um = vnet_get_udp_main ();
116
117 clib_error_t *error = NULL;
118
119 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
120 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
121 input);
122
123 udp_dst_port_info_t *port_info;
124 if (um->punt_unknown4)
125 {
126 vlib_cli_output (vm, "IPv4 UDP punt: enabled");
127 }
128 else
129 {
130 u8 *s = NULL;
131 vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
132 {
133 if (udp_is_valid_dst_port (port_info->dst_port, 1))
134 {
135 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
136 }
137 }
138 s = format (s, "%c", 0);
139 vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
140 }
141
142 if (um->punt_unknown6)
143 {
144 vlib_cli_output (vm, "IPv6 UDP punt: enabled");
145 }
146 else
147 {
148 u8 *s = NULL;
149 vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
150 {
151 if (udp_is_valid_dst_port (port_info->dst_port, 01))
152 {
153 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
154 }
155 }
156 s = format (s, "%c", 0);
157 vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
158 }
159
160 return (error);
161}
162/* *INDENT-OFF* */
163VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
164{
165 .path = "show udp punt",
166 .short_help = "show udp punt [ipv4|ipv6]",
167 .function = show_udp_punt_fn,
168};
169/* *INDENT-ON* */
170
171/*
172 * fd.io coding-style-patch-verification: ON
173 *
174 * Local Variables:
175 * eval: (c-set-style "gnu")
176 * End:
177 */