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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <nat/dslite/dslite.h>
19 dslite_add_del_pool_addr_command_fn (vlib_main_t * vm,
20 unformat_input_t * input,
21 vlib_cli_command_t * cmd)
23 dslite_main_t *dm = &dslite_main;
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;
29 clib_error_t *error = 0;
31 /* Get a line of input. */
32 if (!unformat_user (input, unformat_line_input, line_input))
35 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
37 if (unformat (line_input, "%U - %U",
38 unformat_ip4_address, &start_addr,
39 unformat_ip4_address, &end_addr))
41 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
42 end_addr = start_addr;
43 else if (unformat (line_input, "del"))
47 error = clib_error_return (0, "unknown input '%U'",
48 format_unformat_error, line_input);
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);
56 if (end_host_order < start_host_order)
58 error = clib_error_return (0, "end address less than start address");
62 count = (end_host_order - start_host_order) + 1;
63 this_addr = start_addr;
65 rv = nat_add_del_ip4_pool_addrs (&dm->pool, this_addr, count, is_add, 0);
69 case VNET_API_ERROR_NO_SUCH_ENTRY:
71 clib_error_return (0, "DS-Lite pool address %U not exist.",
72 format_ip4_address, &this_addr);
74 case VNET_API_ERROR_VALUE_EXIST:
76 clib_error_return (0, "DS-Lite pool address %U exist.",
77 format_ip4_address, &this_addr);
82 unformat_free (line_input);
88 dslite_show_pool_command_fn (vlib_main_t * vm,
89 unformat_input_t * input,
90 vlib_cli_command_t * cmd)
92 dslite_main_t *dm = &dslite_main;
93 nat_ip4_pool_addr_t *a;
95 vlib_cli_output (vm, "DS-Lite pool:");
98 vec_foreach (a, dm->pool.pool_addr)
100 vlib_cli_output (vm, "%U", format_ip4_address, &a->addr);
106 static clib_error_t *
107 dslite_set_aftr_tunnel_addr_command_fn (vlib_main_t * vm,
108 unformat_input_t * input,
109 vlib_cli_command_t * cmd)
111 dslite_main_t *dm = &dslite_main;
112 unformat_input_t _line_input, *line_input = &_line_input;
113 ip6_address_t ip6_addr;
115 clib_error_t *error = 0;
117 /* Get a line of input. */
118 if (!unformat_user (input, unformat_line_input, line_input))
121 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
123 if (unformat (line_input, "%U", unformat_ip6_address, &ip6_addr))
127 error = clib_error_return (0, "unknown input '%U'",
128 format_unformat_error, line_input);
133 rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
137 clib_error_return (0,
138 "Set DS-Lite AFTR tunnel endpoint address failed.");
141 unformat_free (line_input);
146 static clib_error_t *
147 dslite_show_aftr_ip6_addr_command_fn (vlib_main_t * vm,
148 unformat_input_t * input,
149 vlib_cli_command_t * cmd)
151 dslite_main_t *dm = &dslite_main;
153 vlib_cli_output (vm, "%U", format_ip6_address, &dm->aftr_ip6_addr);
157 static clib_error_t *
158 dslite_set_b4_tunnel_addr_command_fn (vlib_main_t * vm,
159 unformat_input_t * input,
160 vlib_cli_command_t * cmd)
162 dslite_main_t *dm = &dslite_main;
163 unformat_input_t _line_input, *line_input = &_line_input;
164 ip6_address_t ip6_addr;
166 clib_error_t *error = 0;
168 /* Get a line of input. */
169 if (!unformat_user (input, unformat_line_input, line_input))
172 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
174 if (unformat (line_input, "%U", unformat_ip6_address, &ip6_addr))
178 error = clib_error_return (0, "unknown input '%U'",
179 format_unformat_error, line_input);
184 rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
188 clib_error_return (0, "Set DS-Lite B4 tunnel endpoint address failed.");
191 unformat_free (line_input);
196 static clib_error_t *
197 dslite_show_b4_ip6_addr_command_fn (vlib_main_t * vm,
198 unformat_input_t * input,
199 vlib_cli_command_t * cmd)
201 dslite_main_t *dm = &dslite_main;
203 vlib_cli_output (vm, "%U", format_ip6_address, &dm->b4_ip6_addr);
208 format_dslite_session (u8 * s, va_list * args)
210 dslite_session_t *session = va_arg (*args, dslite_session_t *);
211 u32 indent = format_get_indent (s);
213 s = format (s, "%Uin %U:%u out %U:%u protocol %U\n",
214 format_white_space, indent + 2,
215 format_ip4_address, &session->in2out.addr,
216 clib_net_to_host_u16 (session->in2out.port),
217 format_ip4_address, &session->out2in.addr,
218 clib_net_to_host_u16 (session->out2in.port),
219 format_snat_protocol, session->in2out.proto);
220 s = format (s, "%Utotal pkts %d, total bytes %lld\n",
221 format_white_space, indent + 4,
222 session->total_pkts, session->total_bytes);
227 format_dslite_b4 (u8 * s, va_list * args)
229 dslite_per_thread_data_t *td = va_arg (*args, dslite_per_thread_data_t *);
230 dslite_b4_t *b4 = va_arg (*args, dslite_b4_t *);
231 dlist_elt_t *head, *elt;
232 u32 elt_index, head_index;
234 dslite_session_t *session;
237 format (s, "B4 %U %d sessions\n", format_ip6_address, &b4->addr,
240 if (b4->nsessions == 0)
243 head_index = b4->sessions_per_b4_list_head_index;
244 head = pool_elt_at_index (td->list_pool, head_index);
245 elt_index = head->next;
246 elt = pool_elt_at_index (td->list_pool, elt_index);
247 session_index = elt->value;
248 while (session_index != ~0)
250 session = pool_elt_at_index (td->sessions, session_index);
251 s = format (s, "%U", format_dslite_session, session);
252 elt_index = elt->next;
253 elt = pool_elt_at_index (td->list_pool, elt_index);
254 session_index = elt->value;
260 static clib_error_t *
261 dslite_show_sessions_command_fn (vlib_main_t * vm,
262 unformat_input_t * input,
263 vlib_cli_command_t * cmd)
265 dslite_main_t *dm = &dslite_main;
266 dslite_per_thread_data_t *td;
270 vec_foreach (td, dm->per_thread_data)
272 pool_foreach (b4, td->b4s,
274 vlib_cli_output (vm, "%U", format_dslite_b4, td, b4);
286 * @cliexstart{dslite add pool address}
287 * Add/delete DS-Lite pool address for AFTR element.
288 * To add DS-Lite pool address use:
289 * vpp# dslite add pool address 10.1.1.3
290 * To add DS-Lite pool address range use:
291 * vpp# dslite add pool address 10.1.1.5 - 10.1.1.7
294 VLIB_CLI_COMMAND (dslite_add_pool_address_command, static) = {
295 .path = "dslite add pool address",
296 .short_help = "dslite add pool address <ip4-range-start> [- <ip4-range-end>] "
298 .function = dslite_add_del_pool_addr_command_fn,
303 * @cliexstart{show dslite pool}
304 * Show DS-lite pool addresses.
305 * vpp# show dslite pool
313 VLIB_CLI_COMMAND (show_dslite_pool_command, static) = {
314 .path = "show dslite pool",
315 .short_help = "show dslite pool",
316 .function = dslite_show_pool_command_fn,
321 * @cliexstart{dslite set aftr-tunnel-endpoint-address}
322 * Set IPv6 tunnel endpoint address of the AFTR element.
323 * To set AFTR tunnel endpoint address use:
324 * vpp# dslite set aftr-tunnel-endpoint-address 2001:db8:85a3::8a2e:370:1
327 VLIB_CLI_COMMAND (dslite_set_aftr_tunnel_addr, static) = {
328 .path = "dslite set aftr-tunnel-endpoint-address",
329 .short_help = "dslite set aftr-tunnel-endpoint-address <ip6>",
330 .function = dslite_set_aftr_tunnel_addr_command_fn,
335 * @cliexstart{show dslite aftr-tunnel-endpoint-address}
336 * Show IPv6 tunnel endpoint address of the AFTR element.
337 * vpp# show dslite aftr-tunnel-endpoint-address
338 * 2001:db8:85a3::8a2e:370:1
341 VLIB_CLI_COMMAND (dslite_show_aftr_ip6_addr, static) = {
342 .path = "show dslite aftr-tunnel-endpoint-address",
343 .short_help = "show dslite aftr-tunnel-endpoint-address",
344 .function = dslite_show_aftr_ip6_addr_command_fn,
349 * @cliexstart{dslite set b4-tunnel-endpoint-address}
350 * Set IPv6 tunnel endpoint address of the B4 element.
351 * To set B4 tunnel endpoint address use:
352 * vpp# dslite set b4-tunnel-endpoint-address 2001:db8:62aa::375e:f4c1:1
355 VLIB_CLI_COMMAND (dslite_set_b4_tunnel_addr, static) = {
356 .path = "dslite set b4-tunnel-endpoint-address",
357 .short_help = "dslite set b4-tunnel-endpoint-address <ip6>",
358 .function = dslite_set_b4_tunnel_addr_command_fn,
363 * @cliexstart{show dslite b4-tunnel-endpoint-address}
364 * Show IPv6 tunnel endpoint address of the B4 element.
365 * vpp# show dslite b4-tunnel-endpoint-address
366 * 2001:db8:62aa::375e:f4c1:1
369 VLIB_CLI_COMMAND (dslite_show_b4_ip6_addr, static) = {
370 .path = "show dslite b4-tunnel-endpoint-address",
371 .short_help = "show dslite b4-tunnel-endpoint-address",
372 .function = dslite_show_b4_ip6_addr_command_fn,
377 * @cliexstart{show dslite sessions}
378 * Show DS-Lite sessions.
379 * vpp# show dslite sessions
380 * B4 fd01:2::2 1 sessions
381 * in 192.168.1.1:20000 out 10.0.0.3:16253 protocol udp
382 * total pkts 2, total bytes 136
383 * B4 fd01:2::3 2 sessions
384 * in 192.168.1.1:20001 out 10.0.0.3:18995 protocol tcp
385 * total pkts 2, total bytes 160
386 * in 192.168.1.1:4000 out 10.0.0.3:53893 protocol icmp
387 * total pkts 2, total bytes 136
390 VLIB_CLI_COMMAND (dslite_show_sessions, static) = {
391 .path = "show dslite sessions",
392 .short_help = "show dslite sessions",
393 .function = dslite_show_sessions_command_fn,
399 * fd.io coding-style-patch-verification: ON
402 * eval: (c-set-style "gnu")