npt66: add show command and rx/tx counters
[vpp.git] / src / plugins / npt66 / npt66_cli.c
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright(c) 2023 Cisco Systems, Inc.
3
4 #include <stdbool.h>
5 #include <vlib/vlib.h>
6 #include <vnet/feature/feature.h>
7 #include <vnet/ip/ip.h>
8 #include <vppinfra/clib_error.h>
9 #include "npt66.h"
10
11 static clib_error_t *
12 set_npt66_binding_command_fn (vlib_main_t *vm, unformat_input_t *input,
13                               vlib_cli_command_t *cmd)
14 {
15   unformat_input_t _line_input, *line_input = &_line_input;
16   clib_error_t *error = 0;
17   bool internal_set = false, external_set = false;
18   bool add = true;
19   u32 sw_if_index = ~0;
20   ip6_address_t internal, external;
21   int internal_plen = 0, external_plen = 0;
22
23   /* Get a line of input. */
24   if (!unformat_user (input, unformat_line_input, line_input))
25     return 0;
26
27   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
28     {
29       if (unformat (line_input, "internal %U/%d", unformat_ip6_address,
30                     &internal, &internal_plen))
31         internal_set = true;
32       else if (unformat (line_input, "external %U/%d", unformat_ip6_address,
33                          &external, &external_plen))
34         external_set = true;
35       else if (unformat (line_input, "interface %U",
36                          unformat_vnet_sw_interface, vnet_get_main (),
37                          &sw_if_index))
38         ;
39       else if (unformat (line_input, "del"))
40         {
41           add = false;
42         }
43       else
44         {
45           error = clib_error_return (0, "unknown input `%U'",
46                                      format_unformat_error, line_input);
47           goto done;
48         }
49     }
50   if (sw_if_index == ~0)
51     {
52       error = clib_error_return (0, "interface is required `%U'",
53                                  format_unformat_error, line_input);
54       goto done;
55     }
56   if (!internal_set)
57     {
58       error = clib_error_return (0, "missing parameter: internal `%U'",
59                                  format_unformat_error, line_input);
60       goto done;
61     }
62   if (!external_set)
63     {
64       error = clib_error_return (0, "missing parameter: external `%U'",
65                                  format_unformat_error, line_input);
66       goto done;
67     }
68
69   int rv = npt66_binding_add_del (sw_if_index, &internal, internal_plen,
70                                   &external, external_plen, add);
71   if (rv)
72     {
73       error = clib_error_return (0, "Adding binding failed %d", rv);
74       goto done;
75     }
76
77 done:
78   unformat_free (line_input);
79
80   return error;
81 }
82
83 VLIB_CLI_COMMAND (set_npt66_binding_command, static) = {
84   .path = "set npt66 binding",
85   .short_help = "set npt66 binding interface <name> internal <pfx> "
86                 "external <pfx> [del]",
87   .function = set_npt66_binding_command_fn,
88 };
89
90 static u8 *
91 format_npt66_binding (u8 *s, va_list *args)
92 {
93   u32 index = va_arg (*args, u32);
94   npt66_binding_t *b = va_arg (*args, npt66_binding_t *);
95   s = format (s, "[%d] internal: %U/%d external: %U/%d", index,
96               format_ip6_address, &b->internal, b->internal_plen,
97               format_ip6_address, &b->external, b->external_plen);
98   return s;
99 }
100
101 static clib_error_t *
102 show_npt66_bindings_command_fn (vlib_main_t *vm, unformat_input_t *input,
103                                 vlib_cli_command_t *cmd)
104 {
105   npt66_main_t *nm = &npt66_main;
106   npt66_binding_t *b;
107   clib_error_t *error = 0;
108
109   /* Get a line of input. */
110   pool_foreach (b, nm->bindings)
111     {
112       vlib_cli_output (vm, "%U", format_npt66_binding, b - nm->bindings, b);
113     }
114   return error;
115 }
116
117 VLIB_CLI_COMMAND (show_npt66_bindings_command, static) = {
118   .path = "show npt66 bindings",
119   .short_help = "show npt66 bindings",
120   .function = show_npt66_bindings_command_fn,
121 };