blob: 6c8992cd0dec5152a13b78604de6ba7fc3a6443c [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 {
Samvel Vartapetov7f5f21e2021-12-28 10:51:36 +0300190 if (udp_is_valid_dst_port (port_info->dst_port, 0))
Florin Corasc98ef752020-04-07 17:30:13 +0000191 {
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}
Florin Corasc98ef752020-04-07 17:30:13 +0000201VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
202{
203 .path = "show udp punt",
204 .short_help = "show udp punt [ipv4|ipv6]",
205 .function = show_udp_punt_fn,
206};
Florin Corasc98ef752020-04-07 17:30:13 +0000207
BenoƮt Ganneb3559ce2022-06-08 10:23:43 +0200208static void
209table_format_udp_port_ (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
210 int port, int bind, int is_ip4)
211{
BenoƮt Ganned52f80f2023-06-05 10:02:29 +0200212 const udp_dst_port_info_t *pi;
213
214 if (bind && !udp_is_valid_dst_port (port, is_ip4))
215 return;
216
217 pi = udp_get_dst_port_info (um, port, is_ip4);
BenoƮt Ganneb3559ce2022-06-08 10:23:43 +0200218 if (!pi)
219 return;
BenoƮt Ganned52f80f2023-06-05 10:02:29 +0200220
BenoƮt Ganneb3559ce2022-06-08 10:23:43 +0200221 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);
BenoƮt Ganned52f80f2023-06-05 10:02:29 +0200226
BenoƮt Ganneb3559ce2022-06-08 10:23:43 +0200227 (*c)++;
228}
229
230static void
231table_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)
233{
234 if (ip4)
235 table_format_udp_port_ (vm, um, t, c, port, bind, 1 /* is_ip4 */);
236 if (ip6)
237 table_format_udp_port_ (vm, um, t, c, port, bind, 0 /* is_ip4 */);
238}
239
240static clib_error_t *
241show_udp_ports (vlib_main_t *vm, unformat_input_t *input,
242 vlib_cli_command_t *cmd)
243{
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;
248 int port = -1;
249 int bind = 1;
250 int c = 0;
251
252 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
253 {
254 if (unformat (input, "ip4"))
255 ip6 = 0;
256 else if (unformat (input, "ip6"))
257 ip4 = 0;
258 else if (unformat (input, "bind"))
259 bind = 1;
260 else if (unformat (input, "all"))
261 bind = 0;
262 else if (unformat (input, "%d", &port))
263 ;
264 else
265 {
266 err = clib_error_return (0, "unknown input `%U'",
267 format_unformat_error, input);
268 goto out;
269 }
270 }
271
272 table_add_header_col (t, 4, "port", "proto", "node", "desc");
273
274 if (port > 65535)
275 {
276 err = clib_error_return (0, "wrong port %d", port);
277 goto out;
278 }
279 else if (port < 0)
280 {
281 for (port = 0; port < 65536; port++)
282 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
283 }
284 else
285 {
286 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
287 }
288
289 vlib_cli_output (vm, "%U", format_table, t);
290
291out:
292 table_free (t);
293 return err;
294}
295
296VLIB_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>]",
300 .is_mp_safe = 1,
301};
302
Florin Corasd9b4d9f2024-02-06 16:31:50 -0800303static void
304table_format_udp_transport_port_ (vlib_main_t *vm, table_t *t, int *c,
305 int port, int is_ip4)
306{
307 udp_main_t *um = &udp_main;
308 u32 refcnt;
309 u16 port_ne;
310
311 port_ne = clib_host_to_net_u16 (port);
312 refcnt = um->transport_ports_refcnt[is_ip4][port_ne];
313 if (!refcnt)
314 return;
315
316 if (!udp_is_valid_dst_port (port, is_ip4))
317 {
318 clib_warning ("Port %u is not registered refcnt %u!", port, refcnt);
319 return;
320 }
321
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);
325
326 (*c)++;
327}
328
329static void
330table_format_udp_transport_port (vlib_main_t *vm, table_t *t, int *c, int port,
331 int ipv)
332{
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 */);
337}
338
339static clib_error_t *
340show_udp_transport_ports (vlib_main_t *vm, unformat_input_t *input,
341 vlib_cli_command_t *cmd)
342{
343 table_t table = {}, *t = &table;
344 int ipv = -1, port = -1, c = 0;
345 clib_error_t *err = 0;
346
347 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
348 {
349 if (unformat (input, "ip4"))
350 ipv = 0;
351 else if (unformat (input, "ip6"))
352 ipv = 1;
353 else if (unformat (input, "%d", &port))
354 ;
355 else
356 {
357 err = clib_error_return (0, "unknown input `%U'",
358 format_unformat_error, input);
359 goto out;
360 }
361 }
362
363 table_add_header_col (t, 3, "port", "proto", "ref-cnt");
364
365 if (port > 65535)
366 {
367 err = clib_error_return (0, "wrong port %d", port);
368 goto out;
369 }
370
371 if (port < 0)
372 {
373 for (port = 0; port < 65536; port++)
374 table_format_udp_transport_port (vm, t, &c, port, ipv);
375 }
376 else
377 {
378 table_format_udp_transport_port (vm, t, &c, port, ipv);
379 }
380
381 vlib_cli_output (vm, "%U\n", format_table, t);
382
383out:
384 table_free (t);
385 return err;
386}
387
388VLIB_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>]",
392 .is_mp_safe = 1,
393};
394
Florin Corasc98ef752020-04-07 17:30:13 +0000395/*
396 * fd.io coding-style-patch-verification: ON
397 *
398 * Local Variables:
399 * eval: (c-set-style "gnu")
400 * End:
401 */