summaryrefslogtreecommitdiffstats
path: root/src/vnet/udp/udp_cli.c
blob: 6c8992cd0dec5152a13b78604de6ba7fc3a6443c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vppinfra/error.h>
#include <vppinfra/format.h>
#include <vppinfra/format_table.h>
#include <vnet/udp/udp.h>
#include <vnet/session/session_types.h>

u8 *
format_udp_connection_id (u8 * s, va_list * args)
{
  udp_connection_t *uc = va_arg (*args, udp_connection_t *);
  if (!uc)
    return s;
  if (uc->c_is_ip4)
    s = format (s, "[%u:%u][%s] %U:%d->%U:%d", uc->c_thread_index,
		uc->c_s_index, "U", format_ip4_address, &uc->c_lcl_ip4,
		clib_net_to_host_u16 (uc->c_lcl_port), format_ip4_address,
		&uc->c_rmt_ip4, clib_net_to_host_u16 (uc->c_rmt_port));
  else
    s = format (s, "[%u:%u][%s] %U:%d->%U:%d", uc->c_thread_index,
		uc->c_s_index, "U", format_ip6_address, &uc->c_lcl_ip6,
		clib_net_to_host_u16 (uc->c_lcl_port), format_ip6_address,
		&uc->c_rmt_ip6, clib_net_to_host_u16 (uc->c_rmt_port));
  return s;
}

static const char *udp_cfg_flags_str[] = {
#define _(sym, str) str,
  foreach_udp_cfg_flag
#undef _
};

static u8 *
format_udp_cfg_flags (u8 *s, va_list *args)
{
  udp_connection_t *tc = va_arg (*args, udp_connection_t *);
  int i, last = -1;

  for (i = 0; i < UDP_CFG_N_FLAG_BITS; i++)
    if (tc->cfg_flags & (1 << i))
      last = i;
  if (last >= 0)
    s = format (s, " cfg: ");
  for (i = 0; i < last; i++)
    {
      if (tc->cfg_flags & (1 << i))
	s = format (s, "%s, ", udp_cfg_flags_str[i]);
    }
  if (last >= 0)
    s = format (s, "%s", udp_cfg_flags_str[last]);
  return s;
}

static const char *udp_connection_flags_str[] = {
#define _(sym, str) str,
  foreach_udp_connection_flag
#undef _
};

static u8 *
format_udp_connection_flags (u8 * s, va_list * args)
{
  udp_connection_t *uc = va_arg (*args, udp_connection_t *);
  int i, last = -1;

  for (i = 0; i < UDP_CONN_N_FLAGS; i++)
    if (uc->flags & (1 << i))
      last = i;
  for (i = 0; i < last; i++)
    {
      if (uc->flags & (1 << i))
	s = format (s, "%s, ", udp_connection_flags_str[i]);
    }
  if (last >= 0)
    s = format (s, "%s", udp_connection_flags_str[last]);
  return s;
}

static u8 *
format_udp_vars (u8 * s, va_list * args)
{
  udp_connection_t *uc = va_arg (*args, udp_connection_t *);

  s = format (s, " index %u%U flags: %U\n", uc->c_c_index,
	      format_udp_cfg_flags, uc, format_udp_connection_flags, uc);
  s = format (s, " fib_index: %u next_node: %u opaque: %u ", uc->c_fib_index);
  if (!(uc->flags & UDP_CONN_F_LISTEN))
    s = format (s, " sw_if_index: %d mss: %u\n", uc->sw_if_index, uc->mss);
  else
    s = format (s, "\n");

  return s;
}

u8 *
format_udp_connection (u8 * s, va_list * args)
{
  udp_connection_t *uc = va_arg (*args, udp_connection_t *);
  u32 verbose = va_arg (*args, u32);
  if (!uc)
    return s;
  s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_udp_connection_id, uc);
  if (verbose)
    {
      s = format (s, "%-" SESSION_CLI_STATE_LEN "s",
		  (uc->flags & UDP_CONN_F_LISTEN) ? "LISTEN" : "OPENED", uc);
      if (verbose > 1)
	s = format (s, "\n%U", format_udp_vars, uc);
    }
  return s;
}

static clib_error_t *
udp_config_fn (vlib_main_t * vm, unformat_input_t * input)
{
  udp_main_t *um = &udp_main;
  u32 tmp;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "mtu %u", &tmp))
	um->default_mtu = tmp;
      else if (unformat (input, "icmp-unreachable-disabled"))
	um->icmp_send_unreachable_disabled = 1;
      else if (unformat (input, "no-csum-offload"))
	um->csum_offload = 0;
      else
	return clib_error_return (0, "unknown input `%U'",
				  format_unformat_error, input);
    }
  return 0;
}

VLIB_CONFIG_FUNCTION (udp_config_fn, "udp");

static clib_error_t *
show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
		  vlib_cli_command_t * cmd_arg)
{
  udp_main_t *um = vnet_get_udp_main ();

  clib_error_t *error = NULL;

  if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    return clib_error_return (0, "unknown input `%U'", format_unformat_error,
			      input);

  udp_dst_port_info_t *port_info;
  if (um->punt_unknown4)
    {
      vlib_cli_output (vm, "IPv4 UDP punt: enabled");
    }
  else
    {
      u8 *s = NULL;
      vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
      {
	if (udp_is_valid_dst_port (port_info->dst_port, 1))
	  {
	    s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
	  }
      }
      s = format (s, "%c", 0);
      vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
    }

  if (um->punt_unknown6)
    {
      vlib_cli_output (vm, "IPv6 UDP punt: enabled");
    }
  else
    {
      u8 *s = NULL;
      vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
      {
	if (udp_is_valid_dst_port (port_info->dst_port, 0))
	  {
	    s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
	  }
      }
      s = format (s, "%c", 0);
      vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
    }

  return (error);
}
VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
{
  .path = "show udp punt",
  .short_help = "show udp punt [ipv4|ipv6]",
  .function = show_udp_punt_fn,
};

