FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
dslite_cli.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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 #include <nat/dslite/dslite.h>
16 
17 #define DSLITE_EXPECTED_ARGUMENT "expected required argument(s)"
18 
19 static clib_error_t *
21  unformat_input_t * input,
22  vlib_cli_command_t * cmd)
23 {
25  unformat_input_t _line_input, *line_input = &_line_input;
26  ip4_address_t start_addr, end_addr, this_addr;
27  u32 start_host_order, end_host_order;
28  int count, rv;
29  u8 is_add = 1;
30  clib_error_t *error = 0;
31 
32  /* Get a line of input. */
33  if (!unformat_user (input, unformat_line_input, line_input))
35 
36  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
37  {
38  if (unformat (line_input, "%U - %U",
41  ;
42  else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
44  else if (unformat (line_input, "del"))
45  is_add = 0;
46  else
47  {
48  error = clib_error_return (0, "unknown input '%U'",
49  format_unformat_error, line_input);
50  goto done;
51  }
52  }
53 
54  start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
55  end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
56 
57  if (end_host_order < start_host_order)
58  {
59  error = clib_error_return (0, "end address less than start address");
60  goto done;
61  }
62 
63  count = (end_host_order - start_host_order) + 1;
64  this_addr = start_addr;
65 
66  rv = nat_add_del_ip4_pool_addrs (&dm->pool, this_addr, count, is_add, 0);
67 
68  switch (rv)
69  {
70  case VNET_API_ERROR_NO_SUCH_ENTRY:
71  error =
72  clib_error_return (0, "DS-Lite pool address %U not exist.",
73  format_ip4_address, &this_addr);
74  break;
75  case VNET_API_ERROR_VALUE_EXIST:
76  error =
77  clib_error_return (0, "DS-Lite pool address %U exist.",
78  format_ip4_address, &this_addr);
79  break;
80  }
81 
82 done:
83  unformat_free (line_input);
84 
85  return error;
86 }
87 
88 static clib_error_t *
90  unformat_input_t * input,
91  vlib_cli_command_t * cmd)
92 {
95 
96  vlib_cli_output (vm, "DS-Lite pool:");
97 
98  /* *INDENT-OFF* */
100  {
101  vlib_cli_output (vm, "%U", format_ip4_address, &a->addr);
102  }
103  /* *INDENT-ON* */
104  return 0;
105 }
106 
107 static clib_error_t *
109  unformat_input_t * input,
110  vlib_cli_command_t * cmd)
111 {
112  dslite_main_t *dm = &dslite_main;
113  unformat_input_t _line_input, *line_input = &_line_input;
114  ip6_address_t ip6_addr;
115  int rv;
116  clib_error_t *error = 0;
117 
118  /* Get a line of input. */
119  if (!unformat_user (input, unformat_line_input, line_input))
121 
122  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
123  {
124  if (unformat (line_input, "%U", unformat_ip6_address, &ip6_addr))
125  ;
126  else
127  {
128  error = clib_error_return (0, "unknown input '%U'",
129  format_unformat_error, line_input);
130  goto done;
131  }
132  }
133 
134  rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
135 
136  if (rv)
137  error =
139  "Set DS-Lite AFTR tunnel endpoint address failed.");
140 
141 done:
142  unformat_free (line_input);
143 
144  return error;
145 }
146 
147 static clib_error_t *
149  unformat_input_t * input,
150  vlib_cli_command_t * cmd)
151 {
152  dslite_main_t *dm = &dslite_main;
153 
155  return 0;
156 }
157 
158 static clib_error_t *
160  unformat_input_t * input,
161  vlib_cli_command_t * cmd)
162 {
163  dslite_main_t *dm = &dslite_main;
164  unformat_input_t _line_input, *line_input = &_line_input;
165  ip6_address_t ip6_addr;
166  int rv;
167  clib_error_t *error = 0;
168 
169  /* Get a line of input. */
170  if (!unformat_user (input, unformat_line_input, line_input))
172 
173  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
174  {
175  if (unformat (line_input, "%U", unformat_ip6_address, &ip6_addr))
176  ;
177  else
178  {
179  error = clib_error_return (0, "unknown input '%U'",
180  format_unformat_error, line_input);
181  goto done;
182  }
183  }
184 
185  rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
186 
187  if (rv)
188  error =
189  clib_error_return (0, "Set DS-Lite B4 tunnel endpoint address failed.");
190 
191 done:
192  unformat_free (line_input);
193 
194  return error;
195 }
196 
197 static clib_error_t *
199  unformat_input_t * input,
200  vlib_cli_command_t * cmd)
201 {
202  dslite_main_t *dm = &dslite_main;
203 
205  return 0;
206 }
207 
208 static u8 *
209 format_dslite_session (u8 * s, va_list * args)
210 {
211  dslite_session_t *session = va_arg (*args, dslite_session_t *);
212  u32 indent = format_get_indent (s);
213 
214  s = format (s, "%Uin %U:%u out %U:%u protocol %U\n",
215  format_white_space, indent + 2,
216  format_ip4_address, &session->in2out.addr,
217  clib_net_to_host_u16 (session->in2out.port),
218  format_ip4_address, &session->out2in.addr,
219  clib_net_to_host_u16 (session->out2in.port),
220  format_nat_protocol, session->in2out.proto);
221  s = format (s, "%Utotal pkts %d, total bytes %lld\n",
222  format_white_space, indent + 4,
223  session->total_pkts, session->total_bytes);
224  return s;
225 }
226 
227 static u8 *
228 format_dslite_b4 (u8 * s, va_list * args)
229 {
230  dslite_per_thread_data_t *td = va_arg (*args, dslite_per_thread_data_t *);
231  dslite_b4_t *b4 = va_arg (*args, dslite_b4_t *);
232  dlist_elt_t *head, *elt;
233  u32 elt_index, head_index;
234  u32 session_index;
235  dslite_session_t *session;
236 
237  s =
238  format (s, "B4 %U %d sessions\n", format_ip6_address, &b4->addr,
239  b4->nsessions);
240 
241  if (b4->nsessions == 0)
242  return s;
243 
244  head_index = b4->sessions_per_b4_list_head_index;
245  head = pool_elt_at_index (td->list_pool, head_index);
246  elt_index = head->next;
247  elt = pool_elt_at_index (td->list_pool, elt_index);
248  session_index = elt->value;
249  while (session_index != ~0)
250  {
251  session = pool_elt_at_index (td->sessions, session_index);
252  s = format (s, "%U", format_dslite_session, session);
253  elt_index = elt->next;
254  elt = pool_elt_at_index (td->list_pool, elt_index);
255  session_index = elt->value;
256  }
257 
258  return s;
259 }
260 
261 static clib_error_t *
263  unformat_input_t * input,
264  vlib_cli_command_t * cmd)
265 {
266  dslite_main_t *dm = &dslite_main;
268  dslite_b4_t *b4;
269 
270  /* *INDENT-OFF* */
271  vec_foreach (td, dm->per_thread_data)
272  {
273  pool_foreach (b4, td->b4s)
274  {
275  vlib_cli_output (vm, "%U", format_dslite_b4, td, b4);
276  }
277  }
278  /* *INDENT-ON* */
279 
280  return 0;
281 }
282 
283 /* *INDENT-OFF* */
284 
285 /*?
286  * @cliexpar
287  * @cliexstart{dslite add pool address}
288  * Add/delete DS-Lite pool address for AFTR element.
289  * To add DS-Lite pool address use:
290  * vpp# dslite add pool address 10.1.1.3
291  * To add DS-Lite pool address range use:
292  * vpp# dslite add pool address 10.1.1.5 - 10.1.1.7
293  * @cliexend
294 ?*/
296  .path = "dslite add pool address",
297  .short_help = "dslite add pool address <ip4-range-start> [- <ip4-range-end>] "
298  " [del]",
300 };
301 
302 /*?
303  * @cliexpar
304  * @cliexstart{show dslite pool}
305  * Show DS-lite pool addresses.
306  * vpp# show dslite pool
307  * DS-Lite pool:
308  * 10.0.0.3
309  * 10.0.0.5
310  * 10.0.0.6
311  * 10.0.0.7
312  * @cliexend
313 ?*/
315  .path = "show dslite pool",
316  .short_help = "show dslite pool",
317  .function = dslite_show_pool_command_fn,
318 };
319 
320 /*?
321  * @cliexpar
322  * @cliexstart{dslite set aftr-tunnel-endpoint-address}
323  * Set IPv6 tunnel endpoint address of the AFTR element.
324  * To set AFTR tunnel endpoint address use:
325  * vpp# dslite set aftr-tunnel-endpoint-address 2001:db8:85a3::8a2e:370:1
326  * @cliexend
327 ?*/
329  .path = "dslite set aftr-tunnel-endpoint-address",
330  .short_help = "dslite set aftr-tunnel-endpoint-address <ip6>",
332 };
333 
334 /*?
335  * @cliexpar
336  * @cliexstart{show dslite aftr-tunnel-endpoint-address}
337  * Show IPv6 tunnel endpoint address of the AFTR element.
338  * vpp# show dslite aftr-tunnel-endpoint-address
339  * 2001:db8:85a3::8a2e:370:1
340  * @cliexend
341 ?*/
343  .path = "show dslite aftr-tunnel-endpoint-address",
344  .short_help = "show dslite aftr-tunnel-endpoint-address",
346 };
347 
348 /*?
349  * @cliexpar
350  * @cliexstart{dslite set b4-tunnel-endpoint-address}
351  * Set IPv6 tunnel endpoint address of the B4 element.
352  * To set B4 tunnel endpoint address use:
353  * vpp# dslite set b4-tunnel-endpoint-address 2001:db8:62aa::375e:f4c1:1
354  * @cliexend
355 ?*/
357  .path = "dslite set b4-tunnel-endpoint-address",
358  .short_help = "dslite set b4-tunnel-endpoint-address <ip6>",
360 };
361 
362 /*?
363  * @cliexpar
364  * @cliexstart{show dslite b4-tunnel-endpoint-address}
365  * Show IPv6 tunnel endpoint address of the B4 element.
366  * vpp# show dslite b4-tunnel-endpoint-address
367  * 2001:db8:62aa::375e:f4c1:1
368  * @cliexend
369 ?*/
371  .path = "show dslite b4-tunnel-endpoint-address",
372  .short_help = "show dslite b4-tunnel-endpoint-address",
374 };
375 
376 /*?
377  * @cliexpar
378  * @cliexstart{show dslite sessions}
379  * Show DS-Lite sessions.
380  * vpp# show dslite sessions
381  * B4 fd01:2::2 1 sessions
382  * in 192.168.1.1:20000 out 10.0.0.3:16253 protocol udp
383  * total pkts 2, total bytes 136
384  * B4 fd01:2::3 2 sessions
385  * in 192.168.1.1:20001 out 10.0.0.3:18995 protocol tcp
386  * total pkts 2, total bytes 160
387  * in 192.168.1.1:4000 out 10.0.0.3:53893 protocol icmp
388  * total pkts 2, total bytes 136
389  * @cliexend
390 ?*/
392  .path = "show dslite sessions",
393  .short_help = "show dslite sessions",
395 };
396 
397 /* *INDENT-ON* */
398 
399 /*
400  * fd.io coding-style-patch-verification: ON
401  *
402  * Local Variables:
403  * eval: (c-set-style "gnu")
404  * End:
405  */
dslite_b4_t::sessions_per_b4_list_head_index
u32 sessions_per_b4_list_head_index
Definition: dslite.h:80
nat_ip4_pool_addr_s
Definition: alloc.h:40
dslite_add_pool_address_command
static vlib_cli_command_t dslite_add_pool_address_command
(constructor) VLIB_CLI_COMMAND (dslite_add_pool_address_command)
Definition: dslite_cli.c:295
dslite_show_pool_command_fn
static clib_error_t * dslite_show_pool_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: dslite_cli.c:89
dslite_set_aftr_tunnel_addr_command_fn
static clib_error_t * dslite_set_aftr_tunnel_addr_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: dslite_cli.c:108
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
end_addr
vl_api_address_t end_addr
Definition: ikev2_types.api:38
format_dslite_b4
static u8 * format_dslite_b4(u8 *s, va_list *args)
Definition: dslite_cli.c:228
app_rx_mq_elt_::next
struct app_rx_mq_elt_ * next
Definition: application.h:100
dslite_set_aftr_ip6_addr
int dslite_set_aftr_ip6_addr(dslite_main_t *dm, ip6_address_t *addr)
Definition: dslite.c:141
dslite.h
dslite_main_t
Definition: dslite.h:103
dslite_main_t::per_thread_data
dslite_per_thread_data_t * per_thread_data
Definition: dslite.h:109
format_ip4_address
format_function_t format_ip4_address
Definition: format.h:73
dslite_main_t::pool
nat_ip4_pool_t pool
Definition: dslite.h:115
unformat_line_input
unformat_function_t unformat_line_input
Definition: format.h:275
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
format_dslite_session
static u8 * format_dslite_session(u8 *s, va_list *args)
Definition: dslite_cli.c:209
dslite_per_thread_data_t::sessions
dslite_session_t * sessions
Definition: dslite.h:97
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
dslite_main_t::aftr_ip6_addr
ip6_address_t aftr_ip6_addr
Definition: dslite.h:105
dslite_set_aftr_tunnel_addr
static vlib_cli_command_t dslite_set_aftr_tunnel_addr
(constructor) VLIB_CLI_COMMAND (dslite_set_aftr_tunnel_addr)
Definition: dslite_cli.c:328
dslite_set_b4_tunnel_addr
static vlib_cli_command_t dslite_set_b4_tunnel_addr
(constructor) VLIB_CLI_COMMAND (dslite_set_b4_tunnel_addr)
Definition: dslite_cli.c:356
unformat_input_t
struct _unformat_input_t unformat_input_t
start_addr
vl_api_address_t start_addr
Definition: ikev2_types.api:37
error
Definition: cJSON.c:88
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
count
u8 count
Definition: dhcp.api:208
dslite_b4_t
Definition: dslite.h:77
unformat_free
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
dslite_set_b4_tunnel_addr_command_fn
static clib_error_t * dslite_set_b4_tunnel_addr_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: dslite_cli.c:159
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
dslite_show_b4_ip6_addr_command_fn
static clib_error_t * dslite_show_b4_ip6_addr_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: dslite_cli.c:198
dlist_elt_t::next
u32 next
Definition: dlist.h:30
format_unformat_error
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
dlist_elt_t
Definition: dlist.h:28
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
ip4_address_t
Definition: ip4_packet.h:50
dslite_b4_t::nsessions
u32 nsessions
Definition: dslite.h:81
dslite_show_aftr_ip6_addr
static vlib_cli_command_t dslite_show_aftr_ip6_addr
(constructor) VLIB_CLI_COMMAND (dslite_show_aftr_ip6_addr)
Definition: dslite_cli.c:342
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
dslite_show_sessions_command_fn
static clib_error_t * dslite_show_sessions_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: dslite_cli.c:262
dslite_show_sessions
static vlib_cli_command_t dslite_show_sessions
(constructor) VLIB_CLI_COMMAND (dslite_show_sessions)
Definition: dslite_cli.c:391
dslite_per_thread_data_t::list_pool
dlist_elt_t * list_pool
Definition: dslite.h:100
nat_ip4_pool_s::pool_addr
nat_ip4_pool_addr_t * pool_addr
Definition: alloc.h:64
format_nat_protocol
format_function_t format_nat_protocol
Definition: nat64.h:506
show_dslite_pool_command
static vlib_cli_command_t show_dslite_pool_command
(constructor) VLIB_CLI_COMMAND (show_dslite_pool_command)
Definition: dslite_cli.c:314
format
description fragment has unexpected format
Definition: map.api:433
format_get_indent
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u32
unsigned int u32
Definition: types.h:88
dslite_show_aftr_ip6_addr_command_fn
static clib_error_t * dslite_show_aftr_ip6_addr_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: dslite_cli.c:148
dslite_main_t::b4_ip6_addr
ip6_address_t b4_ip6_addr
Definition: dslite.h:107
dslite_show_b4_ip6_addr
static vlib_cli_command_t dslite_show_b4_ip6_addr
(constructor) VLIB_CLI_COMMAND (dslite_show_b4_ip6_addr)
Definition: dslite_cli.c:370
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
elt
app_rx_mq_elt_t * elt
Definition: application.c:488
dslite_set_b4_ip6_addr
int dslite_set_b4_ip6_addr(dslite_main_t *dm, ip6_address_t *addr)
Definition: dslite.c:187
vlib_main_t
Definition: main.h:102
nat_add_del_ip4_pool_addrs
int nat_add_del_ip4_pool_addrs(nat_ip4_pool_t *pool, ip4_address_t addr, u32 count, u8 is_add, void *opaque)
Definition: alloc.c:76
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
a
a
Definition: bitmap.h:525
dslite_per_thread_data_t
Definition: dslite.h:84
format_ip6_address
format_function_t format_ip6_address
Definition: format.h:91
DSLITE_EXPECTED_ARGUMENT
#define DSLITE_EXPECTED_ARGUMENT
Definition: dslite_cli.c:17
dslite_b4_t::addr
ip6_address_t addr
Definition: dslite.h:79
unformat_ip6_address
unformat_function_t unformat_ip6_address
Definition: format.h:89
rv
int __clib_unused rv
Definition: application.c:491
unformat_ip4_address
unformat_function_t unformat_ip4_address
Definition: format.h:68
dslite_main
dslite_main_t dslite_main
Definition: dslite.c:21
vlib_cli_command_t
Definition: cli.h:92
dslite_per_thread_data_t::b4s
dslite_b4_t * b4s
Definition: dslite.h:94
dslite_add_del_pool_addr_command_fn
static clib_error_t * dslite_add_del_pool_addr_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: dslite_cli.c:20
format_white_space
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137