nat: move nat64 to a subfeature
[vpp.git] / src / plugins / nat / nat64 / nat64_cli.c
1 /*
2  * Copyright (c) 2020 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 <vnet/fib/fib_table.h>
17 #include <nat/nat64/nat64.h>
18
19 static clib_error_t *
20 nat64_plugin_enable_disable_command_fn (vlib_main_t * vm,
21                                         unformat_input_t * input,
22                                         vlib_cli_command_t * cmd)
23 {
24   unformat_input_t _line_input, *line_input = &_line_input;
25   u8 enable = 0, is_set = 0;
26   clib_error_t *error = 0;
27   nat64_config_t c = { 0 };
28
29   if (!unformat_user (input, unformat_line_input, line_input))
30     return 0;
31
32   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
33     {
34       if (!is_set && unformat (line_input, "enable"))
35         {
36           unformat (line_input, "bib-buckets %u", &c.bib_buckets);
37           unformat (line_input, "bib-memory %u", &c.bib_memory_size);
38           unformat (line_input, "st-buckets %u", &c.st_buckets);
39           unformat (line_input, "st-memory %u", &c.st_memory_size);
40           enable = 1;
41         }
42       else if (!is_set && unformat (line_input, "disable"));
43       else
44         {
45           error = clib_error_return (0, "unknown input '%U'",
46                                      format_unformat_error, line_input);
47           goto done;
48         }
49       is_set = 1;
50     }
51
52   if (enable)
53     {
54       if (nat64_plugin_enable (c))
55         error = clib_error_return (0, "plugin enable failed");
56     }
57   else
58     {
59       if (nat64_plugin_disable ())
60         error = clib_error_return (0, "plugin disable failed");
61     }
62 done:
63   unformat_free (line_input);
64   return error;
65 }
66
67 static clib_error_t *
68 nat64_add_del_pool_addr_command_fn (vlib_main_t * vm,
69                                     unformat_input_t * input,
70                                     vlib_cli_command_t * cmd)
71 {
72   unformat_input_t _line_input, *line_input = &_line_input;
73   ip4_address_t start_addr, end_addr, this_addr;
74   u32 start_host_order, end_host_order;
75   int i, count, rv;
76   u32 vrf_id = ~0;
77   u8 is_add = 1;
78   clib_error_t *error = 0;
79
80   /* Get a line of input. */
81   if (!unformat_user (input, unformat_line_input, line_input))
82     return 0;
83
84   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
85     {
86       if (unformat (line_input, "%U - %U",
87                     unformat_ip4_address, &start_addr,
88                     unformat_ip4_address, &end_addr))
89         ;
90       else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
91         ;
92       else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
93         end_addr = start_addr;
94       else if (unformat (line_input, "del"))
95         is_add = 0;
96       else
97         {
98           error = clib_error_return (0, "unknown input '%U'",
99                                      format_unformat_error, line_input);
100           goto done;
101         }
102     }
103
104   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
105   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
106
107   if (end_host_order < start_host_order)
108     {
109       error = clib_error_return (0, "end address less than start address");
110       goto done;
111     }
112
113   count = (end_host_order - start_host_order) + 1;
114   this_addr = start_addr;
115
116   for (i = 0; i < count; i++)
117     {
118       rv = nat64_add_del_pool_addr (0, &this_addr, vrf_id, is_add);
119
120       switch (rv)
121         {
122         case VNET_API_ERROR_NO_SUCH_ENTRY:
123           error =
124             clib_error_return (0, "NAT64 pool address %U not exist.",
125                                format_ip4_address, &this_addr);
126           goto done;
127         case VNET_API_ERROR_VALUE_EXIST:
128           error =
129             clib_error_return (0, "NAT64 pool address %U exist.",
130                                format_ip4_address, &this_addr);
131           goto done;
132         default:
133           break;
134
135         }
136       increment_v4_address (&this_addr);
137     }
138
139 done:
140   unformat_free (line_input);
141
142   return error;
143 }
144
145 static int
146 nat64_cli_pool_walk (nat64_address_t * ap, void *ctx)
147 {
148   vlib_main_t *vm = ctx;
149
150   if (ap->fib_index != ~0)
151     {
152       fib_table_t *fib;
153       fib = fib_table_get (ap->fib_index, FIB_PROTOCOL_IP6);
154       if (!fib)
155         return -1;
156       vlib_cli_output (vm, " %U tenant VRF: %u", format_ip4_address,
157                        &ap->addr, fib->ft_table_id);
158     }
159   else
160     vlib_cli_output (vm, " %U", format_ip4_address, &ap->addr);
161
162 #define _(N, i, n, s) \
163   vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
164   foreach_nat_protocol
165 #undef _
166     return 0;
167 }
168
169 static clib_error_t *
170 nat64_show_pool_command_fn (vlib_main_t * vm,
171                             unformat_input_t * input,
172                             vlib_cli_command_t * cmd)
173 {
174   vlib_cli_output (vm, "NAT64 pool:");
175   nat64_pool_addr_walk (nat64_cli_pool_walk, vm);
176
177   return 0;
178 }
179
180 static clib_error_t *
181 nat64_interface_feature_command_fn (vlib_main_t * vm,
182                                     unformat_input_t *
183                                     input, vlib_cli_command_t * cmd)
184 {
185   unformat_input_t _line_input, *line_input = &_line_input;
186   vnet_main_t *vnm = vnet_get_main ();
187   clib_error_t *error = 0;
188   u32 sw_if_index;
189   u32 *inside_sw_if_indices = 0;
190   u32 *outside_sw_if_indices = 0;
191   u8 is_add = 1;
192   int i, rv;
193
194   /* Get a line of input. */
195   if (!unformat_user (input, unformat_line_input, line_input))
196     return 0;
197
198   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
199     {
200       if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
201                     vnm, &sw_if_index))
202         vec_add1 (inside_sw_if_indices, sw_if_index);
203       else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
204                          vnm, &sw_if_index))
205         vec_add1 (outside_sw_if_indices, sw_if_index);
206       else if (unformat (line_input, "del"))
207         is_add = 0;
208       else
209         {
210           error = clib_error_return (0, "unknown input '%U'",
211                                      format_unformat_error, line_input);
212           goto done;
213         }
214     }
215
216   if (vec_len (inside_sw_if_indices))
217     {
218       for (i = 0; i < vec_len (inside_sw_if_indices); i++)
219         {
220           sw_if_index = inside_sw_if_indices[i];
221           rv = nat64_interface_add_del (sw_if_index, 1, is_add);
222           switch (rv)
223             {
224             case VNET_API_ERROR_NO_SUCH_ENTRY:
225               error =
226                 clib_error_return (0, "%U NAT64 feature not enabled.",
227                                    format_vnet_sw_if_index_name, vnm,
228                                    sw_if_index);
229               goto done;
230             case VNET_API_ERROR_VALUE_EXIST:
231               error =
232                 clib_error_return (0, "%U NAT64 feature already enabled.",
233                                    format_vnet_sw_if_index_name, vnm,
234                                    vnm, sw_if_index);
235               goto done;
236             case VNET_API_ERROR_INVALID_VALUE:
237             case VNET_API_ERROR_INVALID_VALUE_2:
238               error =
239                 clib_error_return (0,
240                                    "%U NAT64 feature enable/disable failed.",
241                                    format_vnet_sw_if_index_name, vnm,
242                                    sw_if_index);
243               goto done;
244             default:
245               break;
246
247             }
248         }
249     }
250
251   if (vec_len (outside_sw_if_indices))
252     {
253       for (i = 0; i < vec_len (outside_sw_if_indices); i++)
254         {
255           sw_if_index = outside_sw_if_indices[i];
256           rv = nat64_interface_add_del (sw_if_index, 0, is_add);
257           switch (rv)
258             {
259             case VNET_API_ERROR_NO_SUCH_ENTRY:
260               error =
261                 clib_error_return (0, "%U NAT64 feature not enabled.",
262                                    format_vnet_sw_if_index_name, vnm,
263                                    sw_if_index);
264               goto done;
265             case VNET_API_ERROR_VALUE_EXIST:
266               error =
267                 clib_error_return (0, "%U NAT64 feature already enabled.",
268                                    format_vnet_sw_if_index_name, vnm,
269                                    sw_if_index);
270               goto done;
271             case VNET_API_ERROR_INVALID_VALUE:
272             case VNET_API_ERROR_INVALID_VALUE_2:
273               error =
274                 clib_error_return (0,
275                                    "%U NAT64 feature enable/disable failed.",
276                                    format_vnet_sw_if_index_name, vnm,
277                                    sw_if_index);
278               goto done;
279             default:
280               break;
281
282             }
283         }
284     }
285
286 done:
287   unformat_free (line_input);
288   vec_free (inside_sw_if_indices);
289   vec_free (outside_sw_if_indices);
290
291   return error;
292 }
293
294 static int
295 nat64_cli_interface_walk (nat64_interface_t * i, void *ctx)
296 {
297   vlib_main_t *vm = ctx;
298   vnet_main_t *vnm = vnet_get_main ();
299
300   vlib_cli_output (vm, " %U %s", format_vnet_sw_if_index_name, vnm,
301                    i->sw_if_index,
302                    (nat64_interface_is_inside (i)
303                     && nat64_interface_is_outside (i)) ? "in out" :
304                    nat64_interface_is_inside (i) ? "in" : "out");
305   return 0;
306 }
307
308 static clib_error_t *
309 nat64_show_interfaces_command_fn (vlib_main_t * vm,
310                                   unformat_input_t *
311                                   input, vlib_cli_command_t * cmd)
312 {
313   vlib_cli_output (vm, "NAT64 interfaces:");
314   nat64_interfaces_walk (nat64_cli_interface_walk, vm);
315
316   return 0;
317 }
318
319 static clib_error_t *
320 nat64_add_del_static_bib_command_fn (vlib_main_t *
321                                      vm,
322                                      unformat_input_t
323                                      * input, vlib_cli_command_t * cmd)
324 {
325   unformat_input_t _line_input, *line_input = &_line_input;
326   clib_error_t *error = 0;
327   u8 is_add = 1;
328   ip6_address_t in_addr;
329   ip4_address_t out_addr;
330   u32 in_port = 0;
331   u32 out_port = 0;
332   u32 vrf_id = 0, protocol;
333   nat_protocol_t proto = 0;
334   u8 p = 0;
335   int rv;
336
337   if (!unformat_user (input, unformat_line_input, line_input))
338     return 0;
339
340   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
341     {
342       if (unformat (line_input, "%U %u", unformat_ip6_address,
343                     &in_addr, &in_port))
344         ;
345       else if (unformat (line_input, "%U %u", unformat_ip4_address,
346                          &out_addr, &out_port))
347         ;
348       else if (unformat (line_input, "vrf %u", &vrf_id))
349         ;
350       else if (unformat (line_input, "%U", unformat_nat_protocol, &proto))
351         ;
352       else
353         if (unformat
354             (line_input, "%U %U %u", unformat_ip6_address, &in_addr,
355              unformat_ip4_address, &out_addr, &protocol))
356         p = (u8) protocol;
357       else if (unformat (line_input, "del"))
358         is_add = 0;
359       else
360         {
361           error = clib_error_return (0, "unknown input: '%U'",
362                                      format_unformat_error, line_input);
363           goto done;
364         }
365     }
366
367   if (!p)
368     {
369       if (!in_port)
370         {
371           error =
372             clib_error_return (0, "inside port and address  must be set");
373           goto done;
374         }
375
376       if (!out_port)
377         {
378           error =
379             clib_error_return (0, "outside port and address  must be set");
380           goto done;
381         }
382
383       p = nat_proto_to_ip_proto (proto);
384     }
385
386   rv =
387     nat64_add_del_static_bib_entry (&in_addr, &out_addr, (u16) in_port,
388                                     (u16) out_port, p, vrf_id, is_add);
389
390   switch (rv)
391     {
392     case VNET_API_ERROR_NO_SUCH_ENTRY:
393       error = clib_error_return (0, "NAT64 BIB entry not exist.");
394       goto done;
395     case VNET_API_ERROR_VALUE_EXIST:
396       error = clib_error_return (0, "NAT64 BIB entry exist.");
397       goto done;
398     case VNET_API_ERROR_UNSPECIFIED:
399       error = clib_error_return (0, "Crerate NAT64 BIB entry failed.");
400       goto done;
401     case VNET_API_ERROR_INVALID_VALUE:
402       error =
403         clib_error_return (0,
404                            "Outside address %U and port %u already in use.",
405                            format_ip4_address, &out_addr, out_port);
406       goto done;
407     case VNET_API_ERROR_INVALID_VALUE_2:
408       error = clib_error_return (0, "Invalid outside port.");
409     default:
410       break;
411     }
412
413 done:
414   unformat_free (line_input);
415
416   return error;
417 }
418
419 static int
420 nat64_cli_bib_walk (nat64_db_bib_entry_t * bibe, void *ctx)
421 {
422   vlib_main_t *vm = ctx;
423   fib_table_t *fib;
424
425   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
426   if (!fib)
427     return -1;
428
429   switch (bibe->proto)
430     {
431     case IP_PROTOCOL_ICMP:
432     case IP_PROTOCOL_TCP:
433     case IP_PROTOCOL_UDP:
434       vlib_cli_output (vm, " %U %u %U %u protocol %U vrf %u %s %u sessions",
435                        format_ip6_address, &bibe->in_addr,
436                        clib_net_to_host_u16 (bibe->in_port),
437                        format_ip4_address, &bibe->out_addr,
438                        clib_net_to_host_u16 (bibe->out_port),
439                        format_nat_protocol,
440                        ip_proto_to_nat_proto (bibe->proto), fib->ft_table_id,
441                        bibe->is_static ? "static" : "dynamic", bibe->ses_num);
442       break;
443     default:
444       vlib_cli_output (vm, " %U %U protocol %u vrf %u %s %u sessions",
445                        format_ip6_address, &bibe->in_addr,
446                        format_ip4_address, &bibe->out_addr,
447                        bibe->proto, fib->ft_table_id,
448                        bibe->is_static ? "static" : "dynamic", bibe->ses_num);
449     }
450   return 0;
451 }
452
453 static clib_error_t *
454 nat64_show_bib_command_fn (vlib_main_t * vm,
455                            unformat_input_t * input, vlib_cli_command_t * cmd)
456 {
457   nat64_main_t *nm = &nat64_main;
458   unformat_input_t _line_input, *line_input = &_line_input;
459   clib_error_t *error = 0;
460   u32 proto = NAT_PROTOCOL_OTHER;
461   u8 p = 255;
462   nat64_db_t *db;
463
464   if (!unformat_user (input, unformat_line_input, line_input))
465     return 0;
466
467   if (unformat (line_input, "%U", unformat_nat_protocol, &proto))
468     p = nat_proto_to_ip_proto (proto);
469   else if (unformat (line_input, "unknown"))
470     p = 0;
471   else if (unformat (line_input, "all"))
472     ;
473   else
474     {
475       error = clib_error_return (0, "unknown input: '%U'",
476                                  format_unformat_error, line_input);
477       goto done;
478     }
479
480   if (p == 255)
481     vlib_cli_output (vm, "NAT64 BIB entries:");
482   else
483     vlib_cli_output (vm, "NAT64 %U BIB entries:", format_nat_protocol, proto);
484
485   /* *INDENT-OFF* */
486   vec_foreach (db, nm->db)
487     nat64_db_bib_walk (db, p, nat64_cli_bib_walk, vm);
488   /* *INDENT-ON* */
489
490 done:
491   unformat_free (line_input);
492
493   return error;
494 }
495
496 typedef struct nat64_cli_st_walk_ctx_t_
497 {
498   vlib_main_t *vm;
499   nat64_db_t *db;
500 } nat64_cli_st_walk_ctx_t;
501
502 static int
503 nat64_cli_st_walk (nat64_db_st_entry_t * ste, void *arg)
504 {
505   nat64_cli_st_walk_ctx_t *ctx = arg;
506   vlib_main_t *vm = ctx->vm;
507   nat64_db_bib_entry_t *bibe;
508   fib_table_t *fib;
509
510   bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
511   if (!bibe)
512     return -1;
513
514   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
515   if (!fib)
516     return -1;
517
518   u32 vrf_id = fib->ft_table_id;
519
520   if (ste->proto == IP_PROTOCOL_ICMP)
521     vlib_cli_output (vm, " %U %U %u %U %U %u protocol %U vrf %u",
522                      format_ip6_address, &bibe->in_addr,
523                      format_ip6_address, &ste->in_r_addr,
524                      clib_net_to_host_u16 (bibe->in_port),
525                      format_ip4_address, &bibe->out_addr,
526                      format_ip4_address, &ste->out_r_addr,
527                      clib_net_to_host_u16 (bibe->out_port),
528                      format_nat_protocol,
529                      ip_proto_to_nat_proto (bibe->proto), vrf_id);
530   else if (ste->proto == IP_PROTOCOL_TCP || ste->proto == IP_PROTOCOL_UDP)
531     vlib_cli_output (vm, " %U %u %U %u %U %u %U %u protcol %U vrf %u",
532                      format_ip6_address, &bibe->in_addr,
533                      clib_net_to_host_u16 (bibe->in_port),
534                      format_ip6_address, &ste->in_r_addr,
535                      clib_net_to_host_u16 (ste->r_port),
536                      format_ip4_address, &bibe->out_addr,
537                      clib_net_to_host_u16 (bibe->out_port),
538                      format_ip4_address, &ste->out_r_addr,
539                      clib_net_to_host_u16 (ste->r_port),
540                      format_nat_protocol,
541                      ip_proto_to_nat_proto (bibe->proto), vrf_id);
542   else
543     vlib_cli_output (vm, " %U %U %U %U protocol %u vrf %u",
544                      format_ip6_address, &bibe->in_addr,
545                      format_ip6_address, &ste->in_r_addr,
546                      format_ip4_address, &bibe->out_addr,
547                      format_ip4_address, &ste->out_r_addr,
548                      bibe->proto, vrf_id);
549
550   return 0;
551 }
552
553 static clib_error_t *
554 nat64_show_st_command_fn (vlib_main_t * vm,
555                           unformat_input_t * input, vlib_cli_command_t * cmd)
556 {
557   nat64_main_t *nm = &nat64_main;
558   unformat_input_t _line_input, *line_input = &_line_input;
559   clib_error_t *error = 0;
560   u32 proto = NAT_PROTOCOL_OTHER;
561   u8 p = 255;
562   nat64_db_t *db;
563   nat64_cli_st_walk_ctx_t ctx = {
564     .vm = vm,
565   };
566
567   if (!unformat_user (input, unformat_line_input, line_input))
568     return 0;
569
570   if (unformat (line_input, "%U", unformat_nat_protocol, &proto))
571     p = nat_proto_to_ip_proto (proto);
572   else if (unformat (line_input, "unknown"))
573     p = 0;
574   else if (unformat (line_input, "all"))
575     ;
576   else
577     {
578       error = clib_error_return (0, "unknown input: '%U'",
579                                  format_unformat_error, line_input);
580       goto done;
581     }
582
583   if (p == 255)
584     vlib_cli_output (vm, "NAT64 sessions:");
585   else
586     vlib_cli_output (vm, "NAT64 %U sessions:", format_nat_protocol, proto);
587   /* *INDENT-OFF* */
588   vec_foreach (db, nm->db)
589     {
590       ctx.db = db;
591       nat64_db_st_walk (db, p, nat64_cli_st_walk, &ctx);
592     }
593   /* *INDENT-ON* */
594
595 done:
596   unformat_free (line_input);
597
598   return error;
599 }
600
601 static clib_error_t *
602 nat64_add_del_prefix_command_fn (vlib_main_t * vm, unformat_input_t * input,
603                                  vlib_cli_command_t * cmd)
604 {
605   nat64_main_t *nm = &nat64_main;
606   vnet_main_t *vnm = vnet_get_main ();
607   clib_error_t *error = 0;
608   unformat_input_t _line_input, *line_input = &_line_input;
609   u8 is_add = 1;
610   u32 vrf_id = 0, sw_if_index = ~0;
611   ip6_address_t prefix;
612   u32 plen = 0;
613   int rv;
614
615   if (!unformat_user (input, unformat_line_input, line_input))
616     return 0;
617
618   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
619     {
620       if (unformat
621           (line_input, "%U/%u", unformat_ip6_address, &prefix, &plen))
622         ;
623       else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
624         ;
625       else if (unformat (line_input, "del"))
626         is_add = 0;
627       else
628         if (unformat
629             (line_input, "interface %U", unformat_vnet_sw_interface, vnm,
630              &sw_if_index))
631         ;
632       else
633         {
634           error = clib_error_return (0, "unknown input: '%U'",
635                                      format_unformat_error, line_input);
636           goto done;
637         }
638     }
639
640   if (!plen)
641     {
642       error = clib_error_return (0, "NAT64 prefix must be set.");
643       goto done;
644     }
645
646   rv = nat64_add_del_prefix (&prefix, (u8) plen, vrf_id, is_add);
647
648   switch (rv)
649     {
650     case VNET_API_ERROR_NO_SUCH_ENTRY:
651       error = clib_error_return (0, "NAT64 prefix not exist.");
652       goto done;
653     case VNET_API_ERROR_INVALID_VALUE:
654       error = clib_error_return (0, "Invalid prefix length.");
655       goto done;
656     default:
657       break;
658     }
659
660   /*
661    * Add RX interface route, whenNAT isn't running on the real input
662    * interface
663    */
664   if (sw_if_index != ~0)
665     {
666       u32 fib_index;
667       fib_prefix_t fibpfx = {
668         .fp_len = plen,
669         .fp_proto = FIB_PROTOCOL_IP6,
670         .fp_addr = {
671                     .ip6 = prefix}
672       };
673
674       if (is_add)
675         {
676           fib_index =
677             fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
678                                                vrf_id, nm->fib_src_hi);
679           fib_table_entry_update_one_path (fib_index, &fibpfx,
680                                            nm->fib_src_hi,
681                                            FIB_ENTRY_FLAG_NONE,
682                                            DPO_PROTO_IP6, NULL,
683                                            sw_if_index, ~0, 0,
684                                            NULL, FIB_ROUTE_PATH_INTF_RX);
685         }
686       else
687         {
688           fib_index = fib_table_find (FIB_PROTOCOL_IP6, vrf_id);
689           fib_table_entry_path_remove (fib_index, &fibpfx,
690                                        nm->fib_src_hi,
691                                        DPO_PROTO_IP6, NULL,
692                                        sw_if_index, ~0, 1,
693                                        FIB_ROUTE_PATH_INTF_RX);
694           fib_table_unlock (fib_index, FIB_PROTOCOL_IP6, nm->fib_src_hi);
695         }
696     }
697
698 done:
699   unformat_free (line_input);
700
701   return error;
702 }
703
704 static int
705 nat64_cli_prefix_walk (nat64_prefix_t * p, void *ctx)
706 {
707   vlib_main_t *vm = ctx;
708
709   vlib_cli_output (vm, " %U/%u tenant-vrf %u",
710                    format_ip6_address, &p->prefix, p->plen, p->vrf_id);
711
712   return 0;
713 }
714
715 static clib_error_t *
716 nat64_show_prefix_command_fn (vlib_main_t * vm,
717                               unformat_input_t * input,
718                               vlib_cli_command_t * cmd)
719 {
720   vlib_cli_output (vm, "NAT64 prefix:");
721   nat64_prefix_walk (nat64_cli_prefix_walk, vm);
722
723   return 0;
724 }
725
726 static clib_error_t *
727 nat64_add_interface_address_command_fn (vlib_main_t * vm,
728                                         unformat_input_t * input,
729                                         vlib_cli_command_t * cmd)
730 {
731   vnet_main_t *vnm = vnet_get_main ();
732   unformat_input_t _line_input, *line_input = &_line_input;
733   u32 sw_if_index;
734   int rv;
735   int is_add = 1;
736   clib_error_t *error = 0;
737
738   /* Get a line of input. */
739   if (!unformat_user (input, unformat_line_input, line_input))
740     return 0;
741
742   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
743     {
744       if (unformat
745           (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index));
746       else if (unformat (line_input, "del"))
747         is_add = 0;
748       else
749         {
750           error = clib_error_return (0, "unknown input '%U'",
751                                      format_unformat_error, line_input);
752           goto done;
753         }
754     }
755
756   rv = nat64_add_interface_address (sw_if_index, is_add);
757
758   switch (rv)
759     {
760     case VNET_API_ERROR_NO_SUCH_ENTRY:
761       error = clib_error_return (0, "entry not exist");
762       break;
763     case VNET_API_ERROR_VALUE_EXIST:
764       error = clib_error_return (0, "entry exist");
765       break;
766     default:
767       break;
768     }
769
770 done:
771   unformat_free (line_input);
772
773   return error;
774 }
775
776 /* *INDENT-OFF* */
777 /*?
778  * @cliexpar
779  * @cliexstart{nat64 plugin}
780  * Enable/disable NAT64 plugin.
781  * To enable NAT64 plugin use:
782  *  vpp# nat64 plugin enable
783  * To enable NAT64 plugin and configure buckets/memory:
784  *  vpp# nat64 plugin enable bib-buckets <n> bib-memory <s> \
785  *    st-buckets <n> st-memory <s>
786  * To disable NAT64 plugin:
787  *  vpp# nat64 plugin disable
788  * @cliexend
789 ?*/
790 VLIB_CLI_COMMAND (nat64_plugin_enable_disable_command, static) =
791 {
792   .path = "nat64 plugin",
793   .short_help = "nat64 plugin <enable "
794                 "[bib-buckets <count>] [bib-memory <size>] "
795                 "[st-buckets <count>] [st-memory <size>] | disable>",
796   .function = nat64_plugin_enable_disable_command_fn,
797 };
798
799 /*?
800  * @cliexpar
801  * @cliexstart{nat64 add pool address}
802  * Add/delete NAT64 pool address.
803  * To add single NAT64 pool address use:
804  *  vpp# nat64 add pool address 10.1.1.10
805  * To add NAT64 pool address range use:
806  *  vpp# nat64 add pool address 10.1.1.2 - 10.1.1.5
807  * To add NAT64 pool address for specific tenant use:
808  *  vpp# nat64 add pool address 10.1.1.100 tenant-vrf 100
809  * @cliexend
810 ?*/
811 VLIB_CLI_COMMAND (nat64_add_pool_address_command, static) = {
812   .path = "nat64 add pool address",
813   .short_help = "nat64 add pool address <ip4-range-start> [- <ip4-range-end>] "
814                 "[tenant-vrf <vrf-id>] [del]",
815   .function = nat64_add_del_pool_addr_command_fn,
816 };
817
818 /*?
819  * @cliexpar
820  * @cliexstart{show nat64 pool}
821  * Show NAT64 pool.
822  *  vpp# show nat64 pool
823  *  NAT64 pool:
824  *   10.1.1.3 tenant VRF: 0
825  *   10.1.1.10 tenant VRF: 10
826  * @cliexend
827 ?*/
828 VLIB_CLI_COMMAND (show_nat64_pool_command, static) = {
829   .path = "show nat64 pool",
830   .short_help = "show nat64 pool",
831   .function = nat64_show_pool_command_fn,
832 };
833
834 /*?
835  * @cliexpar
836  * @cliexstart{set interface nat64}
837  * Enable/disable NAT64 feature on the interface.
838  * To enable NAT64 feature with local (IPv6) network interface
839  * GigabitEthernet0/8/0 and external (IPv4) network interface
840  * GigabitEthernet0/a/0 use:
841  *  vpp# set interface nat64 in GigabitEthernet0/8/0 out GigabitEthernet0/a/0
842  * @cliexend
843 ?*/
844 VLIB_CLI_COMMAND (set_interface_nat64_command, static) = {
845   .path = "set interface nat64",
846   .short_help = "set interface nat64 in|out <intfc> [del]",
847   .function = nat64_interface_feature_command_fn,
848 };
849
850 /*?
851  * @cliexpar
852  * @cliexstart{show nat64 interfaces}
853  * Show interfaces with NAT64 feature.
854  * To show interfaces with NAT64 feature use:
855  *  vpp# show nat64 interfaces
856  *  NAT64 interfaces:
857  *   GigabitEthernet0/8/0 in
858  *   GigabitEthernet0/a/0 out
859  * @cliexend
860 ?*/
861 VLIB_CLI_COMMAND (show_nat64_interfaces_command, static) = {
862   .path = "show nat64 interfaces",
863   .short_help = "show nat64 interfaces",
864   .function = nat64_show_interfaces_command_fn,
865 };
866
867 /*?
868  * @cliexpar
869  * @cliexstart{nat64 add static bib}
870  * Add/delete NAT64 static BIB entry.
871  * To create NAT64 satatic BIB entry use:
872  *  vpp# nat64 add static bib 2001:db8:c000:221:: 1234 10.1.1.3 5678 tcp
873  *  vpp# nat64 add static bib 2001:db8:c000:221:: 1234 10.1.1.3 5678 udp vrf 10
874  * @cliexend
875 ?*/
876 VLIB_CLI_COMMAND (nat64_add_del_static_bib_command, static) = {
877   .path = "nat64 add static bib",
878   .short_help = "nat64 add static bib <ip6-addr> <port> <ip4-addr> <port> "
879                 "tcp|udp|icmp [vfr <table-id>] [del]",
880   .function = nat64_add_del_static_bib_command_fn,
881 };
882
883 /*?
884  * @cliexpar
885  * @cliexstart{show nat64 bib}
886  * Show NAT64 BIB entries.
887  * To show NAT64 TCP BIB entries use:
888  *  vpp# show nat64 bib tcp
889  *  NAT64 tcp BIB:
890  *   fd01:1::2 6303 10.0.0.3 62303 tcp vrf 0 dynamic 1 sessions
891  *   2001:db8:c000:221:: 1234 10.1.1.3 5678 tcp vrf 0 static 2 sessions
892  * To show NAT64 UDP BIB entries use:
893  *  vpp# show nat64 bib udp
894  *  NAT64 udp BIB:
895  *   fd01:1::2 6304 10.0.0.3 10546 udp vrf 0 dynamic 10 sessions
896  *   2001:db8:c000:221:: 1234 10.1.1.3 5678 udp vrf 10 static 0 sessions
897  * To show NAT64 ICMP BIB entries use:
898  *  vpp# show nat64 bib icmp
899  *  NAT64 icmp BIB:
900  *   fd01:1::2 6305 10.0.0.3 63209 icmp vrf 10 dynamic 1 sessions
901  * @cliexend
902 ?*/
903 VLIB_CLI_COMMAND (show_nat64_bib_command, static) = {
904   .path = "show nat64 bib",
905   .short_help = "show nat64 bib all|tcp|udp|icmp|unknown",
906   .function = nat64_show_bib_command_fn,
907 };
908
909 /*?
910  * @cliexpar
911  * @cliexstart{show nat64 session table}
912  * Show NAT64 session table.
913  * To show NAT64 TCP session table use:
914  *  vpp# show nat64 session table tcp
915  *  NAT64 tcp session table:
916  *   fd01:1::2 6303 64:ff9b::ac10:202 20 10.0.0.3 62303 172.16.2.2 20 tcp vrf 0
917  *   fd01:3::2 6303 64:ff9b::ac10:202 20 10.0.10.3 21300 172.16.2.2 20 tcp vrf 10
918  * To show NAT64 UDP session table use:
919  * #vpp show nat64 session table udp
920  * NAT64 udp session table:
921  *  fd01:1::2 6304 64:ff9b::ac10:202 20 10.0.0.3 10546 172.16.2.2 20 udp vrf 0
922  *  fd01:3::2 6304 64:ff9b::ac10:202 20 10.0.10.3 58627 172.16.2.2 20 udp vrf 10
923  *  fd01:1::2 1235 64:ff9b::a00:3 4023 10.0.0.3 24488 10.0.0.3 4023 udp vrf 0
924  *  fd01:1::3 23 64:ff9b::a00:3 24488 10.0.0.3 4023 10.0.0.3 24488 udp vrf 0
925  * To show NAT64 ICMP session table use:
926  * #vpp show nat64 session table icmp
927  * NAT64 icmp session table:
928  *  fd01:1::2 64:ff9b::ac10:202 6305 10.0.0.3 172.16.2.2 63209 icmp vrf 0
929  * @cliexend
930 ?*/
931 VLIB_CLI_COMMAND (show_nat64_st_command, static) = {
932   .path = "show nat64 session table",
933   .short_help = "show nat64 session table all|tcp|udp|icmp|unknown",
934   .function = nat64_show_st_command_fn,
935 };
936
937 /*?
938  * @cliexpar
939  * @cliexstart{nat64 add prefix}
940  * Set NAT64 prefix for generating IPv6 representations of IPv4 addresses.
941  * To set NAT64 global prefix use:
942  *  vpp# nat64 add prefix 2001:db8::/32
943  * To set NAT64 prefix for specific tenant use:
944  *  vpp# nat64 add prefix 2001:db8:122:300::/56 tenant-vrf 10
945  * @cliexend
946 ?*/
947 VLIB_CLI_COMMAND (nat64_add_del_prefix_command, static) = {
948   .path = "nat64 add prefix",
949   .short_help = "nat64 add prefix <ip6-prefix>/<plen> [tenant-vrf <vrf-id>] "
950                 "[del] [interface <interface]",
951   .function = nat64_add_del_prefix_command_fn,
952 };
953
954 /*?
955  * @cliexpar
956  * @cliexstart{show nat64 prefix}
957  * Show NAT64 prefix.
958  * To show NAT64 prefix use:
959  *  vpp# show nat64 prefix
960  *  NAT64 prefix:
961  *   2001:db8::/32 tenant-vrf 0
962  *   2001:db8:122:300::/56 tenant-vrf 10
963  * @cliexend
964 ?*/
965 VLIB_CLI_COMMAND (show_nat64_prefix_command, static) = {
966   .path = "show nat64 prefix",
967   .short_help = "show nat64 prefix",
968   .function = nat64_show_prefix_command_fn,
969 };
970
971 /*?
972  * @cliexpar
973  * @cliexstart{nat64 add interface address}
974  * Add/delete NAT64 pool address from specific (DHCP addressed) interface.
975  * To add NAT64 pool address from specific interface use:
976  *  vpp# nat64 add interface address GigabitEthernet0/8/0
977  * @cliexend
978 ?*/
979 VLIB_CLI_COMMAND (nat64_add_interface_address_command, static) = {
980     .path = "nat64 add interface address",
981     .short_help = "nat64 add interface address <interface> [del]",
982     .function = nat64_add_interface_address_command_fn,
983 };
984 /* *INDENT-ON* */
985
986 /*
987  * fd.io coding-style-patch-verification: ON
988  *
989  * Local Variables:
990  * eval: (c-set-style "gnu")
991  * End:
992  */