Line data Source code
1 : /*
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 <vppinfra/error.h>
17 : #include <vppinfra/format.h>
18 : #include <vppinfra/format_table.h>
19 : #include <vnet/udp/udp.h>
20 : #include <vnet/session/session_types.h>
21 :
22 : u8 *
23 2 : format_udp_connection_id (u8 * s, va_list * args)
24 : {
25 2 : udp_connection_t *uc = va_arg (*args, udp_connection_t *);
26 2 : if (!uc)
27 0 : return s;
28 2 : if (uc->c_is_ip4)
29 2 : 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 2 : clib_net_to_host_u16 (uc->c_lcl_port), format_ip4_address,
32 2 : &uc->c_rmt_ip4, clib_net_to_host_u16 (uc->c_rmt_port));
33 : else
34 0 : 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 0 : clib_net_to_host_u16 (uc->c_lcl_port), format_ip6_address,
37 0 : &uc->c_rmt_ip6, clib_net_to_host_u16 (uc->c_rmt_port));
38 2 : return s;
39 : }
40 :
41 : static const char *udp_cfg_flags_str[] = {
42 : #define _(sym, str) str,
43 : foreach_udp_cfg_flag
44 : #undef _
45 : };
46 :
47 : static u8 *
48 2 : format_udp_cfg_flags (u8 *s, va_list *args)
49 : {
50 2 : udp_connection_t *tc = va_arg (*args, udp_connection_t *);
51 2 : int i, last = -1;
52 :
53 4 : for (i = 0; i < UDP_CFG_N_FLAG_BITS; i++)
54 2 : if (tc->cfg_flags & (1 << i))
55 0 : last = i;
56 2 : if (last >= 0)
57 0 : s = format (s, " cfg: ");
58 2 : for (i = 0; i < last; i++)
59 : {
60 0 : if (tc->cfg_flags & (1 << i))
61 0 : s = format (s, "%s, ", udp_cfg_flags_str[i]);
62 : }
63 2 : if (last >= 0)
64 0 : s = format (s, "%s", udp_cfg_flags_str[last]);
65 2 : return s;
66 : }
67 :
68 : static const char *udp_connection_flags_str[] = {
69 : #define _(sym, str) str,
70 : foreach_udp_connection_flag
71 : #undef _
72 : };
73 :
74 : static u8 *
75 2 : format_udp_connection_flags (u8 * s, va_list * args)
76 : {
77 2 : udp_connection_t *uc = va_arg (*args, udp_connection_t *);
78 2 : int i, last = -1;
79 :
80 12 : for (i = 0; i < UDP_CONN_N_FLAGS; i++)
81 10 : if (uc->flags & (1 << i))
82 4 : last = i;
83 5 : for (i = 0; i < last; i++)
84 : {
85 3 : if (uc->flags & (1 << i))
86 2 : s = format (s, "%s, ", udp_connection_flags_str[i]);
87 : }
88 2 : if (last >= 0)
89 2 : s = format (s, "%s", udp_connection_flags_str[last]);
90 2 : return s;
91 : }
92 :
93 : static u8 *
94 2 : format_udp_vars (u8 * s, va_list * args)
95 : {
96 2 : udp_connection_t *uc = va_arg (*args, udp_connection_t *);
97 :
98 2 : 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 2 : s = format (s, " fib_index: %u next_node: %u opaque: %u ", uc->c_fib_index);
101 2 : if (!(uc->flags & UDP_CONN_F_LISTEN))
102 1 : s = format (s, " sw_if_index: %d mss: %u\n", uc->sw_if_index, uc->mss);
103 : else
104 1 : s = format (s, "\n");
105 :
106 2 : return s;
107 : }
108 :
109 : u8 *
110 2 : format_udp_connection (u8 * s, va_list * args)
111 : {
112 2 : udp_connection_t *uc = va_arg (*args, udp_connection_t *);
113 2 : u32 verbose = va_arg (*args, u32);
114 2 : if (!uc)
115 0 : return s;
116 2 : s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_udp_connection_id, uc);
117 2 : if (verbose)
118 : {
119 2 : s = format (s, "%-" SESSION_CLI_STATE_LEN "s",
120 2 : (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
121 2 : if (verbose > 1)
122 2 : s = format (s, "\n%U", format_udp_vars, uc);
123 : }
124 2 : return s;
125 : }
126 :
127 : static clib_error_t *
128 559 : udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
129 : {
130 559 : udp_main_t *um = &udp_main;
131 : u32 tmp;
132 :
133 559 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
134 : {
135 0 : if (unformat (input, "mtu %u", &tmp))
136 0 : um->default_mtu = tmp;
137 0 : else if (unformat (input, "icmp-unreachable-disabled"))
138 0 : um->icmp_send_unreachable_disabled = 1;
139 0 : else if (unformat (input, "no-csum-offload"))
140 0 : um->csum_offload = 0;
141 : else
142 0 : return clib_error_return (0, "unknown input `%U'",
143 : format_unformat_error, input);
144 : }
145 559 : return 0;
146 : }
147 :
148 7306 : VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
149 :
150 : static clib_error_t *
151 0 : show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
152 : vlib_cli_command_t * cmd_arg)
153 : {
154 0 : udp_main_t *um = vnet_get_udp_main ();
155 :
156 0 : clib_error_t *error = NULL;
157 :
158 0 : if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
159 0 : return clib_error_return (0, "unknown input `%U'", format_unformat_error,
160 : input);
161 :
162 : udp_dst_port_info_t *port_info;
163 0 : if (um->punt_unknown4)
164 : {
165 0 : vlib_cli_output (vm, "IPv4 UDP punt: enabled");
166 : }
167 : else
168 : {
169 0 : u8 *s = NULL;
170 0 : vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
171 : {
172 0 : if (udp_is_valid_dst_port (port_info->dst_port, 1))
173 : {
174 0 : s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
175 : }
176 : }
177 0 : s = format (s, "%c", 0);
178 0 : vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
179 : }
180 :
181 0 : if (um->punt_unknown6)
182 : {
183 0 : vlib_cli_output (vm, "IPv6 UDP punt: enabled");
184 : }
185 : else
186 : {
187 0 : u8 *s = NULL;
188 0 : vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
189 : {
190 0 : if (udp_is_valid_dst_port (port_info->dst_port, 01))
191 : {
192 0 : s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
193 : }
194 : }
195 0 : s = format (s, "%c", 0);
196 0 : vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
197 : }
198 :
199 0 : return (error);
200 : }
201 : /* *INDENT-OFF* */
202 272887 : VLIB_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 :
210 : static void
211 0 : table_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;
215 :
216 0 : if (bind && !udp_is_valid_dst_port (port, is_ip4))
217 0 : return;
218 :
219 0 : pi = udp_get_dst_port_info (um, port, is_ip4);
220 0 : if (!pi)
221 0 : return;
222 :
223 0 : table_format_cell (t, *c, 0, "%d", pi->dst_port);
224 0 : table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
225 0 : table_format_cell (t, *c, 2, ~0 == pi->node_index ? "none" : "%U",
226 : format_vlib_node_name, vm, pi->node_index);
227 0 : table_format_cell (t, *c, 3, "%s", pi->name);
228 :
229 0 : (*c)++;
230 : }
231 :
232 : static void
233 0 : table_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 0 : if (ip4)
237 0 : table_format_udp_port_ (vm, um, t, c, port, bind, 1 /* is_ip4 */);
238 0 : if (ip6)
239 0 : table_format_udp_port_ (vm, um, t, c, port, bind, 0 /* is_ip4 */);
240 0 : }
241 :
242 : static clib_error_t *
243 0 : show_udp_ports (vlib_main_t *vm, unformat_input_t *input,
244 : vlib_cli_command_t *cmd)
245 : {
246 0 : table_t table = {}, *t = &table;
247 0 : udp_main_t *um = &udp_main;
248 0 : clib_error_t *err = 0;
249 0 : int ip4 = 1, ip6 = 1;
250 0 : int port = -1;
251 0 : int bind = 1;
252 0 : int c = 0;
253 :
254 0 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
255 : {
256 0 : if (unformat (input, "ip4"))
257 0 : ip6 = 0;
258 0 : else if (unformat (input, "ip6"))
259 0 : ip4 = 0;
260 0 : else if (unformat (input, "bind"))
261 0 : bind = 1;
262 0 : else if (unformat (input, "all"))
263 0 : bind = 0;
264 0 : else if (unformat (input, "%d", &port))
265 : ;
266 : else
267 : {
268 0 : err = clib_error_return (0, "unknown input `%U'",
269 : format_unformat_error, input);
270 0 : goto out;
271 : }
272 : }
273 :
274 0 : table_add_header_col (t, 4, "port", "proto", "node", "desc");
275 :
276 0 : if (port > 65535)
277 : {
278 0 : err = clib_error_return (0, "wrong port %d", port);
279 0 : goto out;
280 : }
281 0 : else if (port < 0)
282 : {
283 0 : for (port = 0; port < 65536; port++)
284 0 : table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
285 : }
286 : else
287 : {
288 0 : table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
289 : }
290 :
291 0 : vlib_cli_output (vm, "%U", format_table, t);
292 :
293 0 : out:
294 0 : table_free (t);
295 0 : return err;
296 : }
297 :
298 272887 : VLIB_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 :
305 : /*
306 : * fd.io coding-style-patch-verification: ON
307 : *
308 : * Local Variables:
309 : * eval: (c-set-style "gnu")
310 : * End:
311 : */
|