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