blob: d60f0dde50b5b46cd0d067fc851306a6a59e61bf [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
Benoît Ganneb3559ce2022-06-08 10:23:43 +020016#include <vppinfra/error.h>
17#include <vppinfra/format.h>
18#include <vppinfra/format_table.h>
Florin Corasc98ef752020-04-07 17:30:13 +000019#include <vnet/udp/udp.h>
Florin Coras7bf6ed62020-09-23 12:02:08 -070020#include <vnet/session/session_types.h>
Florin Corasc98ef752020-04-07 17:30:13 +000021
22u8 *
23format_udp_connection_id (u8 * s, va_list * args)
24{
25 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
26 if (!uc)
27 return s;
28 if (uc->c_is_ip4)
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));
33 else
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));
38 return s;
39}
40
Florin Corasf8ee39f2022-10-18 18:37:56 -070041static const char *udp_cfg_flags_str[] = {
42#define _(sym, str) str,
43 foreach_udp_cfg_flag
44#undef _
45};
46
47static u8 *
48format_udp_cfg_flags (u8 *s, va_list *args)
49{
50 udp_connection_t *tc = va_arg (*args, udp_connection_t *);
51 int i, last = -1;
52
53 for (i = 0; i < UDP_CFG_N_FLAG_BITS; i++)
54 if (tc->cfg_flags & (1 << i))
55 last = i;
56 if (last >= 0)
57 s = format (s, " cfg: ");
58 for (i = 0; i < last; i++)
59 {
60 if (tc->cfg_flags & (1 << i))
61 s = format (s, "%s, ", udp_cfg_flags_str[i]);
62 }
63 if (last >= 0)
64 s = format (s, "%s", udp_cfg_flags_str[last]);
65 return s;
66}
67
Florin Corasc98ef752020-04-07 17:30:13 +000068static const char *udp_connection_flags_str[] = {
69#define _(sym, str) str,
70 foreach_udp_connection_flag
71#undef _
72};
73
74static u8 *
75format_udp_connection_flags (u8 * s, va_list * args)
76{
77 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
78 int i, last = -1;
79
80 for (i = 0; i < UDP_CONN_N_FLAGS; i++)
81 if (uc->flags & (1 << i))
82 last = i;
83 for (i = 0; i < last; i++)
84 {
85 if (uc->flags & (1 << i))
86 s = format (s, "%s, ", udp_connection_flags_str[i]);
87 }
88 if (last >= 0)
89 s = format (s, "%s", udp_connection_flags_str[last]);
90 return s;
91}
92
93static u8 *
94format_udp_vars (u8 * s, va_list * args)
95{
96 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
Steven Luongbf12efc2022-07-25 09:29:23 -070097
Florin Coras97943262023-05-12 16:35:14 -070098 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);
Florin Corasc98ef752020-04-07 17:30:13 +0000101 if (!(uc->flags & UDP_CONN_F_LISTEN))
Florin Coras97943262023-05-12 16:35:14 -0700102 s = format (s, " sw_if_index: %d mss: %u\n", uc->sw_if_index, uc->mss);
103 else
104 s = format (s, "\n");
Steven Luongbf12efc2022-07-25 09:29:23 -0700105
Florin Corasc98ef752020-04-07 17:30:13 +0000106 return s;
107}
108
109u8 *
110format_udp_connection (u8 * s, va_list * args)
111{
112 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
113 u32 verbose = va_arg (*args, u32);
114 if (!uc)
115 return s;
Florin Coras7bf6ed62020-09-23 12:02:08 -0700116 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_udp_connection_id, uc);
Florin Corasc98ef752020-04-07 17:30:13 +0000117 if (verbose)
118 {
Florin Coras7bf6ed62020-09-23 12:02:08 -0700119 s = format (s, "%-" SESSION_CLI_STATE_LEN "s",
Florin Corasc98ef752020-04-07 17:30:13 +0000120 (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
121 if (verbose > 1)
122 s = format (s, "\n%U", format_udp_vars, uc);
123 }
124 return s;
125}
126
127static clib_error_t *
128udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
129{
130 udp_main_t *um = &udp_main;
131 u32 tmp;
132
133 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
134 {
135 if (unformat (input, "mtu %u", &tmp))
136 um->default_mtu = tmp;
Florin Coras7743d6b2021-07-22 14:03:11 -0700137 else if (unformat (input, "icmp-unreachable-disabled"))
138 um->icmp_send_unreachable_disabled = 1;
Florin Corasf8ee39f2022-10-18 18:37:56 -0700139 else if (unformat (input, "no-csum-offload"))
140 um->csum_offload = 0;
Florin Corasc98ef752020-04-07 17:30:13 +0000141 else
142 return clib_error_return (0, "unknown input `%U'",
143 format_unformat_error, input);
144 }
145 return 0;
146}
147
148VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
149
150static clib_error_t *
151show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
152 vlib_cli_command_t * cmd_arg)
153{
154 udp_main_t *um = vnet_get_udp_main ();
155
156 clib_error_t *error = NULL;
157
158 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
159 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
160 input);
161
162 udp_dst_port_info_t *port_info;
163 if (um->punt_unknown4)
164 {
165 vlib_cli_output (vm, "IPv4 UDP punt: enabled");
166 }
167 else
168 {
169 u8 *s = NULL;
170 vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
171 {
172 if (udp_is_valid_dst_port (port_info->dst_port, 1))
173 {
174 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
175 }
176 }
177 s = format (s, "%c", 0);
178 vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
179 }
180
181 if (um->punt_unknown6)
182 {
183 vlib_cli_output (vm, "IPv6 UDP punt: enabled");
184 }
185 else
186 {
187 u8 *s = NULL;
188 vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
189 {
190 if (udp_is_valid_dst_port (port_info->dst_port, 01))
191 {
192 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
193 }
194 }
195 s = format (s, "%c", 0);
196 vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
197 }
198
199 return (error);
200}
201/* *INDENT-OFF* */
202VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
203{
204 .path = "show udp punt",
205 .short_help = "show udp punt [ipv4|ipv6]",
206 .function = show_udp_punt_fn,
207};
208/* *INDENT-ON* */
209
Benoît Ganneb3559ce2022-06-08 10:23:43 +0200210static void
211table_format_udp_port_ (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
212 int port, int bind, int is_ip4)
213{
214 const udp_dst_port_info_t *pi = udp_get_dst_port_info (um, port, is_ip4);
215 if (!pi)
216 return;
217 if (bind && ~0 == pi->node_index)
218 return;
219 table_format_cell (t, *c, 0, "%d", pi->dst_port);
220 table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
221 table_format_cell (t, *c, 2, ~0 == pi->node_index ? "none" : "%U",
222 format_vlib_node_name, vm, pi->node_index);
223 table_format_cell (t, *c, 3, "%s", pi->name);
224 (*c)++;
225}
226
227static void
228table_format_udp_port (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
229 int port, int bind, int ip4, int ip6)
230{
231 if (ip4)
232 table_format_udp_port_ (vm, um, t, c, port, bind, 1 /* is_ip4 */);
233 if (ip6)
234 table_format_udp_port_ (vm, um, t, c, port, bind, 0 /* is_ip4 */);
235}
236
237static clib_error_t *
238show_udp_ports (vlib_main_t *vm, unformat_input_t *input,
239 vlib_cli_command_t *cmd)
240{
241 table_t table = {}, *t = &table;
242 udp_main_t *um = &udp_main;
243 clib_error_t *err = 0;
244 int ip4 = 1, ip6 = 1;
245 int port = -1;
246 int bind = 1;
247 int c = 0;
248
249 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
250 {
251 if (unformat (input, "ip4"))
252 ip6 = 0;
253 else if (unformat (input, "ip6"))
254 ip4 = 0;
255 else if (unformat (input, "bind"))
256 bind = 1;
257 else if (unformat (input, "all"))
258 bind = 0;
259 else if (unformat (input, "%d", &port))
260 ;
261 else
262 {
263 err = clib_error_return (0, "unknown input `%U'",
264 format_unformat_error, input);
265 goto out;
266 }
267 }
268
269 table_add_header_col (t, 4, "port", "proto", "node", "desc");
270
271 if (port > 65535)
272 {
273 err = clib_error_return (0, "wrong port %d", port);
274 goto out;
275 }
276 else if (port < 0)
277 {
278 for (port = 0; port < 65536; port++)
279 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
280 }
281 else
282 {
283 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
284 }
285
286 vlib_cli_output (vm, "%U", format_table, t);
287
288out:
289 table_free (t);
290 return err;
291}
292
293VLIB_CLI_COMMAND (show_udp_ports_cmd, static) = {
294 .path = "show udp ports",
295 .function = show_udp_ports,
296 .short_help = "show udp ports [ip4|ip6] [bind|all|<port>]",
297 .is_mp_safe = 1,
298};
299
Florin Corasc98ef752020-04-07 17:30:13 +0000300/*
301 * fd.io coding-style-patch-verification: ON
302 *
303 * Local Variables:
304 * eval: (c-set-style "gnu")
305 * End:
306 */