static void
table_format_udp_port_ (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
			int port, int bind, int is_ip4)
{
  const udp_dst_port_info_t *pi;

  if (bind && !udp_is_valid_dst_port (port, is_ip4))
    return;

  pi = udp_get_dst_port_info (um, port, is_ip4);
  if (!pi)
    return;

  table_format_cell (t, *c, 0, "%d", pi->dst_port);
  table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
  table_format_cell (t, *c, 2, ~0 == pi->node_index ? "none" : "%U",
		     format_vlib_node_name, vm, pi->node_index);
  table_format_cell (t, *c, 3, "%s", pi->name);

  (*c)++;
}

static void
table_format_udp_port (vlib_main_t *vm, udp_main_t *um, table_t *t, int *c,
		       int port, int bind, int ip4, int ip6)
{
  if (ip4)
    table_format_udp_port_ (vm, um, t, c, port, bind, 1 /* is_ip4 */);
  if (ip6)
    table_format_udp_port_ (vm, um, t, c, port, bind, 0 /* is_ip4 */);
}

static clib_error_t *
show_udp_ports (vlib_main_t *vm, unformat_input_t *input,
		vlib_cli_command_t *cmd)
{
  table_t table = {}, *t = &table;
  udp_main_t *um = &udp_main;
  clib_error_t *err = 0;
  int ip4 = 1, ip6 = 1;
  int port = -1;
  int bind = 1;
  int c = 0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "ip4"))
	ip6 = 0;
      else if (unformat (input, "ip6"))
	ip4 = 0;
      else if (unformat (input, "bind"))
	bind = 1;
      else if (unformat (input, "all"))
	bind = 0;
      else if (unformat (input, "%d", &port))
	;
      else
	{
	  err = clib_error_return (0, "unknown input `%U'",
				   format_unformat_error, input);
	  goto out;
	}
    }

  table_add_header_col (t, 4, "port", "proto", "node", "desc");

  if (port > 65535)
    {
      err = clib_error_return (0, "wrong port %d", port);
      goto out;
    }
  else if (port < 0)
    {
      for (port = 0; port < 65536; port++)
	table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
    }
  else
    {
      table_format_udp_port (vm, um, t, &c, port, bind, ip4, ip6);
    }

  vlib_cli_output (vm, "%U", format_table, t);

out:
  table_free (t);
  return err;
}

VLIB_CLI_COMMAND (show_udp_ports_cmd, static) = {
  .path = "show udp ports",
  .function = show_udp_ports,
  .short_help = "show udp ports [ip4|ip6] [bind|all|<port>]",
  .is_mp_safe = 1,
};

static void
table_format_udp_transport_port_ (vlib_main_t *vm, table_t *t, int *c,
				  int port, int is_ip4)
{
  udp_main_t *um = &udp_main;
  u32 refcnt;
  u16 port_ne;

  port_ne = clib_host_to_net_u16 (port);
  refcnt = um->transport_ports_refcnt[is_ip4][port_ne];
  if (!refcnt)
    return;

  if (!udp_is_valid_dst_port (port, is_ip4))
    {
      clib_warning ("Port %u is not registered refcnt %u!", port, refcnt);
      return;
    }

  table_format_cell (t, *c, 0, "%d", port);
  table_format_cell (t, *c, 1, is_ip4 ? "ip4" : "ip6");
  table_format_cell (t, *c, 2, "%d", refcnt);

  (*c)++;
}

static void
table_format_udp_transport_port (vlib_main_t *vm, table_t *t, int *c, int port,
				 int ipv)
{
  if (ipv == -1 || ipv == 0)
    table_format_udp_transport_port_ (vm, t, c, port, 1 /* is_ip4 */);
  if (ipv == -1 || ipv == 1)
    table_format_udp_transport_port_ (vm, t, c, port, 0 /* is_ip4 */);
}

static clib_error_t *
show_udp_transport_ports (vlib_main_t *vm, unformat_input_t *input,
			  vlib_cli_command_t *cmd)
{
  table_t table = {}, *t = &table;
  int ipv = -1, port = -1, c = 0;
  clib_error_t *err = 0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "ip4"))
	ipv = 0;
      else if (unformat (input, "ip6"))
	ipv = 1;
      else if (unformat (input, "%d", &port))
	;
      else
	{
	  err = clib_error_return (0, "unknown input `%U'",
				   format_unformat_error, input);
	  goto out;
	}
    }

  table_add_header_col (t, 3, "port", "proto", "ref-cnt");

  if (port > 65535)
    {
      err = clib_error_return (0, "wrong port %d", port);
      goto out;
    }

  if (port < 0)
    {
      for (port = 0; port < 65536; port++)
	table_format_udp_transport_port (vm, t, &c, port, ipv);
    }
  else
    {
      table_format_udp_transport_port (vm, t, &c, port, ipv);
    }

  vlib_cli_output (vm, "%U\n", format_table, t);

out:
  table_free (t);
  return err;
}

VLIB_CLI_COMMAND (show_udp_transport_ports_cmd, static) = {
  .path = "show udp transport ports",
  .function = show_udp_transport_ports,
  .short_help = "show udp transport ports [ip4|ip6] [<port>]",
  .is_mp_safe = 1,
};

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */