3b7bb380c897f3a2ba521ff8c126e6e6b16c2fa5
[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_connection_flags_str[] = {
42 #define _(sym, str) str,
43   foreach_udp_connection_flag
44 #undef _
45 };
46
47 static u8 *
48 format_udp_connection_flags (u8 * s, va_list * args)
49 {
50   udp_connection_t *uc = va_arg (*args, udp_connection_t *);
51   int i, last = -1;
52
53   for (i = 0; i < UDP_CONN_N_FLAGS; i++)
54     if (uc->flags & (1 << i))
55       last = i;
56   for (i = 0; i < last; i++)
57     {
58       if (uc->flags & (1 << i))
59         s = format (s, "%s, ", udp_connection_flags_str[i]);
60     }
61   if (last >= 0)
62     s = format (s, "%s", udp_connection_flags_str[last]);
63   return s;
64 }
65
66 static u8 *
67 format_udp_vars (u8 * s, va_list * args)
68 {
69   udp_connection_t *uc = va_arg (*args, udp_connection_t *);
70   s = format (s, " index %u flags: %U", uc->c_c_index,
71               format_udp_connection_flags, uc);
72
73   if (!(uc->flags & UDP_CONN_F_LISTEN))
74     s = format (s, "\n");
75   return s;
76 }
77
78 u8 *
79 format_udp_connection (u8 * s, va_list * args)
80 {
81   udp_connection_t *uc = va_arg (*args, udp_connection_t *);
82   u32 verbose = va_arg (*args, u32);
83   if (!uc)
84     return s;
85   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_udp_connection_id, uc);
86   if (verbose)
87     {
88       s = format (s, "%-" SESSION_CLI_STATE_LEN "s",
89                   (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
90       if (verbose > 1)
91         s = format (s, "\n%U", format_udp_vars, uc);
92     }
93   return s;
94 }
95
96 static clib_error_t *
97 udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
98 {
99   udp_main_t *um = &udp_main;
100   u32 tmp;
101
102   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
103     {
104       if (unformat (input, "mtu %u", &tmp))
105         um->default_mtu = tmp;
106       else if (unformat (input, "icmp-unreachable-disabled"))
107         um->icmp_send_unreachable_disabled = 1;
108       else
109         return clib_error_return (0, "unknown input `%U'",
110                                   format_unformat_error, input);
111     }
112   return 0;
113 }
114
115 VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
116
117 static clib_error_t *
118 show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
119                   vlib_cli_command_t * cmd_arg)
120 {
121   udp_main_t *um = vnet_get_udp_main ();
122
123   clib_error_t *error = NULL;
124
125   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
126     return clib_error_return (0, "unknown input `%U'", format_unformat_error,
127                               input);
128
129   udp_dst_port_info_t *port_info;
130   if (um->punt_unknown4)
131     {
132       vlib_cli_output (vm, "IPv4 UDP punt: enabled");
133     }
134   else
135     {
136       u8 *s = NULL;
137       vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
138       {
139         if (udp_is_valid_dst_port (port_info->dst_port, 1))
140           {
141             s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
142           }
143       }
144       s = format (s, "%c", 0);
145       vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
146     }
147
148   if (um->punt_unknown6)
149     {
150       vlib_cli_output (vm, "IPv6 UDP punt: enabled");
151     }
152   else
153     {
154       u8 *s = NULL;
155       vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
156       {
157         if (udp_is_valid_dst_port (port_info->dst_port, 01))
158           {
159             s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
160           }
161       }
162       s = format (s, "%c", 0);
163       vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
164     }
165
166   return (error);
167 }
168 /* *INDENT-OFF* */
169 VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
170 {
171   .path = "show udp punt",
172   .short_help = "show udp punt [ipv4|ipv6]",
173   .function = show_udp_punt_fn,
174 };
175 /* *INDENT-ON* */
176
177 static void
178 table_format_udp_port_ (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
179                         int port, int bind, int is_ip4)
180 {
181   const udp_dst_port_info_t *pi = udp_get_dst_port_info (um, port, is_ip4);
182   if (!pi)
183     return;
184   if (bind && ~0 == pi->node_index)
185     return;
186   table_format_cell (t, *c, 0, "%d", pi->dst_port);
187   table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
188   table_format_cell (t, *c, 2, ~0 == pi->node_index ? "none" : "%U",
189                      format_vlib_node_name, vm, pi->node_index);
190   table_format_cell (t, *c, 3, "%s", pi->name);
191   (*c)++;
192 }
193
194 static void
195 table_format_udp_port (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
196                        int port, int bind, int ip4, int ip6)
197 {
198   if (ip4)
199     table_format_udp_port_ (vm, um, t, c, port, bind, 1 /* is_ip4 */);
200   if (ip6)
201     table_format_udp_port_ (vm, um, t, c, port, bind, 0 /* is_ip4 */);
202 }
203
204 static clib_error_t *
205 show_udp_ports (vlib_main_t *vm, unformat_input_t *input,
206                 vlib_cli_command_t *cmd)
207 {
208   table_t table = {}, *t = &table;
209   udp_main_t *um = &udp_main;
210   clib_error_t *err = 0;
211   int ip4 = 1, ip6 = 1;
212   int port = -1;
213   int bind = 1;
214   int c = 0;
215
216   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
217     {
218       if (unformat (input, "ip4"))
219         ip6 = 0;
220       else if (unformat (input, "ip6"))
221         ip4 = 0;
222       else if (unformat (input, "bind"))
223         bind = 1;
224       else if (unformat (input, "all"))
225         bind = 0;
226       else if (unformat (input, "%d", &port))
227         ;
228       else
229         {
230           err = clib_error_return (0, "unknown input `%U'",
231                                    format_unformat_error, input);
232           goto out;
233         }
234     }
235
236   table_add_header_col (t, 4, "port", "proto", "node", "desc");
237
238   if (port > 65535)
239     {
240       err = clib_error_return (0, "wrong port %d", port);
241       goto out;
242     }
243   else if (port < 0)
244     {
245       for (port = 0; port < 65536; port++)
246         table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
247     }
248   else
249     {
250       table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
251     }
252
253   vlib_cli_output (vm, "%U", format_table, t);
254
255 out:
256   table_free (t);
257   return err;
258 }
259
260 VLIB_CLI_COMMAND (show_udp_ports_cmd, static) = {
261   .path = "show udp ports",
262   .function = show_udp_ports,
263   .short_help = "show udp ports [ip4|ip6] [bind|all|<port>]",
264   .is_mp_safe = 1,
265 };
266
267 /*
268  * fd.io coding-style-patch-verification: ON
269  *
270  * Local Variables:
271  * eval: (c-set-style "gnu")
272  * End:
273  */