blob: f5a536cefe4a591138fc75f8ef130fe41fcfdaa7 [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}
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{
BenoƮt Ganned52f80f2023-06-05 10:02:29 +0200214 const udp_dst_port_info_t *pi;
215
216 if (bind && !udp_is_valid_dst_port (port, is_ip4))
217 return;
218
219 pi = udp_get_dst_port_info (um, port, is_ip4);
BenoƮt Ganneb3559ce2022-06-08 10:23:43 +0200220 if (!pi)
221 return;
BenoƮt Ganned52f80f2023-06-05 10:02:29 +0200222
BenoƮt Ganneb3559ce2022-06-08 10:23:43 +0200223 table_format_cell (t, *c, 0, "%d", pi->dst_port);
224 table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
225 table_format_cell (t, *c, 2, ~0 == pi->node_index ? "none" : "%U",
226 format_vlib_node_name, vm, pi->node_index);
227 table_format_cell (t, *c, 3, "%s", pi->name);
BenoƮt Ganned52f80f2023-06-05 10:02:29 +0200228
BenoƮt Ganneb3559ce2022-06-08 10:23:43 +0200229 (*c)++;
230}
231
232static void
233table_format_udp_port (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
234 int port, int bind, int ip4, int ip6)
235{
236 if (ip4)
237 table_format_udp_port_ (vm, um, t, c, port, bind, 1 /* is_ip4 */);
238 if (ip6)
239 table_format_udp_port_ (vm, um, t, c, port, bind, 0 /* is_ip4 */);
240}
241
242static clib_error_t *
243show_udp_ports (vlib_main_t *vm, unformat_input_t *input,
244 vlib_cli_command_t *cmd)
245{
246 table_t table = {}, *t = &table;
247 udp_main_t *um = &udp_main;
248 clib_error_t *err = 0;
249 int ip4 = 1, ip6 = 1;
250 int port = -1;
251 int bind = 1;
252 int c = 0;
253
254 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
255 {
256 if (unformat (input, "ip4"))
257 ip6 = 0;
258 else if (unformat (input, "ip6"))
259 ip4 = 0;
260 else if (unformat (input, "bind"))
261 bind = 1;
262 else if (unformat (input, "all"))
263 bind = 0;
264 else if (unformat (input, "%d", &port))
265 ;
266 else
267 {
268 err = clib_error_return (0, "unknown input `%U'",
269 format_unformat_error, input);
270 goto out;
271 }
272 }
273
274 table_add_header_col (t, 4, "port", "proto", "node", "desc");
275
276 if (port > 65535)
277 {
278 err = clib_error_return (0, "wrong port %d", port);
279 goto out;
280 }
281 else if (port < 0)
282 {
283 for (port = 0; port < 65536; port++)
284 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
285 }
286 else
287 {
288 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
289 }
290
291 vlib_cli_output (vm, "%U", format_table, t);
292
293out:
294 table_free (t);
295 return err;
296}
297
298VLIB_CLI_COMMAND (show_udp_ports_cmd, static) = {
299 .path = "show udp ports",
300 .function = show_udp_ports,
301 .short_help = "show udp ports [ip4|ip6] [bind|all|<port>]",
302 .is_mp_safe = 1,
303};
304
Florin Corasd9b4d9f2024-02-06 16:31:50 -0800305static void
306table_format_udp_transport_port_ (vlib_main_t *vm, table_t *t, int *c,
307 int port, int is_ip4)
308{
309 udp_main_t *um = &udp_main;
310 u32 refcnt;
311 u16 port_ne;
312
313 port_ne = clib_host_to_net_u16 (port);
314 refcnt = um->transport_ports_refcnt[is_ip4][port_ne];
315 if (!refcnt)
316 return;
317
318 if (!udp_is_valid_dst_port (port, is_ip4))
319 {
320 clib_warning ("Port %u is not registered refcnt %u!", port, refcnt);
321 return;
322 }
323
324 table_format_cell (t, *c, 0, "%d", port);
325 table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
326 table_format_cell (t, *c, 2, "%d", refcnt);
327
328 (*c)++;
329}
330
331static void
332table_format_udp_transport_port (vlib_main_t *vm, table_t *t, int *c, int port,
333 int ipv)
334{
335 if (ipv == -1 || ipv == 0)
336 table_format_udp_transport_port_ (vm, t, c, port, 1 /* is_ip4 */);
337 if (ipv == -1 || ipv == 1)
338 table_format_udp_transport_port_ (vm, t, c, port, 0 /* is_ip4 */);
339}
340
341static clib_error_t *
342show_udp_transport_ports (vlib_main_t *vm, unformat_input_t *input,
343 vlib_cli_command_t *cmd)
344{
345 table_t table = {}, *t = &table;
346 int ipv = -1, port = -1, c = 0;
347 clib_error_t *err = 0;
348
349 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
350 {
351 if (unformat (input, "ip4"))
352 ipv = 0;
353 else if (unformat (input, "ip6"))
354 ipv = 1;
355 else if (unformat (input, "%d", &port))
356 ;
357 else
358 {
359 err = clib_error_return (0, "unknown input `%U'",
360 format_unformat_error, input);
361 goto out;
362 }
363 }
364
365 table_add_header_col (t, 3, "port", "proto", "ref-cnt");
366
367 if (port > 65535)
368 {
369 err = clib_error_return (0, "wrong port %d", port);
370 goto out;
371 }
372
373 if (port < 0)
374 {
375 for (port = 0; port < 65536; port++)
376 table_format_udp_transport_port (vm, t, &c, port, ipv);
377 }
378 else
379 {
380 table_format_udp_transport_port (vm, t, &c, port, ipv);
381 }
382
383 vlib_cli_output (vm, "%U\n", format_table, t);
384
385out:
386 table_free (t);
387 return err;
388}
389
390VLIB_CLI_COMMAND (show_udp_transport_ports_cmd, static) = {
391 .path = "show udp transport ports",
392 .function = show_udp_transport_ports,
393 .short_help = "show udp transport ports [ip4|ip6] [<port>]",
394 .is_mp_safe = 1,
395};
396
Florin Corasc98ef752020-04-07 17:30:13 +0000397/*
398 * fd.io coding-style-patch-verification: ON
399 *
400 * Local Variables:
401 * eval: (c-set-style "gnu")
402 * End:
403 */