FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
nat66_cli.c
Go to the documentation of this file.
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 *
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;
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.",
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.",
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 =
83  "%U NAT66 feature enable/disable failed.",
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.",
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.",
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 =
118  "%U NAT66 feature enable/disable failed.",
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
139 {
140  vlib_main_t *vm = ctx;
141  vnet_main_t *vnm = vnet_get_main ();
142 
145  nat66_interface_is_inside (i) ? "in" : "out");
146  return 0;
147 }
148 
149 static clib_error_t *
151  vlib_cli_command_t * cmd)
152 {
153  vlib_cli_output (vm, "NAT66 interfaces:");
155 
156  return 0;
157 }
158 
159 static clib_error_t *
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
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 
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",
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 *
238  unformat_input_t * input,
239  vlib_cli_command_t * cmd)
240 {
241  vlib_cli_output (vm, "NAT66 static mappings:");
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]",
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",
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]",
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",
311 };
312 
313 /*
314  * fd.io coding-style-patch-verification: ON
315  *
316  * Local Variables:
317  * eval: (c-set-style "gnu")
318  * End:
319  */
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
vlib_main_t * vm
Definition: in2out_ed.c:1582
static clib_error_t * nat66_show_interfaces_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: nat66_cli.c:150
unformat_function_t unformat_vnet_sw_interface
static clib_error_t * nat66_show_static_mappings_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: nat66_cli.c:237
unsigned char u8
Definition: types.h:56
static clib_error_t * nat66_interface_feature_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: nat66_cli.c:24
#define nat66_interface_is_inside(i)
Definition: nat66.h:53
nat66_main_t nat66_main
Definition: nat66.c:26
u32 sw_if_index
Definition: nat66.h:48
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
static int nat66_cli_interface_walk(nat66_interface_t *i, void *ctx)
Definition: nat66_cli.c:138
unformat_function_t unformat_line_input
Definition: format.h:283
format_function_t format_vnet_sw_interface_name
void nat66_static_mappings_walk(nat66_static_mapping_walk_fn_t fn, void *ctx)
Definition: nat66.c:254
counter_t packets
packet counter
Definition: counter_types.h:28
long ctx[MAX_CONNS]
Definition: main.c:144
struct _unformat_input_t unformat_input_t
unformat_function_t unformat_ip6_address
Definition: format.h:89
static int nat66_cli_static_mapping_walk(nat66_static_mapping_t *sm, void *ctx)
Definition: nat66_cli.c:214
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
int nat66_interface_add_del(u32 sw_if_index, u8 is_inside, u8 is_add)
Definition: nat66.c:90
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:92
ip6_address_t e_addr
Definition: nat66.h:28
format_function_t format_ip6_address
Definition: format.h:91
static void vlib_get_combined_counter(const vlib_combined_counter_main_t *cm, u32 index, vlib_counter_t *result)
Get the value of a combined counter, never called in the speed path Scrapes the entire set of per-thr...
Definition: counter.h:259
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:696
counter_t bytes
byte counter
Definition: counter_types.h:29
ip6_address_t l_addr
Definition: nat66.h:27
vlib_combined_counter_main_t session_counters
Session counters.
Definition: nat66.h:67
void nat66_interfaces_walk(nat66_interface_walk_fn_t fn, void *ctx)
Definition: nat66.c:136
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
fib_table_t * fib_table_get(fib_node_index_t index, fib_protocol_t proto)
Get a pointer to a FIB table.
Definition: fib_table.c:29
int nat66_static_mapping_add_del(ip6_address_t *l_addr, ip6_address_t *e_addr, u32 vrf_id, u8 is_add)
Definition: nat66.c:175
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
nat66_static_mapping_t * sm
Static mapping pool.
Definition: nat66.h:61
u32 vrf_id
Definition: nat.api:944
NAT66 global declarations.
static clib_error_t * nat66_add_del_static_mapping_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: nat66_cli.c:160
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
A protocol Independent FIB table.
Definition: fib_table.h:71