blob: 09e3a8a3d7f30789de661d6eaf678aa2f757fb48 [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
16#include <vnet/udp/udp.h>
Florin Coras7bf6ed62020-09-23 12:02:08 -070017#include <vnet/session/session_types.h>
Florin Corasc98ef752020-04-07 17:30:13 +000018
19u8 *
20format_udp_connection_id (u8 * s, va_list * args)
21{
22 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
23 if (!uc)
24 return s;
25 if (uc->c_is_ip4)
26 s = format (s, "[%u:%u][%s] %U:%d->%U:%d", uc->c_thread_index,
27 uc->c_s_index, "U", format_ip4_address, &uc->c_lcl_ip4,
28 clib_net_to_host_u16 (uc->c_lcl_port), format_ip4_address,
29 &uc->c_rmt_ip4, clib_net_to_host_u16 (uc->c_rmt_port));
30 else
31 s = format (s, "[%u:%u][%s] %U:%d->%U:%d", uc->c_thread_index,
32 uc->c_s_index, "U", format_ip6_address, &uc->c_lcl_ip6,
33 clib_net_to_host_u16 (uc->c_lcl_port), format_ip6_address,
34 &uc->c_rmt_ip6, clib_net_to_host_u16 (uc->c_rmt_port));
35 return s;
36}
37
38static const char *udp_connection_flags_str[] = {
39#define _(sym, str) str,
40 foreach_udp_connection_flag
41#undef _
42};
43
44static u8 *
45format_udp_connection_flags (u8 * s, va_list * args)
46{
47 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
48 int i, last = -1;
49
50 for (i = 0; i < UDP_CONN_N_FLAGS; i++)
51 if (uc->flags & (1 << i))
52 last = i;
53 for (i = 0; i < last; i++)
54 {
55 if (uc->flags & (1 << i))
56 s = format (s, "%s, ", udp_connection_flags_str[i]);
57 }
58 if (last >= 0)
59 s = format (s, "%s", udp_connection_flags_str[last]);
60 return s;
61}
62
63static u8 *
64format_udp_vars (u8 * s, va_list * args)
65{
66 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
67 s = format (s, " index %u flags: %U", uc->c_c_index,
68 format_udp_connection_flags, uc);
69
70 if (!(uc->flags & UDP_CONN_F_LISTEN))
71 s = format (s, "\n");
72 return s;
73}
74
75u8 *
76format_udp_connection (u8 * s, va_list * args)
77{
78 udp_connection_t *uc = va_arg (*args, udp_connection_t *);
79 u32 verbose = va_arg (*args, u32);
80 if (!uc)
81 return s;
Florin Coras7bf6ed62020-09-23 12:02:08 -070082 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_udp_connection_id, uc);
Florin Corasc98ef752020-04-07 17:30:13 +000083 if (verbose)
84 {
Florin Coras7bf6ed62020-09-23 12:02:08 -070085 s = format (s, "%-" SESSION_CLI_STATE_LEN "s",
Florin Corasc98ef752020-04-07 17:30:13 +000086 (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
87 if (verbose > 1)
88 s = format (s, "\n%U", format_udp_vars, uc);
89 }
90 return s;
91}
92
93static clib_error_t *
94udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
95{
96 udp_main_t *um = &udp_main;
97 u32 tmp;
98
99 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
100 {
101 if (unformat (input, "mtu %u", &tmp))
102 um->default_mtu = tmp;
103 else
104 return clib_error_return (0, "unknown input `%U'",
105 format_unformat_error, input);
106 }
107 return 0;
108}
109
110VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");
111
112static clib_error_t *
113show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
114 vlib_cli_command_t * cmd_arg)
115{
116 udp_main_t *um = vnet_get_udp_main ();
117
118 clib_error_t *error = NULL;
119
120 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
121 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
122 input);
123
124 udp_dst_port_info_t *port_info;
125 if (um->punt_unknown4)
126 {
127 vlib_cli_output (vm, "IPv4 UDP punt: enabled");
128 }
129 else
130 {
131 u8 *s = NULL;
132 vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
133 {
134 if (udp_is_valid_dst_port (port_info->dst_port, 1))
135 {
136 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
137 }
138 }
139 s = format (s, "%c", 0);
140 vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
141 }
142
143 if (um->punt_unknown6)
144 {
145 vlib_cli_output (vm, "IPv6 UDP punt: enabled");
146 }
147 else
148 {
149 u8 *s = NULL;
150 vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
151 {
152 if (udp_is_valid_dst_port (port_info->dst_port, 01))
153 {
154 s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
155 }
156 }
157 s = format (s, "%c", 0);
158 vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
159 }
160
161 return (error);
162}
163/* *INDENT-OFF* */
164VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
165{
166 .path = "show udp punt",
167 .short_help = "show udp punt [ipv4|ipv6]",
168 .function = show_udp_punt_fn,
169};
170/* *INDENT-ON* */
171
172/*
173 * fd.io coding-style-patch-verification: ON
174 *
175 * Local Variables:
176 * eval: (c-set-style "gnu")
177 * End:
178 */