NAT64: change not supported multi threading behaviour
[vpp.git] / src / plugins / snat / nat64_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  * @file
17  * @brief NAT64 CLI
18  */
19
20 #include <snat/nat64.h>
21 #include <snat/snat.h>
22 #include <vnet/fib/fib_table.h>
23
24 static clib_error_t *
25 nat64_add_del_pool_addr_command_fn (vlib_main_t * vm,
26                                     unformat_input_t * input,
27                                     vlib_cli_command_t * cmd)
28 {
29   nat64_main_t *nm = &nat64_main;
30   unformat_input_t _line_input, *line_input = &_line_input;
31   ip4_address_t start_addr, end_addr, this_addr;
32   u32 start_host_order, end_host_order;
33   int i, count, rv;
34   u32 vrf_id = ~0;
35   u8 is_add = 1;
36   clib_error_t *error = 0;
37
38   if (nm->is_disabled)
39     return clib_error_return (0,
40                               "NAT64 disabled, multi thread not supported");
41
42   /* Get a line of input. */
43   if (!unformat_user (input, unformat_line_input, line_input))
44     return 0;
45
46   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
47     {
48       if (unformat (line_input, "%U - %U",
49                     unformat_ip4_address, &start_addr,
50                     unformat_ip4_address, &end_addr))
51         ;
52       else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
53         ;
54       else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
55         end_addr = start_addr;
56       else if (unformat (line_input, "del"))
57         is_add = 0;
58       else
59         {
60           error = clib_error_return (0, "unknown input '%U'",
61                                      format_unformat_error, line_input);
62           goto done;
63         }
64     }
65
66   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
67   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
68
69   if (end_host_order < start_host_order)
70     {
71       error = clib_error_return (0, "end address less than start address");
72       goto done;
73     }
74
75   count = (end_host_order - start_host_order) + 1;
76   this_addr = start_addr;
77
78   for (i = 0; i < count; i++)
79     {
80       rv = nat64_add_del_pool_addr (&this_addr, vrf_id, is_add);
81
82       switch (rv)
83         {
84         case VNET_API_ERROR_NO_SUCH_ENTRY:
85           error =
86             clib_error_return (0, "NAT64 pool address %U not exist.",
87                                format_ip4_address, &this_addr);
88           goto done;
89         case VNET_API_ERROR_VALUE_EXIST:
90           error =
91             clib_error_return (0, "NAT64 pool address %U exist.",
92                                format_ip4_address, &this_addr);
93           goto done;
94         default:
95           break;
96
97         }
98       increment_v4_address (&this_addr);
99     }
100
101 done:
102   unformat_free (line_input);
103
104   return error;
105 }
106
107 static int
108 nat64_cli_pool_walk (snat_address_t * ap, void *ctx)
109 {
110   vlib_main_t *vm = ctx;
111
112   if (ap->fib_index != ~0)
113     {
114       fib_table_t *fib;
115       fib = fib_table_get (ap->fib_index, FIB_PROTOCOL_IP6);
116       if (!fib)
117         return -1;
118       vlib_cli_output (vm, " %U tenant VRF: %u", format_ip4_address,
119                        &ap->addr, fib->ft_table_id);
120     }
121   else
122     vlib_cli_output (vm, " %U", format_ip4_address, &ap->addr);
123
124   return 0;
125 }
126
127 static clib_error_t *
128 nat64_show_pool_command_fn (vlib_main_t * vm,
129                             unformat_input_t * input,
130                             vlib_cli_command_t * cmd)
131 {
132   nat64_main_t *nm = &nat64_main;
133
134   if (nm->is_disabled)
135     return clib_error_return (0,
136                               "NAT64 disabled, multi thread not supported");
137
138   vlib_cli_output (vm, "NAT64 pool:");
139   nat64_pool_addr_walk (nat64_cli_pool_walk, vm);
140
141   return 0;
142 }
143
144 static clib_error_t *
145 nat64_interface_feature_command_fn (vlib_main_t * vm,
146                                     unformat_input_t *
147                                     input, vlib_cli_command_t * cmd)
148 {
149   nat64_main_t *nm = &nat64_main;
150   unformat_input_t _line_input, *line_input = &_line_input;
151   vnet_main_t *vnm = vnet_get_main ();
152   clib_error_t *error = 0;
153   u32 sw_if_index;
154   u32 *inside_sw_if_indices = 0;
155   u32 *outside_sw_if_indices = 0;
156   u8 is_add = 1;
157   int i, rv;
158
159   if (nm->is_disabled)
160     return clib_error_return (0,
161                               "NAT64 disabled, multi thread not supported");
162
163   /* Get a line of input. */
164   if (!unformat_user (input, unformat_line_input, line_input))
165     return 0;
166
167   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
168     {
169       if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
170                     vnm, &sw_if_index))
171         vec_add1 (inside_sw_if_indices, sw_if_index);
172       else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
173                          vnm, &sw_if_index))
174         vec_add1 (outside_sw_if_indices, sw_if_index);
175       else if (unformat (line_input, "del"))
176         is_add = 0;
177       else
178         {
179           error = clib_error_return (0, "unknown input '%U'",
180                                      format_unformat_error, line_input);
181           goto done;
182         }
183     }
184
185   if (vec_len (inside_sw_if_indices))
186     {
187       for (i = 0; i < vec_len (inside_sw_if_indices); i++)
188         {
189           sw_if_index = inside_sw_if_indices[i];
190           rv = nat64_add_del_interface (sw_if_index, 1, is_add);
191           switch (rv)
192             {
193             case VNET_API_ERROR_NO_SUCH_ENTRY:
194               error =
195                 clib_error_return (0, "%U NAT64 feature not enabled.",
196                                    format_vnet_sw_interface_name, vnm,
197                                    vnet_get_sw_interface (vnm, sw_if_index));
198               goto done;
199             case VNET_API_ERROR_VALUE_EXIST:
200               error =
201                 clib_error_return (0, "%U NAT64 feature already enabled.",
202                                    format_vnet_sw_interface_name, vnm,
203                                    vnet_get_sw_interface (vnm, sw_if_index));
204               goto done;
205             case VNET_API_ERROR_INVALID_VALUE:
206             case VNET_API_ERROR_INVALID_VALUE_2:
207               error =
208                 clib_error_return (0,
209                                    "%U NAT64 feature enable/disable failed.",
210                                    format_vnet_sw_interface_name, vnm,
211                                    vnet_get_sw_interface (vnm, sw_if_index));
212               goto done;
213             default:
214               break;
215
216             }
217         }
218     }
219
220   if (vec_len (outside_sw_if_indices))
221     {
222       for (i = 0; i < vec_len (outside_sw_if_indices); i++)
223         {
224           sw_if_index = outside_sw_if_indices[i];
225           rv = nat64_add_del_interface (sw_if_index, 0, is_add);
226           switch (rv)
227             {
228             case VNET_API_ERROR_NO_SUCH_ENTRY:
229               error =
230                 clib_error_return (0, "%U NAT64 feature not enabled.",
231                                    format_vnet_sw_interface_name, vnm,
232                                    vnet_get_sw_interface (vnm, sw_if_index));
233               goto done;
234             case VNET_API_ERROR_VALUE_EXIST:
235               error =
236                 clib_error_return (0, "%U NAT64 feature already enabled.",
237                                    format_vnet_sw_interface_name, vnm,
238                                    vnet_get_sw_interface (vnm, sw_if_index));
239               goto done;
240             case VNET_API_ERROR_INVALID_VALUE:
241             case VNET_API_ERROR_INVALID_VALUE_2:
242               error =
243                 clib_error_return (0,
244                                    "%U NAT64 feature enable/disable failed.",
245                                    format_vnet_sw_interface_name, vnm,
246                                    vnet_get_sw_interface (vnm, sw_if_index));
247               goto done;
248             default:
249               break;
250
251             }
252         }
253     }
254
255 done:
256   unformat_free (line_input);
257   vec_free (inside_sw_if_indices);
258   vec_free (outside_sw_if_indices);
259
260   return error;
261 }
262
263 static int
264 nat64_cli_interface_walk (snat_interface_t * i, void *ctx)
265 {
266   vlib_main_t *vm = ctx;
267   vnet_main_t *vnm = vnet_get_main ();
268
269   vlib_cli_output (vm, " %U %s", format_vnet_sw_interface_name, vnm,
270                    vnet_get_sw_interface (vnm, i->sw_if_index),
271                    i->is_inside ? "in" : "out");
272   return 0;
273 }
274
275 static clib_error_t *
276 nat64_show_interfaces_command_fn (vlib_main_t * vm,
277                                   unformat_input_t *
278                                   input, vlib_cli_command_t * cmd)
279 {
280   nat64_main_t *nm = &nat64_main;
281
282   if (nm->is_disabled)
283     return clib_error_return (0,
284                               "NAT64 disabled, multi thread not supported");
285
286   vlib_cli_output (vm, "NAT64 interfaces:");
287   nat64_interfaces_walk (nat64_cli_interface_walk, vm);
288
289   return 0;
290 }
291
292 static clib_error_t *
293 nat64_add_del_static_bib_command_fn (vlib_main_t *
294                                      vm,
295                                      unformat_input_t
296                                      * input, vlib_cli_command_t * cmd)
297 {
298   nat64_main_t *nm = &nat64_main;
299   unformat_input_t _line_input, *line_input = &_line_input;
300   clib_error_t *error = 0;
301   u8 is_add = 1;
302   ip6_address_t in_addr;
303   ip4_address_t out_addr;
304   u16 in_port = 0;
305   u16 out_port = 0;
306   u32 vrf_id = 0;
307   snat_protocol_t proto = 0;
308   u8 p = 0;
309   int rv;
310
311   if (nm->is_disabled)
312     return clib_error_return (0,
313                               "NAT64 disabled, multi thread not supported");
314
315   if (!unformat_user (input, unformat_line_input, line_input))
316     return 0;
317
318   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
319     {
320       if (unformat (line_input, "%U %u", unformat_ip6_address,
321                     &in_addr, &in_port))
322         ;
323       else if (unformat (line_input, "%U %u", unformat_ip4_address,
324                          &out_addr, &out_port))
325         ;
326       else if (unformat (line_input, "vrf %u", &vrf_id))
327         ;
328       else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
329         ;
330       else if (unformat (line_input, "del"))
331         is_add = 0;
332       else
333         {
334           error = clib_error_return (0, "unknown input: '%U'",
335                                      format_unformat_error, line_input);
336           goto done;
337         }
338     }
339
340   if (!in_port)
341     {
342       error = clib_error_return (0, "inside port and address  must be set");
343       goto done;
344     }
345
346   if (!out_port)
347     {
348       error = clib_error_return (0, "outside port and address  must be set");
349       goto done;
350     }
351
352   p = snat_proto_to_ip_proto (proto);
353
354   rv =
355     nat64_add_del_static_bib_entry (&in_addr, &out_addr, in_port, out_port, p,
356                                     vrf_id, is_add);
357
358   switch (rv)
359     {
360     case VNET_API_ERROR_NO_SUCH_ENTRY:
361       error = clib_error_return (0, "NAT64 BIB entry not exist.");
362       goto done;
363     case VNET_API_ERROR_VALUE_EXIST:
364       error = clib_error_return (0, "NAT64 BIB entry exist.");
365       goto done;
366     case VNET_API_ERROR_UNSPECIFIED:
367       error = clib_error_return (0, "Crerate NAT64 BIB entry failed.");
368       goto done;
369     case VNET_API_ERROR_INVALID_VALUE:
370       error =
371         clib_error_return (0, "Outside addres %U and port %u already in use.",
372                            format_ip4_address, &out_addr, out_port);
373       goto done;
374     default:
375       break;
376     }
377
378 done:
379   unformat_free (line_input);
380
381   return error;
382 }
383
384 static int
385 nat64_cli_bib_walk (nat64_db_bib_entry_t * bibe, void *ctx)
386 {
387   vlib_main_t *vm = ctx;
388   fib_table_t *fib;
389
390   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
391   if (!fib)
392     return -1;
393
394   vlib_cli_output (vm, " %U %u %U %u %U vrf %u %s %u sessions",
395                    format_ip6_address, &bibe->in_addr,
396                    clib_net_to_host_u16 (bibe->in_port), format_ip4_address,
397                    &bibe->out_addr, clib_net_to_host_u16 (bibe->out_port),
398                    format_snat_protocol, bibe->proto, fib->ft_table_id,
399                    bibe->is_static ? "static" : "dynamic", bibe->ses_num);
400   return 0;
401 }
402
403 static clib_error_t *
404 nat64_show_bib_command_fn (vlib_main_t * vm,
405                            unformat_input_t * input, vlib_cli_command_t * cmd)
406 {
407   nat64_main_t *nm = &nat64_main;
408   unformat_input_t _line_input, *line_input = &_line_input;
409   clib_error_t *error = 0;
410   snat_protocol_t proto = 0;
411
412   if (nm->is_disabled)
413     return clib_error_return (0,
414                               "NAT64 disabled, multi thread not supported");
415
416   if (!unformat_user (input, unformat_line_input, line_input))
417     return 0;
418
419   if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
420     ;
421   else
422     {
423       error = clib_error_return (0, "unknown input: '%U'",
424                                  format_unformat_error, line_input);
425       goto done;
426     }
427
428   vlib_cli_output (vm, "NAT64 %U BIB:", format_snat_protocol, proto);
429   nat64_db_bib_walk (&nm->db, proto, nat64_cli_bib_walk, vm);
430
431 done:
432   unformat_free (line_input);
433
434   return error;
435 }
436
437 static clib_error_t *
438 nat64_set_timeouts_command_fn (vlib_main_t * vm, unformat_input_t * input,
439                                vlib_cli_command_t * cmd)
440 {
441   nat64_main_t *nm = &nat64_main;
442   unformat_input_t _line_input, *line_input = &_line_input;
443   clib_error_t *error = 0;
444   u32 timeout, tcp_trans, tcp_est, tcp_incoming_syn;
445
446   tcp_trans = nat64_get_tcp_trans_timeout ();
447   tcp_est = nat64_get_tcp_est_timeout ();
448   tcp_incoming_syn = nat64_get_tcp_incoming_syn_timeout ();
449
450   if (nm->is_disabled)
451     return clib_error_return (0,
452                               "NAT64 disabled, multi thread not supported");
453
454   if (!unformat_user (input, unformat_line_input, line_input))
455     return 0;
456
457   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
458     {
459       if (unformat (line_input, "udp %u", &timeout))
460         {
461           if (nat64_set_udp_timeout (timeout))
462             {
463               error = clib_error_return (0, "Invalid UDP timeout value");
464               goto done;
465             }
466         }
467       else if (unformat (line_input, "icmp %u", &timeout))
468         {
469           if (nat64_set_icmp_timeout (timeout))
470             {
471               error = clib_error_return (0, "Invalid ICMP timeout value");
472               goto done;
473             }
474         }
475       else if (unformat (line_input, "tcp-trans %u", &tcp_trans))
476         {
477           if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
478             {
479               error =
480                 clib_error_return (0, "Invalid TCP transitory tiemout value");
481               goto done;
482             }
483         }
484       else if (unformat (line_input, "tcp-est %u", &tcp_est))
485         {
486           if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
487             {
488               error =
489                 clib_error_return (0,
490                                    "Invalid TCP established tiemout value");
491               goto done;
492             }
493         }
494       else
495         if (unformat (line_input, "tcp-incoming-syn %u", &tcp_incoming_syn))
496         {
497           if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
498             {
499               error =
500                 clib_error_return (0,
501                                    "Invalid TCP incoming SYN tiemout value");
502               goto done;
503             }
504         }
505       else if (unformat (line_input, "reset"))
506         {
507           nat64_set_udp_timeout (0);
508           nat64_set_icmp_timeout (0);
509           nat64_set_tcp_timeouts (0, 0, 0);
510         }
511       else
512         {
513           error = clib_error_return (0, "unknown input '%U'",
514                                      format_unformat_error, line_input);
515           goto done;
516         }
517     }
518
519 done:
520   unformat_free (line_input);
521
522   return error;
523 }
524
525 static clib_error_t *
526 nat64_show_timeouts_command_fn (vlib_main_t * vm, unformat_input_t * input,
527                                 vlib_cli_command_t * cmd)
528 {
529   nat64_main_t *nm = &nat64_main;
530
531   if (nm->is_disabled)
532     return clib_error_return (0,
533                               "NAT64 disabled, multi thread not supported");
534
535   vlib_cli_output (vm, "NAT64 session timeouts:");
536   vlib_cli_output (vm, " UDP %usec", nat64_get_udp_timeout ());
537   vlib_cli_output (vm, " ICMP %usec", nat64_get_icmp_timeout ());
538   vlib_cli_output (vm, " TCP transitory %usec",
539                    nat64_get_tcp_trans_timeout ());
540   vlib_cli_output (vm, " TCP established %usec",
541                    nat64_get_tcp_est_timeout ());
542   vlib_cli_output (vm, " TCP incoming SYN %usec",
543                    nat64_get_tcp_incoming_syn_timeout ());
544
545   return 0;
546 }
547
548 static int
549 nat64_cli_st_walk (nat64_db_st_entry_t * ste, void *ctx)
550 {
551   vlib_main_t *vm = ctx;
552   nat64_main_t *nm = &nat64_main;
553   nat64_db_bib_entry_t *bibe;
554   fib_table_t *fib;
555
556   bibe = nat64_db_bib_entry_by_index (&nm->db, ste->proto, ste->bibe_index);
557   if (!bibe)
558     return -1;
559
560   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
561   if (!fib)
562     return -1;
563
564   u32 vrf_id = fib->ft_table_id;
565
566   if (ste->proto == SNAT_PROTOCOL_ICMP)
567     vlib_cli_output (vm, " %U %U %u %U %U %u %U vrf %u",
568                      format_ip6_address, &bibe->in_addr,
569                      format_ip6_address, &ste->in_r_addr,
570                      clib_net_to_host_u16 (bibe->in_port),
571                      format_ip4_address, &bibe->out_addr,
572                      format_ip4_address, &ste->out_r_addr,
573                      clib_net_to_host_u16 (bibe->out_port),
574                      format_snat_protocol, bibe->proto, vrf_id);
575   else
576     vlib_cli_output (vm, " %U %u %U %u %U %u %U %u %U vrf %u",
577                      format_ip6_address, &bibe->in_addr,
578                      clib_net_to_host_u16 (bibe->in_port),
579                      format_ip6_address, &ste->in_r_addr,
580                      clib_net_to_host_u16 (ste->r_port),
581                      format_ip4_address, &bibe->out_addr,
582                      clib_net_to_host_u16 (bibe->out_port),
583                      format_ip4_address, &ste->out_r_addr,
584                      clib_net_to_host_u16 (ste->r_port),
585                      format_snat_protocol, bibe->proto, vrf_id);
586   return 0;
587 }
588
589 static clib_error_t *
590 nat64_show_st_command_fn (vlib_main_t * vm,
591                           unformat_input_t * input, vlib_cli_command_t * cmd)
592 {
593   nat64_main_t *nm = &nat64_main;
594   unformat_input_t _line_input, *line_input = &_line_input;
595   clib_error_t *error = 0;
596   snat_protocol_t proto = 0;
597
598   if (nm->is_disabled)
599     return clib_error_return (0,
600                               "NAT64 disabled, multi thread not supported");
601
602   if (!unformat_user (input, unformat_line_input, line_input))
603     return 0;
604
605   if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
606     ;
607   else
608     {
609       error = clib_error_return (0, "unknown input: '%U'",
610                                  format_unformat_error, line_input);
611       goto done;
612     }
613
614   vlib_cli_output (vm, "NAT64 %U session table:", format_snat_protocol,
615                    proto);
616   nat64_db_st_walk (&nm->db, proto, nat64_cli_st_walk, vm);
617
618 done:
619   unformat_free (line_input);
620
621   return error;
622 }
623
624 /* *INDENT-OFF* */
625
626 VLIB_CLI_COMMAND (nat64_add_pool_address_command, static) = {
627   .path = "nat64 add pool address",
628   .short_help = "nat64 add pool address <ip4-range-start> [- <ip4-range-end>] "
629                 "[tenant-vrf <vrf-id>] [del]",
630   .function = nat64_add_del_pool_addr_command_fn,
631 };
632
633 VLIB_CLI_COMMAND (show_nat64_pool_command, static) = {
634   .path = "show nat64 pool",
635   .short_help = "show nat64 pool",
636   .function = nat64_show_pool_command_fn,
637 };
638
639 VLIB_CLI_COMMAND (set_interface_nat64_command, static) = {
640   .path = "set interface nat64",
641   .short_help = "set interface nat64 in|out <intfc> [del]",
642   .function = nat64_interface_feature_command_fn,
643 };
644
645 VLIB_CLI_COMMAND (show_nat64_interfaces_command, static) = {
646   .path = "show nat64 interfaces",
647   .short_help = "show nat64 interfaces",
648   .function = nat64_show_interfaces_command_fn,
649 };
650
651 VLIB_CLI_COMMAND (nat64_add_del_static_bib_command, static) = {
652   .path = "nat64 add static bib",
653   .short_help = "nat64 add static bib <ip6-addr> <port> <ip4-addr> <port> "
654                 "tcp|udp|icmp [vfr <table-id>] [del]",
655   .function = nat64_add_del_static_bib_command_fn,
656 };
657
658 VLIB_CLI_COMMAND (show_nat64_bib_command, static) = {
659   .path = "show nat64 bib",
660   .short_help = "show nat64 bib tcp|udp|icmp",
661   .function = nat64_show_bib_command_fn,
662 };
663
664 VLIB_CLI_COMMAND (set_nat64_timeouts_command, static) = {
665   .path = "set nat64 timeouts",
666   .short_help = "set nat64 timeouts udp <sec> icmp <sec> tcp-trans <sec> "
667                 "tcp-est <sec> tcp-incoming-syn <sec> | reset",
668   .function = nat64_set_timeouts_command_fn,
669 };
670
671 VLIB_CLI_COMMAND (show_nat64_timeouts_command, static) = {
672   .path = "show nat64 tiemouts",
673   .short_help = "show nat64 tiemouts",
674   .function = nat64_show_timeouts_command_fn,
675 };
676
677 VLIB_CLI_COMMAND (show_nat64_st_command, static) = {
678   .path = "show nat64 session table",
679   .short_help = "show nat64 session table tcp|udp|icmp",
680   .function = nat64_show_st_command_fn,
681 };
682
683 /* *INDENT-ON* */
684
685 /*
686  * fd.io coding-style-patch-verification: ON
687  *
688  * Local Variables:
689  * eval: (c-set-style "gnu")
690  * End:
691  */