FD.io VPP  v18.01.2-1-g9b554f3
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 
16 #include <nat/dslite.h>
17 
18 static clib_error_t *
20  unformat_input_t * input,
21  vlib_cli_command_t * cmd)
22 {
24  unformat_input_t _line_input, *line_input = &_line_input;
25  ip4_address_t start_addr, end_addr, this_addr;
26  u32 start_host_order, end_host_order;
27  int i, count, rv;
28  u8 is_add = 1;
29  clib_error_t *error = 0;
30 
31  /* Get a line of input. */
32  if (!unformat_user (input, unformat_line_input, line_input))
33  return 0;
34 
35  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
36  {
37  if (unformat (line_input, "%U - %U",
38  unformat_ip4_address, &start_addr,
39  unformat_ip4_address, &end_addr))
40  ;
41  else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
42  end_addr = start_addr;
43  else if (unformat (line_input, "del"))
44  is_add = 0;
45  else
46  {
47  error = clib_error_return (0, "unknown input '%U'",
48  format_unformat_error, line_input);
49  goto done;
50  }
51  }
52 
53  start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
54  end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
55 
56  if (end_host_order < start_host_order)
57  {
58  error = clib_error_return (0, "end address less than start address");
59  goto done;
60  }
61 
62  count = (end_host_order - start_host_order) + 1;
63  this_addr = start_addr;
64 
65  for (i = 0; i < count; i++)
66  {
67  rv = dslite_add_del_pool_addr (dm, &this_addr, is_add);
68 
69  switch (rv)
70  {
71  case VNET_API_ERROR_NO_SUCH_ENTRY:
72  error =
73  clib_error_return (0, "DS-Lite pool address %U not exist.",
74  format_ip4_address, &this_addr);
75  goto done;
76  case VNET_API_ERROR_VALUE_EXIST:
77  error =
78  clib_error_return (0, "DS-Lite pool address %U exist.",
79  format_ip4_address, &this_addr);
80  goto done;
81  default:
82  break;
83 
84  }
85  increment_v4_address (&this_addr);
86  }
87 
88 done:
89  unformat_free (line_input);
90 
91  return error;
92 }
93 
94 static clib_error_t *
96  unformat_input_t * input,
97  vlib_cli_command_t * cmd)
98 {
100  snat_address_t *ap;
101 
102  vlib_cli_output (vm, "DS-Lite pool:");
103 
104  /* *INDENT-OFF* */
105  vec_foreach (ap, dm->addr_pool)
106  {
107  vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
108  }
109  /* *INDENT-ON* */
110  return 0;
111 }
112 
113 static clib_error_t *
115  unformat_input_t * input,
116  vlib_cli_command_t * cmd)
117 {
118  dslite_main_t *dm = &dslite_main;
119  unformat_input_t _line_input, *line_input = &_line_input;
120  ip6_address_t ip6_addr;
121  int rv;
122  clib_error_t *error = 0;
123 
124  /* Get a line of input. */
125  if (!unformat_user (input, unformat_line_input, line_input))
126  return 0;
127 
128  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
129  {
130  if (unformat (line_input, "%U", unformat_ip6_address, &ip6_addr))
131  ;
132  else
133  {
134  error = clib_error_return (0, "unknown input '%U'",
135  format_unformat_error, line_input);
136  goto done;
137  }
138  }
139 
140  rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
141 
142  if (rv)
143  error =
145  "Set DS-Lite AFTR tunnel endpoint address failed.");
146 
147 done:
148  unformat_free (line_input);
149 
150  return error;
151 }
152 
153 static clib_error_t *
155  unformat_input_t * input,
156  vlib_cli_command_t * cmd)
157 {
158  dslite_main_t *dm = &dslite_main;
159 
161  return 0;
162 }
163 
164 static u8 *
165 format_dslite_session (u8 * s, va_list * args)
166 {
167  dslite_session_t *session = va_arg (*args, dslite_session_t *);
168  u32 indent = format_get_indent (s);
169 
170  s = format (s, "%Uin %U:%u out %U:%u protocol %U\n",
171  format_white_space, indent + 2,
172  format_ip4_address, &session->in2out.addr,
173  clib_net_to_host_u16 (session->in2out.port),
174  format_ip4_address, &session->out2in.addr,
175  clib_net_to_host_u16 (session->out2in.port),
176  format_snat_protocol, session->in2out.proto);
177  s = format (s, "%Utotal pkts %d, total bytes %lld\n",
178  format_white_space, indent + 4,
179  session->total_pkts, session->total_bytes);
180  return s;
181 }
182 
183 static u8 *
184 format_dslite_b4 (u8 * s, va_list * args)
185 {
186  dslite_per_thread_data_t *td = va_arg (*args, dslite_per_thread_data_t *);
187  dslite_b4_t *b4 = va_arg (*args, dslite_b4_t *);
188  dlist_elt_t *head, *elt;
189  u32 elt_index, head_index;
190  u32 session_index;
191  dslite_session_t *session;
192 
193  s =
194  format (s, "B4 %U %d sessions\n", format_ip6_address, &b4->addr,
195  b4->nsessions);
196 
197  if (b4->nsessions == 0)
198  return s;
199 
200  head_index = b4->sessions_per_b4_list_head_index;
201  head = pool_elt_at_index (td->list_pool, head_index);
202  elt_index = head->next;
203  elt = pool_elt_at_index (td->list_pool, elt_index);
204  session_index = elt->value;
205  while (session_index != ~0)
206  {
207  session = pool_elt_at_index (td->sessions, session_index);
208  s = format (s, "%U", format_dslite_session, session);
209  elt_index = elt->next;
210  elt = pool_elt_at_index (td->list_pool, elt_index);
211  session_index = elt->value;
212  }
213 
214  return s;
215 }
216 
217 static clib_error_t *
219  unformat_input_t * input,
220  vlib_cli_command_t * cmd)
221 {
222  dslite_main_t *dm = &dslite_main;
224  dslite_b4_t *b4;
225 
226  /* *INDENT-OFF* */
227  vec_foreach (td, dm->per_thread_data)
228  {
229  pool_foreach (b4, td->b4s,
230  ({
231  vlib_cli_output (vm, "%U", format_dslite_b4, td, b4);
232  }));
233  }
234  /* *INDENT-ON* */
235 
236  return 0;
237 }
238 
239 /* *INDENT-OFF* */
240 
241 VLIB_CLI_COMMAND (dslite_add_pool_address_command, static) = {
242  .path = "dslite add pool address",
243  .short_help = "dslite add pool address <ip4-range-start> [- <ip4-range-end>] "
244  " [del]",
246 };
247 
248 VLIB_CLI_COMMAND (show_dslite_pool_command, static) = {
249  .path = "show dslite pool",
250  .short_help = "show dslite pool",
251  .function = dslite_show_pool_command_fn,
252 };
253 
254 VLIB_CLI_COMMAND (dslite_set_aftr_tunnel_addr, static) = {
255  .path = "dslite set aftr-tunnel-endpoint-address",
256  .short_help = "dslite set aftr-tunnel-endpoint-address <ip6>",
258 };
259 
260 VLIB_CLI_COMMAND (dslite_show_aftr_ip6_addr, static) = {
261  .path = "show dslite aftr-tunnel-endpoint-address",
262  .short_help = "show dslite aftr-tunnel-endpoint-address",
264 };
265 
266 VLIB_CLI_COMMAND (dslite_show_sessions, static) = {
267  .path = "show dslite sessions",
268  .short_help = "show dslite sessions",
270 };
271 
272 /* *INDENT-ON* */
273 
274 /*
275  * fd.io coding-style-patch-verification: ON
276  *
277  * Local Variables:
278  * eval: (c-set-style "gnu")
279  * End:
280  */
u32 next
Definition: dlist.h:30
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
u8 * format_snat_protocol(u8 *s, va_list *args)
Definition: nat.c:2266
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static u8 * format_dslite_session(u8 *s, va_list *args)
Definition: dslite_cli.c:165
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:154
format_function_t format_ip4_address
Definition: format.h:79
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:438
unformat_function_t unformat_ip4_address
Definition: format.h:76
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
ip6_address_t addr
Definition: dslite.h:55
#define clib_error_return(e, args...)
Definition: error.h:99
unformat_function_t unformat_line_input
Definition: format.h:281
dlist_elt_t * list_pool
Definition: dslite.h:76
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:459
struct _unformat_input_t unformat_input_t
snat_address_t * addr_pool
Definition: dslite.h:84
int dslite_add_del_pool_addr(dslite_main_t *dm, ip4_address_t *addr, u8 is_add)
Definition: dslite.c:93
unformat_function_t unformat_ip6_address
Definition: format.h:94
ip6_address_t aftr_ip6_addr
Definition: dslite.h:81
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
format_function_t format_ip6_address
Definition: format.h:95
vlib_main_t * vm
Definition: buffer.c:283
dslite_main_t dslite_main
Definition: dslite.c:19
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
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:19
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:218
u32 nsessions
Definition: dslite.h:57
unsigned int u32
Definition: types.h:88
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:95
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:114
size_t count
Definition: vapi.c:42
ip4_address_t addr
Definition: nat.h:177
dslite_b4_t * b4s
Definition: dslite.h:70
u32 value
Definition: dlist.h:32
dslite_per_thread_data_t * per_thread_data
Definition: dslite.h:83
void increment_v4_address(ip4_address_t *a)
Definition: nat.c:599
static u8 * format_dslite_b4(u8 *s, va_list *args)
Definition: dslite_cli.c:184
unsigned char u8
Definition: types.h:56
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
dslite_session_t * sessions
Definition: dslite.h:73
#define vec_foreach(var, vec)
Vector iterator.
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:680
int dslite_set_aftr_ip6_addr(dslite_main_t *dm, ip6_address_t *addr)
Definition: dslite.c:72
u32 sessions_per_b4_list_head_index
Definition: dslite.h:56
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169