1e723923c2ff737914980897eb0b3b5c9767a93c
[vpp.git] / src / vnet / udp / udp_cli.c
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 format_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
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 format_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
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 format_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
93 static u8 *
94 format_udp_vars (u8 * s, va_list * args)
95 {
96   udp_connection_t *uc = va_arg (*args, udp_connection_t *);
97
98   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);
101   if (!(uc->flags & UDP_CONN_F_LISTEN))
102     s = format (s, " sw_if_index: %d mss: %u\n", uc->sw_if_index, uc->mss);
103   else
104     s = format (s, "\n");
105
106   return s;
107 }
108
109 u8 *
110 format_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;
116   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_udp_connection_id, uc);
117   if (verbose)
118     {
119       s = format (s, "%-" SESSION_CLI_STATE_LEN "s",
120                   (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
127 static clib_error_t *
128 udp_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;
137       else if (unformat (input, "icmp-unreachable-disabled"))
138         um->icmp_send_unreachable_disabled = 1;
139       else if (unformat (input, "no-csum-offload"))
140         um->csum_offload = 0;
141       else
142         return clib_error_return (0, "unknown input `%U'",
143                                   format_unformat_error, input);
144     }
145   return 0;
146 }
147
148 VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
149
150 static clib_error_t *
151 show_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* */
202 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 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   if (bind && !udp_is_valid_dst_port (port, is_ip4))
217     return;
218
219   pi = udp_get_dst_port_info (um, port, is_ip4);
220   if (!pi)
221     return;
222
223   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);
228
229   (*c)++;
230 }
231
232 static void
233 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   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
242 static clib_error_t *
243 show_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
293 out:
294   table_free (t);
295   return err;
296 }
297
298 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  */