vlib: prevent some signals from being executed on workers
[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 #include <nat/dslite/dslite.h>
16
17 #define DSLITE_EXPECTED_ARGUMENT "expected required argument(s)"
18
19 static clib_error_t *
20 dslite_add_del_pool_addr_command_fn (vlib_main_t * vm,
21                                      unformat_input_t * input,
22                                      vlib_cli_command_t * cmd)
23 {
24   dslite_main_t *dm = &dslite_main;
25   unformat_input_t _line_input, *line_input = &_line_input;
26   ip4_address_t start_addr, end_addr, this_addr;
27   u32 start_host_order, end_host_order;
28   int count, rv;
29   u8 is_add = 1;
30   clib_error_t *error = 0;
31
32   /* Get a line of input. */
33   if (!unformat_user (input, unformat_line_input, line_input))
34     return clib_error_return (0, DSLITE_EXPECTED_ARGUMENT);
35
36   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
37     {
38       if (unformat (line_input, "%U - %U",
39                     unformat_ip4_address, &start_addr,
40                     unformat_ip4_address, &end_addr))
41         ;
42       else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
43         end_addr = start_addr;
44       else if (unformat (line_input, "del"))
45         is_add = 0;
46       else
47         {
48           error = clib_error_return (0, "unknown input '%U'",
49                                      format_unformat_error, line_input);
50           goto done;
51         }
52     }
53
54   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
55   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
56
57   if (end_host_order < start_host_order)
58     {
59       error = clib_error_return (0, "end address less than start address");
60       goto done;
61     }
62
63   count = (end_host_order - start_host_order) + 1;
64   this_addr = start_addr;
65
66   rv = nat_add_del_ip4_pool_addrs (&dm->pool, this_addr, count, is_add, 0);
67
68   switch (rv)
69     {
70     case VNET_API_ERROR_NO_SUCH_ENTRY:
71       error =
72         clib_error_return (0, "DS-Lite pool address %U not exist.",
73                            format_ip4_address, &this_addr);
74       break;
75     case VNET_API_ERROR_VALUE_EXIST:
76       error =
77         clib_error_return (0, "DS-Lite pool address %U exist.",
78                            format_ip4_address, &this_addr);
79       break;
80     }
81
82 done:
83   unformat_free (line_input);
84
85   return error;
86 }
87
88 static clib_error_t *
89 dslite_show_pool_command_fn (vlib_main_t * vm,
90                              unformat_input_t * input,
91                              vlib_cli_command_t * cmd)
92 {
93   dslite_main_t *dm = &dslite_main;
94   nat_ip4_pool_addr_t *a;
95
96   vlib_cli_output (vm, "DS-Lite pool:");
97
98   vec_foreach (a, dm->pool.pool_addr)
99     {
100       vlib_cli_output (vm, "%U", format_ip4_address, &a->addr);
101     }
102   return 0;
103 }
104
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)
109 {
110   dslite_main_t *dm = &dslite_main;
111   unformat_input_t _line_input, *line_input = &_line_input;
112   ip6_address_t ip6_addr;
113   int rv;
114   clib_error_t *error = 0;
115
116   /* Get a line of input. */
117   if (!unformat_user (input, unformat_line_input, line_input))
118     return clib_error_return (0, DSLITE_EXPECTED_ARGUMENT);
119
120   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
121     {
122       if (unformat (line_input, "%U", unformat_ip6_address, &ip6_addr))
123         ;
124       else
125         {
126           error = clib_error_return (0, "unknown input '%U'",
127                                      format_unformat_error, line_input);
128           goto done;
129         }
130     }
131
132   rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
133
134   if (rv)
135     error =
136       clib_error_return (0,
137                          "Set DS-Lite AFTR tunnel endpoint address failed.");
138
139 done:
140   unformat_free (line_input);
141
142   return error;
143 }
144
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)
149 {
150   dslite_main_t *dm = &dslite_main;
151
152   vlib_cli_output (vm, "%U", format_ip6_address, &dm->aftr_ip6_addr);
153   return 0;
154 }
155
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)
160 {
161   dslite_main_t *dm = &dslite_main;
162   unformat_input_t _line_input, *line_input = &_line_input;
163   ip6_address_t ip6_addr;
164   int rv;
165   clib_error_t *error = 0;
166
167   /* Get a line of input. */
168   if (!unformat_user (input, unformat_line_input, line_input))
169     return clib_error_return (0, DSLITE_EXPECTED_ARGUMENT);
170
171   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
172     {
173       if (unformat (line_input, "%U", unformat_ip6_address, &ip6_addr))
174         ;
175       else
176         {
177           error = clib_error_return (0, "unknown input '%U'",
178                                      format_unformat_error, line_input);
179           goto done;
180         }
181     }
182
183   rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
184
185   if (rv)
186     error =
187       clib_error_return (0, "Set DS-Lite B4 tunnel endpoint address failed.");
188
189 done:
190   unformat_free (line_input);
191
192   return error;
193 }
194
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)
199 {
200   dslite_main_t *dm = &dslite_main;
201
202   vlib_cli_output (vm, "%U", format_ip6_address, &dm->b4_ip6_addr);
203   return 0;
204 }
205
206 static u8 *
207 format_dslite_session (u8 * s, va_list * args)
208 {
209   dslite_session_t *session = va_arg (*args, dslite_session_t *);
210   u32 indent = format_get_indent (s);
211
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);
222   return s;
223 }
224
225 static u8 *
226 format_dslite_b4 (u8 * s, va_list * args)
227 {
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;
232   u32 session_index;
233   dslite_session_t *session;
234
235   s =
236     format (s, "B4 %U %d sessions\n", format_ip6_address, &b4->addr,
237             b4->nsessions);
238
239   if (b4->nsessions == 0)
240     return s;
241
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)
248     {
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;
254     }
255
256   return s;
257 }
258
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)
263 {
264   dslite_main_t *dm = &dslite_main;
265   dslite_per_thread_data_t *td;
266   dslite_b4_t *b4;
267
268   vec_foreach (td, dm->per_thread_data)
269     {
270       pool_foreach (b4, td->b4s)
271        {
272         vlib_cli_output (vm, "%U", format_dslite_b4, td, b4);
273       }
274     }
275
276   return 0;
277 }
278
279
280 /*?
281  * @cliexpar
282  * @cliexstart{dslite add pool address}
283  * Add/delete DS-Lite pool address for AFTR element.
284  * To add DS-Lite pool address use:
285  *  vpp# dslite add pool address 10.1.1.3
286  * To add DS-Lite pool address range use:
287  *  vpp# dslite add pool address 10.1.1.5 - 10.1.1.7
288  * @cliexend
289 ?*/
290 VLIB_CLI_COMMAND (dslite_add_pool_address_command, static) = {
291   .path = "dslite add pool address",
292   .short_help = "dslite add pool address <ip4-range-start> [- <ip4-range-end>] "
293                 " [del]",
294   .function = dslite_add_del_pool_addr_command_fn,
295 };
296
297 /*?
298  * @cliexpar
299  * @cliexstart{show dslite pool}
300  * Show DS-lite pool addresses.
301  * vpp# show dslite pool
302  * DS-Lite pool:
303  * 10.0.0.3
304  * 10.0.0.5
305  * 10.0.0.6
306  * 10.0.0.7
307  * @cliexend
308 ?*/
309 VLIB_CLI_COMMAND (show_dslite_pool_command, static) = {
310   .path = "show dslite pool",
311   .short_help = "show dslite pool",
312   .function = dslite_show_pool_command_fn,
313 };
314
315 /*?
316  * @cliexpar
317  * @cliexstart{dslite set aftr-tunnel-endpoint-address}
318  * Set IPv6 tunnel endpoint address of the AFTR element.
319  * To set AFTR tunnel endpoint address use:
320  * vpp# dslite set aftr-tunnel-endpoint-address 2001:db8:85a3::8a2e:370:1
321  * @cliexend
322 ?*/
323 VLIB_CLI_COMMAND (dslite_set_aftr_tunnel_addr, static) = {
324   .path = "dslite set aftr-tunnel-endpoint-address",
325   .short_help = "dslite set aftr-tunnel-endpoint-address <ip6>",
326   .function = dslite_set_aftr_tunnel_addr_command_fn,
327 };
328
329 /*?
330  * @cliexpar
331  * @cliexstart{show dslite aftr-tunnel-endpoint-address}
332  * Show IPv6 tunnel endpoint address of the AFTR element.
333  * vpp# show dslite aftr-tunnel-endpoint-address
334  * 2001:db8:85a3::8a2e:370:1
335  * @cliexend
336 ?*/
337 VLIB_CLI_COMMAND (dslite_show_aftr_ip6_addr, static) = {
338   .path = "show dslite aftr-tunnel-endpoint-address",
339   .short_help = "show dslite aftr-tunnel-endpoint-address",
340   .function = dslite_show_aftr_ip6_addr_command_fn,
341 };
342
343 /*?
344  * @cliexpar
345  * @cliexstart{dslite set b4-tunnel-endpoint-address}
346  * Set IPv6 tunnel endpoint address of the B4 element.
347  * To set B4 tunnel endpoint address use:
348  * vpp# dslite set b4-tunnel-endpoint-address 2001:db8:62aa::375e:f4c1:1
349  * @cliexend
350 ?*/
351 VLIB_CLI_COMMAND (dslite_set_b4_tunnel_addr, static) = {
352   .path = "dslite set b4-tunnel-endpoint-address",
353   .short_help = "dslite set b4-tunnel-endpoint-address <ip6>",
354   .function = dslite_set_b4_tunnel_addr_command_fn,
355 };
356
357 /*?
358  * @cliexpar
359  * @cliexstart{show dslite b4-tunnel-endpoint-address}
360  * Show IPv6 tunnel endpoint address of the B4 element.
361  * vpp# show dslite b4-tunnel-endpoint-address
362  * 2001:db8:62aa::375e:f4c1:1
363  * @cliexend
364 ?*/
365 VLIB_CLI_COMMAND (dslite_show_b4_ip6_addr, static) = {
366   .path = "show dslite b4-tunnel-endpoint-address",
367   .short_help = "show dslite b4-tunnel-endpoint-address",
368   .function = dslite_show_b4_ip6_addr_command_fn,
369 };
370
371 /*?
372  * @cliexpar
373  * @cliexstart{show dslite sessions}
374  * Show DS-Lite sessions.
375  * vpp# show dslite sessions
376  * B4 fd01:2::2 1 sessions
377  *   in 192.168.1.1:20000 out 10.0.0.3:16253 protocol udp
378  *     total pkts 2, total bytes 136
379  * B4 fd01:2::3 2 sessions
380  *   in 192.168.1.1:20001 out 10.0.0.3:18995 protocol tcp
381  *     total pkts 2, total bytes 160
382  *   in 192.168.1.1:4000 out 10.0.0.3:53893 protocol icmp
383  *     total pkts 2, total bytes 136
384  * @cliexend
385 ?*/
386 VLIB_CLI_COMMAND (dslite_show_sessions, static) = {
387   .path = "show dslite sessions",
388   .short_help = "show dslite sessions",
389   .function = dslite_show_sessions_command_fn,
390 };
391
392
393 /*
394  * fd.io coding-style-patch-verification: ON
395  *
396  * Local Variables:
397  * eval: (c-set-style "gnu")
398  * End:
399  */