blob: 72dd9d8447da4aecf5f13d9673aba233d9d4761e [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 Corasf8ee39f2022-10-18 18:37:56 -070098 s = format (s, " index %u%U flags: %U", uc->c_c_index, format_udp_cfg_flags,
99 uc, format_udp_connection_flags, uc);
Florin Corasc98ef752020-04-07 17:30:13 +0000100 if (!(uc->flags & UDP_CONN_F_LISTEN))
Steven Luongbf12efc2022-07-25 09:29:23 -0700101 s = format (s, " \n sw_if_index: %d, mss: %u\n", uc->sw_if_index, uc->mss);
102
Florin Corasc98ef752020-04-07 17:30:13 +0000103 return s;
104}
105
106u8 *
107format_udp_connection (u8 * s, va_list * args)
108{
109 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
110 u32 verbose = va_arg (*args, u32);
111 if (!uc)
112 return s;
Florin Coras7bf6ed62020-09-23 12:02:08 -0700113 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_udp_connection_id, uc);
Florin Corasc98ef752020-04-07 17:30:13 +0000114 if (verbose)
115 {
Florin Coras7bf6ed62020-09-23 12:02:08 -0700116 s = format (s, "%-" SESSION_CLI_STATE_LEN "s",
Florin Corasc98ef752020-04-07 17:30:13 +0000117 (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
118 if (verbose > 1)
119 s = format (s, "\n%U", format_udp_vars, uc);
120 }
121 return s;
122}
123
124static clib_error_t *
125udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
126{
127 udp_main_t *um = &udp_main;
128 u32 tmp;
129
130 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
131 {
132 if (unformat (input, "mtu %u", &tmp))
133 um->default_mtu = tmp;
Florin Coras7743d6b2021-07-22 14:03:11 -0700134 else if (unformat (input, "icmp-unreachable-disabled"))
135 um->icmp_send_unreachable_disabled = 1;
Florin Corasf8ee39f2022-10-18 18:37:56 -0700136 else if (unformat (input, "no-csum-offload"))
137 um->csum_offload = 0;
Florin Corasc98ef752020-04-07 17:30:13 +0000138 else
139 return clib_error_return (0, "unknown input `%U'",
140 format_unformat_error, input);
141 }
142 return 0;
143}
144
145VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
146
147static clib_error_t *
148show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
149 vlib_cli_command_t * cmd_arg)
150{
151 udp_main_t *um = vnet_get_udp_main ();
152
153 clib_error_t *error = NULL;
154
155 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
156 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
157 input);
158
159 udp_dst_port_info_t *port_info;
160 if (um->punt_unknown4)
161 {
162 vlib_cli_output (vm, "IPv4 UDP punt: enabled");
163 }
164 else
165 {
166 u8 *s = NULL;
167 vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
168 {
169 if (udp_is_valid_dst_port (port_info->dst_port, 1))
170 {
171 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
172 }
173 }
174 s = format (s, "%c", 0);
175 vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
176 }
177
178 if (um->punt_unknown6)
179 {
180 vlib_cli_output (vm, "IPv6 UDP punt: enabled");
181 }
182 else
183 {
184 u8 *s = NULL;
185 vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
186 {
187 if (udp_is_valid_dst_port (port_info->dst_port, 01))
188 {
189 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
190 }
191 }
192 s = format (s, "%c", 0);
193 vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
194 }
195
196 return (error);
197}
198/* *INDENT-OFF* */
199VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
200{
201 .path = "show udp punt",
202 .short_help = "show udp punt [ipv4|ipv6]",
203 .function = show_udp_punt_fn,
204};
205/* *INDENT-ON* */
206
Benoît Ganneb3559ce2022-06-08 10:23:43 +0200207static void
208table_format_udp_port_ (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
209 int port, int bind, int is_ip4)
210{
211 const udp_dst_port_info_t *pi = udp_get_dst_port_info (um, port, is_ip4);
212 if (!pi)
213 return;
214 if (bind && ~0 == pi->node_index)
215 return;
216 table_format_cell (t, *c, 0, "%d", pi->dst_port);
217 table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
218 table_format_cell (t, *c, 2, ~0 == pi->node_index ? "none" : "%U",
219 format_vlib_node_name, vm, pi->node_index);
220 table_format_cell (t, *c, 3, "%s", pi->name);
221 (*c)++;
222}
223
224static void
225table_format_udp_port (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
226 int port, int bind, int ip4, int ip6)
227{
228 if (ip4)
229 table_format_udp_port_ (vm, um, t, c, port, bind, 1 /* is_ip4 */);
230 if (ip6)
231 table_format_udp_port_ (vm, um, t, c, port, bind, 0 /* is_ip4 */);
232}
233
234static clib_error_t *
235show_udp_ports (vlib_main_t *vm, unformat_input_t *input,
236 vlib_cli_command_t *cmd)
237{
238 table_t table = {}, *t = &table;
239 udp_main_t *um = &udp_main;
240 clib_error_t *err = 0;
241 int ip4 = 1, ip6 = 1;
242 int port = -1;
243 int bind = 1;
244 int c = 0;
245
246 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
247 {
248 if (unformat (input, "ip4"))
249 ip6 = 0;
250 else if (unformat (input, "ip6"))
251 ip4 = 0;
252 else if (unformat (input, "bind"))
253 bind = 1;
254 else if (unformat (input, "all"))
255 bind = 0;
256 else if (unformat (input, "%d", &port))
257 ;
258 else
259 {
260 err = clib_error_return (0, "unknown input `%U'",
261 format_unformat_error, input);
262 goto out;
263 }
264 }
265
266 table_add_header_col (t, 4, "port", "proto", "node", "desc");
267
268 if (port > 65535)
269 {
270 err = clib_error_return (0, "wrong port %d", port);
271 goto out;
272 }
273 else if (port < 0)
274 {
275 for (port = 0; port < 65536; port++)
276 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
277 }
278 else
279 {
280 table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
281 }
282
283 vlib_cli_output (vm, "%U", format_table, t);
284
285out:
286 table_free (t);
287 return err;
288}
289
290VLIB_CLI_COMMAND (show_udp_ports_cmd, static) = {
291 .path = "show udp ports",
292 .function = show_udp_ports,
293 .short_help = "show udp ports [ip4|ip6] [bind|all|<port>]",
294 .is_mp_safe = 1,
295};
296
Florin Corasc98ef752020-04-07 17:30:13 +0000297/*
298 * fd.io coding-style-patch-verification: ON
299 *
300 * Local Variables:
301 * eval: (c-set-style "gnu")
302 * End:
303 */