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