NAT: add missing CLI and API documentation (VPP-1142)
[vpp.git] / src / plugins / nat / 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.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 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 *
95 dslite_show_pool_command_fn (vlib_main_t * vm,
96                              unformat_input_t * input,
97                              vlib_cli_command_t * cmd)
98 {
99   dslite_main_t *dm = &dslite_main;
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 *
114 dslite_set_aftr_tunnel_addr_command_fn (vlib_main_t * vm,
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 =
144       clib_error_return (0,
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 *
154 dslite_show_aftr_ip6_addr_command_fn (vlib_main_t * vm,
155                                       unformat_input_t * input,
156                                       vlib_cli_command_t * cmd)
157 {
158   dslite_main_t *dm = &dslite_main;
159
160   vlib_cli_output (vm, "%U", format_ip6_address, &dm->aftr_ip6_addr);
161   return 0;
162 }
163
164 static clib_error_t *
165 dslite_set_b4_tunnel_addr_command_fn (vlib_main_t * vm,
166                                       unformat_input_t * input,
167                                       vlib_cli_command_t * cmd)
168 {
169   dslite_main_t *dm = &dslite_main;
170   unformat_input_t _line_input, *line_input = &_line_input;
171   ip6_address_t ip6_addr;
172   int rv;
173   clib_error_t *error = 0;
174
175   /* Get a line of input. */
176   if (!unformat_user (input, unformat_line_input, line_input))
177     return 0;
178
179   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
180     {
181       if (unformat (line_input, "%U", unformat_ip6_address, &ip6_addr))
182         ;
183       else
184         {
185           error = clib_error_return (0, "unknown input '%U'",
186                                      format_unformat_error, line_input);
187           goto done;
188         }
189     }
190
191   rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
192
193   if (rv)
194     error =
195       clib_error_return (0, "Set DS-Lite B4 tunnel endpoint address failed.");
196
197 done:
198   unformat_free (line_input);
199
200   return error;
201 }
202
203 static clib_error_t *
204 dslite_show_b4_ip6_addr_command_fn (vlib_main_t * vm,
205                                     unformat_input_t * input,
206                                     vlib_cli_command_t * cmd)
207 {
208   dslite_main_t *dm = &dslite_main;
209
210   vlib_cli_output (vm, "%U", format_ip6_address, &dm->b4_ip6_addr);
211   return 0;
212 }
213
214 static u8 *
215 format_dslite_session (u8 * s, va_list * args)
216 {
217   dslite_session_t *session = va_arg (*args, dslite_session_t *);
218   u32 indent = format_get_indent (s);
219
220   s = format (s, "%Uin %U:%u out %U:%u protocol %U\n",
221               format_white_space, indent + 2,
222               format_ip4_address, &session->in2out.addr,
223               clib_net_to_host_u16 (session->in2out.port),
224               format_ip4_address, &session->out2in.addr,
225               clib_net_to_host_u16 (session->out2in.port),
226               format_snat_protocol, session->in2out.proto);
227   s = format (s, "%Utotal pkts %d, total bytes %lld\n",
228               format_white_space, indent + 4,
229               session->total_pkts, session->total_bytes);
230   return s;
231 }
232
233 static u8 *
234 format_dslite_b4 (u8 * s, va_list * args)
235 {
236   dslite_per_thread_data_t *td = va_arg (*args, dslite_per_thread_data_t *);
237   dslite_b4_t *b4 = va_arg (*args, dslite_b4_t *);
238   dlist_elt_t *head, *elt;
239   u32 elt_index, head_index;
240   u32 session_index;
241   dslite_session_t *session;
242
243   s =
244     format (s, "B4 %U %d sessions\n", format_ip6_address, &b4->addr,
245             b4->nsessions);
246
247   if (b4->nsessions == 0)
248     return s;
249
250   head_index = b4->sessions_per_b4_list_head_index;
251   head = pool_elt_at_index (td->list_pool, head_index);
252   elt_index = head->next;
253   elt = pool_elt_at_index (td->list_pool, elt_index);
254   session_index = elt->value;
255   while (session_index != ~0)
256     {
257       session = pool_elt_at_index (td->sessions, session_index);
258       s = format (s, "%U", format_dslite_session, session);
259       elt_index = elt->next;
260       elt = pool_elt_at_index (td->list_pool, elt_index);
261       session_index = elt->value;
262     }
263
264   return s;
265 }
266
267 static clib_error_t *
268 dslite_show_sessions_command_fn (vlib_main_t * vm,
269                                  unformat_input_t * input,
270                                  vlib_cli_command_t * cmd)
271 {
272   dslite_main_t *dm = &dslite_main;
273   dslite_per_thread_data_t *td;
274   dslite_b4_t *b4;
275
276   /* *INDENT-OFF* */
277   vec_foreach (td, dm->per_thread_data)
278     {
279       pool_foreach (b4, td->b4s,
280       ({
281         vlib_cli_output (vm, "%U", format_dslite_b4, td, b4);
282       }));
283     }
284   /* *INDENT-ON* */
285
286   return 0;
287 }
288
289 /* *INDENT-OFF* */
290
291 /*?
292  * @cliexpar
293  * @cliexstart{dslite add pool address}
294  * Add/delete DS-Lite pool address for AFTR element.
295  * To add DS-Lite pool address use:
296  *  vpp# dslite add pool address 10.1.1.3
297  * To add DS-Lite pool address range use:
298  *  vpp# dslite add pool address 10.1.1.5 - 10.1.1.7
299  * @cliexend
300 ?*/
301 VLIB_CLI_COMMAND (dslite_add_pool_address_command, static) = {
302   .path = "dslite add pool address",
303   .short_help = "dslite add pool address <ip4-range-start> [- <ip4-range-end>] "
304                 " [del]",
305   .function = dslite_add_del_pool_addr_command_fn,
306 };
307
308 /*?
309  * @cliexpar
310  * @cliexstart{show dslite pool}
311  * Show DS-lite pool addresses.
312  * vpp# show dslite pool
313  * DS-Lite pool:
314  * 10.0.0.3
315  * 10.0.0.5
316  * 10.0.0.6
317  * 10.0.0.7
318  * @cliexend
319 ?*/
320 VLIB_CLI_COMMAND (show_dslite_pool_command, static) = {
321   .path = "show dslite pool",
322   .short_help = "show dslite pool",
323   .function = dslite_show_pool_command_fn,
324 };
325
326 /*?
327  * @cliexpar
328  * @cliexstart{dslite set aftr-tunnel-endpoint-address}
329  * Set IPv6 tunnel endpoint address of the AFTR element.
330  * To set AFTR tunnel endpoint address use:
331  * vpp# dslite set aftr-tunnel-endpoint-address 2001:db8:85a3::8a2e:370:1
332  * @cliexend
333 ?*/
334 VLIB_CLI_COMMAND (dslite_set_aftr_tunnel_addr, static) = {
335   .path = "dslite set aftr-tunnel-endpoint-address",
336   .short_help = "dslite set aftr-tunnel-endpoint-address <ip6>",
337   .function = dslite_set_aftr_tunnel_addr_command_fn,
338 };
339
340 /*?
341  * @cliexpar
342  * @cliexstart{show dslite aftr-tunnel-endpoint-address}
343  * Show IPv6 tunnel endpoint address of the AFTR element.
344  * vpp# show dslite aftr-tunnel-endpoint-address
345  * 2001:db8:85a3::8a2e:370:1
346  * @cliexend
347 ?*/
348 VLIB_CLI_COMMAND (dslite_show_aftr_ip6_addr, static) = {
349   .path = "show dslite aftr-tunnel-endpoint-address",
350   .short_help = "show dslite aftr-tunnel-endpoint-address",
351   .function = dslite_show_aftr_ip6_addr_command_fn,
352 };
353
354 /*?
355  * @cliexpar
356  * @cliexstart{dslite set b4-tunnel-endpoint-address}
357  * Set IPv6 tunnel endpoint address of the B4 element.
358  * To set B4 tunnel endpoint address use:
359  * vpp# dslite set b4-tunnel-endpoint-address 2001:db8:62aa::375e:f4c1:1
360  * @cliexend
361 ?*/
362 VLIB_CLI_COMMAND (dslite_set_b4_tunnel_addr, static) = {
363   .path = "dslite set b4-tunnel-endpoint-address",
364   .short_help = "dslite set b4-tunnel-endpoint-address <ip6>",
365   .function = dslite_set_b4_tunnel_addr_command_fn,
366 };
367
368 /*?
369  * @cliexpar
370  * @cliexstart{show dslite b4-tunnel-endpoint-address}
371  * Show IPv6 tunnel endpoint address of the B4 element.
372  * vpp# show dslite b4-tunnel-endpoint-address
373  * 2001:db8:62aa::375e:f4c1:1
374  * @cliexend
375 ?*/
376 VLIB_CLI_COMMAND (dslite_show_b4_ip6_addr, static) = {
377   .path = "show dslite b4-tunnel-endpoint-address",
378   .short_help = "show dslite b4-tunnel-endpoint-address",
379   .function = dslite_show_b4_ip6_addr_command_fn,
380 };
381
382 /*?
383  * @cliexpar
384  * @cliexstart{show dslite sessions}
385  * Show DS-Lite sessions.
386  * vpp# show dslite sessions
387  * B4 fd01:2::2 1 sessions
388  *   in 192.168.1.1:20000 out 10.0.0.3:16253 protocol udp
389  *     total pkts 2, total bytes 136
390  * B4 fd01:2::3 2 sessions
391  *   in 192.168.1.1:20001 out 10.0.0.3:18995 protocol tcp
392  *     total pkts 2, total bytes 160
393  *   in 192.168.1.1:4000 out 10.0.0.3:53893 protocol icmp
394  *     total pkts 2, total bytes 136
395  * @cliexend
396 ?*/
397 VLIB_CLI_COMMAND (dslite_show_sessions, static) = {
398   .path = "show dslite sessions",
399   .short_help = "show dslite sessions",
400   .function = dslite_show_sessions_command_fn,
401 };
402
403 /* *INDENT-ON* */
404
405 /*
406  * fd.io coding-style-patch-verification: ON
407  *
408  * Local Variables:
409  * eval: (c-set-style "gnu")
410  * End:
411  */