d5c0ca6498b779f5c57d1c9ea8b0d8f8e3c45e26
[vpp.git] / src / plugins / nat / dslite / dslite_cli.c
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/dslite.h>
17
18 static clib_error_t *
19 dslite_add_del_pool_addr_command_fn (vlib_main_t * vm,
20                                      unformat_input_t * input,
21                                      vlib_cli_command_t * cmd)
22 {
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;
27   int 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   rv = nat_add_del_ip4_pool_addrs (&dm->pool, this_addr, count, is_add, 0);
66
67   switch (rv)
68     {
69     case VNET_API_ERROR_NO_SUCH_ENTRY:
70       error =
71         clib_error_return (0, "DS-Lite pool address %U not exist.",
72                            format_ip4_address, &this_addr);
73       break;
74     case VNET_API_ERROR_VALUE_EXIST:
75       error =
76         clib_error_return (0, "DS-Lite pool address %U exist.",
77                            format_ip4_address, &this_addr);
78       break;
79     }
80
81 done:
82   unformat_free (line_input);
83
84   return error;
85 }
86
87 static clib_error_t *
88 dslite_show_pool_command_fn (vlib_main_t * vm,
89                              unformat_input_t * input,
90                              vlib_cli_command_t * cmd)
91 {
92   dslite_main_t *dm = &dslite_main;
93   nat_ip4_pool_addr_t *a;
94
95   vlib_cli_output (vm, "DS-Lite pool:");
96
97   /* *INDENT-OFF* */
98   vec_foreach (a, dm->pool.pool_addr)
99     {
100       vlib_cli_output (vm, "%U", format_ip4_address, &a->addr);
101     }
102   /* *INDENT-ON* */
103   return 0;
104 }
105
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)
110 {
111   dslite_main_t *dm = &dslite_main;
112   unformat_input_t _line_input, *line_input = &_line_input;
113   ip6_address_t ip6_addr;
114   int rv;
115   clib_error_t *error = 0;
116
117   /* Get a line of input. */
118   if (!unformat_user (input, unformat_line_input, line_input))
119     return 0;
120
121   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
122     {
123       if (unformat (line_input, "%U", unformat_ip6_address, &ip6_addr))
124         ;
125       else
126         {
127           error = clib_error_return (0, "unknown input '%U'",
128                                      format_unformat_error, line_input);
129           goto done;
130         }
131     }
132
133   rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
134
135   if (rv)
136     error =
137       clib_error_return (0,
138                          "Set DS-Lite AFTR tunnel endpoint address failed.");
139
140 done:
141   unformat_free (line_input);
142
143   return error;
144 }
145
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)
150 {
151   dslite_main_t *dm = &dslite_main;
152
153   vlib_cli_output (vm, "%U", format_ip6_address, &dm->aftr_ip6_addr);
154   return 0;
155 }
156
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)
161 {
162   dslite_main_t *dm = &dslite_main;
163   unformat_input_t _line_input, *line_input = &_line_input;
164   ip6_address_t ip6_addr;
165   int rv;
166   clib_error_t *error = 0;
167
168   /* Get a line of input. */
169   if (!unformat_user (input, unformat_line_input, line_input))
170     return 0;
171
172   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
173     {
174       if (unformat (line_input, "%U", unformat_ip6_address, &ip6_addr))
175         ;
176       else
177         {
178           error = clib_error_return (0, "unknown input '%U'",
179                                      format_unformat_error, line_input);
180           goto done;
181         }
182     }
183
184   rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
185
186   if (rv)
187     error =
188       clib_error_return (0, "Set DS-Lite B4 tunnel endpoint address failed.");
189
190 done:
191   unformat_free (line_input);
192
193   return error;
194 }
195
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)
200 {
201   dslite_main_t *dm = &dslite_main;
202
203   vlib_cli_output (vm, "%U", format_ip6_address, &dm->b4_ip6_addr);
204   return 0;
205 }
206
207 static u8 *
208 format_dslite_session (u8 * s, va_list * args)
209 {
210   dslite_session_t *session = va_arg (*args, dslite_session_t *);
211   u32 indent = format_get_indent (s);
212
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);
223   return s;
224 }
225
226 static u8 *
227 format_dslite_b4 (u8 * s, va_list * args)
228 {
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;
233   u32 session_index;
234   dslite_session_t *session;
235
236   s =
237     format (s, "B4 %U %d sessions\n", format_ip6_address, &b4->addr,
238             b4->nsessions);
239
240   if (b4->nsessions == 0)
241     return s;
242
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)
249     {
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;
255     }
256
257   return s;
258 }
259
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)
264 {
265   dslite_main_t *dm = &dslite_main;
266   dslite_per_thread_data_t *td;
267   dslite_b4_t *b4;
268
269   /* *INDENT-OFF* */
270   vec_foreach (td, dm->per_thread_data)
271     {
272       pool_foreach (b4, td->b4s,
273       ({
274         vlib_cli_output (vm, "%U", format_dslite_b4, td, b4);
275       }));
276     }
277   /* *INDENT-ON* */
278
279   return 0;
280 }
281
282 /* *INDENT-OFF* */
283
284 /*?
285  * @cliexpar
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
292  * @cliexend
293 ?*/
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>] "
297                 " [del]",
298   .function = dslite_add_del_pool_addr_command_fn,
299 };
300
301 /*?
302  * @cliexpar
303  * @cliexstart{show dslite pool}
304  * Show DS-lite pool addresses.
305  * vpp# show dslite pool
306  * DS-Lite pool:
307  * 10.0.0.3
308  * 10.0.0.5
309  * 10.0.0.6
310  * 10.0.0.7
311  * @cliexend
312 ?*/
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,
317 };
318
319 /*?
320  * @cliexpar
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
325  * @cliexend
326 ?*/
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,
331 };
332
333 /*?
334  * @cliexpar
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
339  * @cliexend
340 ?*/
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,
345 };
346
347 /*?
348  * @cliexpar
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
353  * @cliexend
354 ?*/
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,
359 };
360
361 /*?
362  * @cliexpar
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
367  * @cliexend
368 ?*/
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,
373 };
374
375 /*?
376  * @cliexpar
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
388  * @cliexend
389 ?*/
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,
394 };
395
396 /* *INDENT-ON* */
397
398 /*
399  * fd.io coding-style-patch-verification: ON
400  *
401  * Local Variables:
402  * eval: (c-set-style "gnu")
403  * End:
404  */