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.
15 #include <nat/dslite/dslite.h>
18 dslite_add_del_pool_addr_command_fn (vlib_main_t * vm,
19 unformat_input_t * input,
20 vlib_cli_command_t * cmd)
22 dslite_main_t *dm = &dslite_main;
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;
28 clib_error_t *error = 0;
30 /* Get a line of input. */
31 if (!unformat_user (input, unformat_line_input, line_input))
34 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
36 if (unformat (line_input, "%U - %U",
37 unformat_ip4_address, &start_addr,
38 unformat_ip4_address, &end_addr))
40 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
41 end_addr = start_addr;
42 else if (unformat (line_input, "del"))
46 error = clib_error_return (0, "unknown input '%U'",
47 format_unformat_error, line_input);
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);
55 if (end_host_order < start_host_order)
57 error = clib_error_return (0, "end address less than start address");
61 count = (end_host_order - start_host_order) + 1;
62 this_addr = start_addr;
64 rv = nat_add_del_ip4_pool_addrs (&dm->pool, this_addr, count, is_add, 0);
68 case VNET_API_ERROR_NO_SUCH_ENTRY:
70 clib_error_return (0, "DS-Lite pool address %U not exist.",
71 format_ip4_address, &this_addr);
73 case VNET_API_ERROR_VALUE_EXIST:
75 clib_error_return (0, "DS-Lite pool address %U exist.",
76 format_ip4_address, &this_addr);
81 unformat_free (line_input);
87 dslite_show_pool_command_fn (vlib_main_t * vm,
88 unformat_input_t * input,
89 vlib_cli_command_t * cmd)
91 dslite_main_t *dm = &dslite_main;
92 nat_ip4_pool_addr_t *a;
94 vlib_cli_output (vm, "DS-Lite pool:");
97 vec_foreach (a, dm->pool.pool_addr)
99 vlib_cli_output (vm, "%U", format_ip4_address, &a->addr);
105 static clib_error_t *
106 dslite_set_aftr_tunnel_addr_command_fn (vlib_main_t * vm,
107 unformat_input_t * input,
108 vlib_cli_command_t * cmd)
110 dslite_main_t *dm = &dslite_main;
111 unformat_input_t _line_input, *line_input = &_line_input;
112 ip6_address_t ip6_addr;
114 clib_error_t *error = 0;
116 /* Get a line of input. */
117 if (!unformat_user (input, unformat_line_input, line_input))
120 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
122 if (unformat (line_input, "%U", unformat_ip6_address, &ip6_addr))
126 error = clib_error_return (0, "unknown input '%U'",
127 format_unformat_error, line_input);
132 rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
136 clib_error_return (0,
137 "Set DS-Lite AFTR tunnel endpoint address failed.");
140 unformat_free (line_input);
145 static clib_error_t *
146 dslite_show_aftr_ip6_addr_command_fn (vlib_main_t * vm,
147 unformat_input_t * input,
148 vlib_cli_command_t * cmd)
150 dslite_main_t *dm = &dslite_main;
152 vlib_cli_output (vm, "%U", format_ip6_address, &dm->aftr_ip6_addr);
156 static clib_error_t *
157 dslite_set_b4_tunnel_addr_command_fn (vlib_main_t * vm,
158 unformat_input_t * input,
159 vlib_cli_command_t * cmd)
161 dslite_main_t *dm = &dslite_main;
162 unformat_input_t _line_input, *line_input = &_line_input;
163 ip6_address_t ip6_addr;
165 clib_error_t *error = 0;
167 /* Get a line of input. */
168 if (!unformat_user (input, unformat_line_input, line_input))
171 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
173 if (unformat (line_input, "%U", unformat_ip6_address, &ip6_addr))
177 error = clib_error_return (0, "unknown input '%U'",
178 format_unformat_error, line_input);
183 rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
187 clib_error_return (0, "Set DS-Lite B4 tunnel endpoint address failed.");
190 unformat_free (line_input);
195 static clib_error_t *
196 dslite_show_b4_ip6_addr_command_fn (vlib_main_t * vm,
197 unformat_input_t * input,
198 vlib_cli_command_t * cmd)
200 dslite_main_t *dm = &dslite_main;
202 vlib_cli_output (vm, "%U", format_ip6_address, &dm->b4_ip6_addr);
207 format_dslite_session (u8 * s, va_list * args)
209 dslite_session_t *session = va_arg (*args, dslite_session_t *);
210 u32 indent = format_get_indent (s);
212 s = format (s, "%Uin %U:%u out %U:%u protocol %U\n",
213 format_white_space, indent + 2,
214 format_ip4_address, &session->in2out.addr,
215 clib_net_to_host_u16 (session->in2out.port),
216 format_ip4_address, &session->out2in.addr,
217 clib_net_to_host_u16 (session->out2in.port),
218 format_nat_protocol, session->in2out.proto);
219 s = format (s, "%Utotal pkts %d, total bytes %lld\n",
220 format_white_space, indent + 4,
221 session->total_pkts, session->total_bytes);
226 format_dslite_b4 (u8 * s, va_list * args)
228 dslite_per_thread_data_t *td = va_arg (*args, dslite_per_thread_data_t *);
229 dslite_b4_t *b4 = va_arg (*args, dslite_b4_t *);
230 dlist_elt_t *head, *elt;
231 u32 elt_index, head_index;
233 dslite_session_t *session;
236 format (s, "B4 %U %d sessions\n", format_ip6_address, &b4->addr,
239 if (b4->nsessions == 0)
242 head_index = b4->sessions_per_b4_list_head_index;
243 head = pool_elt_at_index (td->list_pool, head_index);
244 elt_index = head->next;
245 elt = pool_elt_at_index (td->list_pool, elt_index);
246 session_index = elt->value;
247 while (session_index != ~0)
249 session = pool_elt_at_index (td->sessions, session_index);
250 s = format (s, "%U", format_dslite_session, session);
251 elt_index = elt->next;
252 elt = pool_elt_at_index (td->list_pool, elt_index);
253 session_index = elt->value;
259 static clib_error_t *
260 dslite_show_sessions_command_fn (vlib_main_t * vm,
261 unformat_input_t * input,
262 vlib_cli_command_t * cmd)
264 dslite_main_t *dm = &dslite_main;
265 dslite_per_thread_data_t *td;
269 vec_foreach (td, dm->per_thread_data)
271 pool_foreach (b4, td->b4s)
273 vlib_cli_output (vm, "%U", format_dslite_b4, td, b4);
285 * @cliexstart{dslite add pool address}
286 * Add/delete DS-Lite pool address for AFTR element.
287 * To add DS-Lite pool address use:
288 * vpp# dslite add pool address 10.1.1.3
289 * To add DS-Lite pool address range use:
290 * vpp# dslite add pool address 10.1.1.5 - 10.1.1.7
293 VLIB_CLI_COMMAND (dslite_add_pool_address_command, static) = {
294 .path = "dslite add pool address",
295 .short_help = "dslite add pool address <ip4-range-start> [- <ip4-range-end>] "
297 .function = dslite_add_del_pool_addr_command_fn,
302 * @cliexstart{show dslite pool}
303 * Show DS-lite pool addresses.
304 * vpp# show dslite pool
312 VLIB_CLI_COMMAND (show_dslite_pool_command, static) = {
313 .path = "show dslite pool",
314 .short_help = "show dslite pool",
315 .function = dslite_show_pool_command_fn,
320 * @cliexstart{dslite set aftr-tunnel-endpoint-address}
321 * Set IPv6 tunnel endpoint address of the AFTR element.
322 * To set AFTR tunnel endpoint address use:
323 * vpp# dslite set aftr-tunnel-endpoint-address 2001:db8:85a3::8a2e:370:1
326 VLIB_CLI_COMMAND (dslite_set_aftr_tunnel_addr, static) = {
327 .path = "dslite set aftr-tunnel-endpoint-address",
328 .short_help = "dslite set aftr-tunnel-endpoint-address <ip6>",
329 .function = dslite_set_aftr_tunnel_addr_command_fn,
334 * @cliexstart{show dslite aftr-tunnel-endpoint-address}
335 * Show IPv6 tunnel endpoint address of the AFTR element.
336 * vpp# show dslite aftr-tunnel-endpoint-address
337 * 2001:db8:85a3::8a2e:370:1
340 VLIB_CLI_COMMAND (dslite_show_aftr_ip6_addr, static) = {
341 .path = "show dslite aftr-tunnel-endpoint-address",
342 .short_help = "show dslite aftr-tunnel-endpoint-address",
343 .function = dslite_show_aftr_ip6_addr_command_fn,
348 * @cliexstart{dslite set b4-tunnel-endpoint-address}
349 * Set IPv6 tunnel endpoint address of the B4 element.
350 * To set B4 tunnel endpoint address use:
351 * vpp# dslite set b4-tunnel-endpoint-address 2001:db8:62aa::375e:f4c1:1
354 VLIB_CLI_COMMAND (dslite_set_b4_tunnel_addr, static) = {
355 .path = "dslite set b4-tunnel-endpoint-address",
356 .short_help = "dslite set b4-tunnel-endpoint-address <ip6>",
357 .function = dslite_set_b4_tunnel_addr_command_fn,
362 * @cliexstart{show dslite b4-tunnel-endpoint-address}
363 * Show IPv6 tunnel endpoint address of the B4 element.
364 * vpp# show dslite b4-tunnel-endpoint-address
365 * 2001:db8:62aa::375e:f4c1:1
368 VLIB_CLI_COMMAND (dslite_show_b4_ip6_addr, static) = {
369 .path = "show dslite b4-tunnel-endpoint-address",
370 .short_help = "show dslite b4-tunnel-endpoint-address",
371 .function = dslite_show_b4_ip6_addr_command_fn,
376 * @cliexstart{show dslite sessions}
377 * Show DS-Lite sessions.
378 * vpp# show dslite sessions
379 * B4 fd01:2::2 1 sessions
380 * in 192.168.1.1:20000 out 10.0.0.3:16253 protocol udp
381 * total pkts 2, total bytes 136
382 * B4 fd01:2::3 2 sessions
383 * in 192.168.1.1:20001 out 10.0.0.3:18995 protocol tcp
384 * total pkts 2, total bytes 160
385 * in 192.168.1.1:4000 out 10.0.0.3:53893 protocol icmp
386 * total pkts 2, total bytes 136
389 VLIB_CLI_COMMAND (dslite_show_sessions, static) = {
390 .path = "show dslite sessions",
391 .short_help = "show dslite sessions",
392 .function = dslite_show_sessions_command_fn,
398 * fd.io coding-style-patch-verification: ON
401 * eval: (c-set-style "gnu")