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