npt66: network prefix translation for ipv6
[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 };