api: clean up use of deprecated flag
[vpp.git] / src / plugins / nat / nat66 / nat66_cli.c
1 /*
2  * Copyright (c) 2018 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  * @file
17  * @brief NAT66 CLI
18  */
19
20 #include <nat/nat66/nat66.h>
21 #include <vnet/fib/fib_table.h>
22
23 static clib_error_t *
24 nat66_interface_feature_command_fn (vlib_main_t * vm,
25                                     unformat_input_t * input,
26                                     vlib_cli_command_t * cmd)
27 {
28   unformat_input_t _line_input, *line_input = &_line_input;
29   vnet_main_t *vnm = vnet_get_main ();
30   clib_error_t *error = 0;
31   u32 sw_if_index;
32   u32 *inside_sw_if_indices = 0;
33   u32 *outside_sw_if_indices = 0;
34   u8 is_add = 1;
35   int i, rv;
36
37   /* Get a line of input. */
38   if (!unformat_user (input, unformat_line_input, line_input))
39     return 0;
40
41   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
42     {
43       if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
44                     vnm, &sw_if_index))
45         vec_add1 (inside_sw_if_indices, sw_if_index);
46       else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
47                          vnm, &sw_if_index))
48         vec_add1 (outside_sw_if_indices, sw_if_index);
49       else if (unformat (line_input, "del"))
50         is_add = 0;
51       else
52         {
53           error = clib_error_return (0, "unknown input '%U'",
54                                      format_unformat_error, line_input);
55           goto done;
56         }
57     }
58
59   if (vec_len (inside_sw_if_indices))
60     {
61       for (i = 0; i < vec_len (inside_sw_if_indices); i++)
62         {
63           sw_if_index = inside_sw_if_indices[i];
64           rv = nat66_interface_add_del (sw_if_index, 1, is_add);
65           switch (rv)
66             {
67             case VNET_API_ERROR_NO_SUCH_ENTRY:
68               error =
69                 clib_error_return (0, "%U NAT66 feature not enabled.",
70                                    format_vnet_sw_interface_name, vnm,
71                                    vnet_get_sw_interface (vnm, sw_if_index));
72               goto done;
73             case VNET_API_ERROR_VALUE_EXIST:
74               error =
75                 clib_error_return (0, "%U NAT66 feature already enabled.",
76                                    format_vnet_sw_interface_name, vnm,
77                                    vnet_get_sw_interface (vnm, sw_if_index));
78               goto done;
79             case VNET_API_ERROR_INVALID_VALUE:
80             case VNET_API_ERROR_INVALID_VALUE_2:
81               error =
82                 clib_error_return (0,
83                                    "%U NAT66 feature enable/disable failed.",
84                                    format_vnet_sw_interface_name, vnm,
85                                    vnet_get_sw_interface (vnm, sw_if_index));
86               goto done;
87             default:
88               break;
89
90             }
91         }
92     }
93
94   if (vec_len (outside_sw_if_indices))
95     {
96       for (i = 0; i < vec_len (outside_sw_if_indices); i++)
97         {
98           sw_if_index = outside_sw_if_indices[i];
99           rv = nat66_interface_add_del (sw_if_index, 0, is_add);
100           switch (rv)
101             {
102             case VNET_API_ERROR_NO_SUCH_ENTRY:
103               error =
104                 clib_error_return (0, "%U NAT66 feature not enabled.",
105                                    format_vnet_sw_interface_name, vnm,
106                                    vnet_get_sw_interface (vnm, sw_if_index));
107               goto done;
108             case VNET_API_ERROR_VALUE_EXIST:
109               error =
110                 clib_error_return (0, "%U NAT66 feature already enabled.",
111                                    format_vnet_sw_interface_name, vnm,
112                                    vnet_get_sw_interface (vnm, sw_if_index));
113               goto done;
114             case VNET_API_ERROR_INVALID_VALUE:
115             case VNET_API_ERROR_INVALID_VALUE_2:
116               error =
117                 clib_error_return (0,
118                                    "%U NAT66 feature enable/disable failed.",
119                                    format_vnet_sw_interface_name, vnm,
120                                    vnet_get_sw_interface (vnm, sw_if_index));
121               goto done;
122             default:
123               break;
124
125             }
126         }
127     }
128
129 done:
130   unformat_free (line_input);
131   vec_free (inside_sw_if_indices);
132   vec_free (outside_sw_if_indices);
133
134   return error;
135 }
136
137 static int
138 nat66_cli_interface_walk (nat66_interface_t * i, void *ctx)
139 {
140   vlib_main_t *vm = ctx;
141   vnet_main_t *vnm = vnet_get_main ();
142
143   vlib_cli_output (vm, " %U %s", format_vnet_sw_interface_name, vnm,
144                    vnet_get_sw_interface (vnm, i->sw_if_index),
145                    nat66_interface_is_inside (i) ? "in" : "out");
146   return 0;
147 }
148
149 static clib_error_t *
150 nat66_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
151                                   vlib_cli_command_t * cmd)
152 {
153   vlib_cli_output (vm, "NAT66 interfaces:");
154   nat66_interfaces_walk (nat66_cli_interface_walk, vm);
155
156   return 0;
157 }
158
159 static clib_error_t *
160 nat66_add_del_static_mapping_command_fn (vlib_main_t * vm,
161                                          unformat_input_t * input,
162                                          vlib_cli_command_t * cmd)
163 {
164   unformat_input_t _line_input, *line_input = &_line_input;
165   clib_error_t *error = 0;
166   u8 is_add = 1;
167   ip6_address_t l_addr, e_addr;
168   u32 vrf_id = 0;
169   int rv;
170
171   /* Get a line of input. */
172   if (!unformat_user (input, unformat_line_input, line_input))
173     return 0;
174
175   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
176     {
177       if (unformat (line_input, "local %U external %U",
178                     unformat_ip6_address, &l_addr,
179                     unformat_ip6_address, &e_addr))
180         ;
181       else if (unformat (line_input, "vrf %u", &vrf_id))
182         ;
183       else if (unformat (line_input, "del"))
184         is_add = 0;
185       else
186         {
187           error = clib_error_return (0, "unknown input: '%U'",
188                                      format_unformat_error, line_input);
189           goto done;
190         }
191     }
192
193   rv = nat66_static_mapping_add_del (&l_addr, &e_addr, vrf_id, is_add);
194
195   switch (rv)
196     {
197     case VNET_API_ERROR_NO_SUCH_ENTRY:
198       error = clib_error_return (0, "NAT66 static mapping entry not exist.");
199       goto done;
200     case VNET_API_ERROR_VALUE_EXIST:
201       error = clib_error_return (0, "NAT66 static mapping entry exist.");
202       goto done;
203     default:
204       break;
205     }
206
207 done:
208   unformat_free (line_input);
209
210   return error;
211 }
212
213 static int
214 nat66_cli_static_mapping_walk (nat66_static_mapping_t * sm, void *ctx)
215 {
216   nat66_main_t *nm = &nat66_main;
217   vlib_main_t *vm = ctx;
218   fib_table_t *fib;
219   vlib_counter_t vc;
220
221   fib = fib_table_get (sm->fib_index, FIB_PROTOCOL_IP6);
222   if (!fib)
223     return -1;
224
225   vlib_get_combined_counter (&nm->session_counters, sm - nm->sm, &vc);
226
227   vlib_cli_output (vm, " local %U external %U vrf %d",
228                    format_ip6_address, &sm->l_addr,
229                    format_ip6_address, &sm->e_addr, fib->ft_table_id);
230   vlib_cli_output (vm, "  total pkts %lld, total bytes %lld", vc.packets,
231                    vc.bytes);
232
233   return 0;
234 }
235
236 static clib_error_t *
237 nat66_show_static_mappings_command_fn (vlib_main_t * vm,
238                                        unformat_input_t * input,
239                                        vlib_cli_command_t * cmd)
240 {
241   vlib_cli_output (vm, "NAT66 static mappings:");
242   nat66_static_mappings_walk (nat66_cli_static_mapping_walk, vm);
243   return 0;
244 }
245
246 /* *INDENT-OFF* */
247 /*?
248  * @cliexpar
249  * @cliexstart{set interface nat66}
250  * Enable/disable NAT66 feature on the interface.
251  * To enable NAT66 feature with local (IPv6) network interface
252  * GigabitEthernet0/8/0 and external (IPv4) network interface
253  * GigabitEthernet0/a/0 use:
254  *  vpp# set interface nat66 in GigabitEthernet0/8/0 out GigabitEthernet0/a/0
255  * @cliexend
256 ?*/
257 VLIB_CLI_COMMAND (set_interface_nat66_command, static) = {
258   .path = "set interface nat66",
259   .short_help = "set interface nat66 in|out <intfc> [del]",
260   .function = nat66_interface_feature_command_fn,
261 };
262
263 /*?
264  * @cliexpar
265  * @cliexstart{show nat66 interfaces}
266  * Show interfaces with NAT66 feature.
267  * To show interfaces with NAT66 feature use:
268  *  vpp# show nat66 interfaces
269  *  NAT66 interfaces:
270  *   GigabitEthernet0/8/0 in
271  *   GigabitEthernet0/a/0 out
272  * @cliexend
273 ?*/
274 VLIB_CLI_COMMAND (show_nat66_interfaces_command, static) = {
275   .path = "show nat66 interfaces",
276   .short_help = "show nat66 interfaces",
277   .function = nat66_show_interfaces_command_fn,
278 };
279
280 /*?
281  * @cliexpar
282  * @cliexstart{nat66 add static mapping}
283  * Add/delete NAT66 static mapping entry.
284  * To add NAT66 static mapping entry use:
285  *  vpp# nat66 add static mapping local fd01:1::4 external 2001:db8:c000:223::
286  *  vpp# nat66 add static mapping local fd01:1::2 external 2001:db8:c000:221:: vrf 10
287  * @cliexend
288 ?*/
289 VLIB_CLI_COMMAND (show_nat66_add_del_static_mapping_command, static) = {
290   .path = "nat66 add static mapping",
291   .short_help = "nat66 add static mapping local <ip6-addr> external <ip6-addr>"
292                 " [vfr <table-id>] [del]",
293   .function = nat66_add_del_static_mapping_command_fn,
294 };
295
296 /*?
297  * @cliexpar
298  * @cliexstart{show nat66 static mappings}
299  * Show NAT66 static mappings.
300  * To show NAT66 static mappings use:
301  *  vpp# show nat66 static mappings
302  *  NAT66 static mappings:
303  *   local fd01:1::4 external 2001:db8:c000:223:: vrf 0
304  *   local fd01:1::2 external 2001:db8:c000:221:: vrf 10
305  * @cliexend
306 ?*/
307 VLIB_CLI_COMMAND (show_nat66_static_mappings_command, static) = {
308   .path = "show nat66 static mappings",
309   .short_help = "show nat66 static mappings",
310   .function = nat66_show_static_mappings_command_fn,
311 };
312
313 /*
314  * fd.io coding-style-patch-verification: ON
315  *
316  * Local Variables:
317  * eval: (c-set-style "gnu")
318  * End:
319  */