NAT: disable CLI/API not supported in active mode (VPP-1234)
[vpp.git] / src / plugins / nat / nat44_cli.c
1 /*
2  * Copyright (c) 2018 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 NAT44 CLI
18  */
19
20 #include <nat/nat.h>
21 #include <nat/nat_ipfix_logging.h>
22 #include <nat/nat_det.h>
23 #include <vnet/fib/fib_table.h>
24
25 #define UNSUPPORTED_IN_DET_MODE_STR \
26   "This command is unsupported in deterministic mode"
27 #define SUPPORTED_ONLY_IN_DET_MODE_STR \
28   "This command is supported only in deterministic mode"
29
30 static clib_error_t *
31 set_workers_command_fn (vlib_main_t * vm,
32                         unformat_input_t * input, vlib_cli_command_t * cmd)
33 {
34   unformat_input_t _line_input, *line_input = &_line_input;
35   snat_main_t *sm = &snat_main;
36   uword *bitmap = 0;
37   int rv = 0;
38   clib_error_t *error = 0;
39
40   if (sm->deterministic)
41     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
42
43   /* Get a line of input. */
44   if (!unformat_user (input, unformat_line_input, line_input))
45     return 0;
46
47   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
48     {
49       if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
50         ;
51       else
52         {
53           error = clib_error_return (0, "unknown input '%U'",
54                                      format_unformat_error, line_input);
55           goto done;
56         }
57     }
58
59   if (bitmap == 0)
60     {
61       error = clib_error_return (0, "List of workers must be specified.");
62       goto done;
63     }
64
65   rv = snat_set_workers (bitmap);
66
67   clib_bitmap_free (bitmap);
68
69   switch (rv)
70     {
71     case VNET_API_ERROR_INVALID_WORKER:
72       error = clib_error_return (0, "Invalid worker(s).");
73       goto done;
74     case VNET_API_ERROR_FEATURE_DISABLED:
75       error = clib_error_return (0,
76                                  "Supported only if 2 or more workes available.");
77       goto done;
78     default:
79       break;
80     }
81
82 done:
83   unformat_free (line_input);
84
85   return error;
86 }
87
88 static clib_error_t *
89 nat_show_workers_commnad_fn (vlib_main_t * vm, unformat_input_t * input,
90                              vlib_cli_command_t * cmd)
91 {
92   snat_main_t *sm = &snat_main;
93   u32 *worker;
94
95   if (sm->deterministic)
96     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
97
98   if (sm->num_workers > 1)
99     {
100       vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
101       /* *INDENT-OFF* */
102       vec_foreach (worker, sm->workers)
103         {
104           vlib_worker_thread_t *w =
105             vlib_worker_threads + *worker + sm->first_worker_index;
106           vlib_cli_output (vm, "  %s", w->name);
107         }
108       /* *INDENT-ON* */
109     }
110
111   return 0;
112 }
113
114 static clib_error_t *
115 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
116                                               unformat_input_t * input,
117                                               vlib_cli_command_t * cmd)
118 {
119   unformat_input_t _line_input, *line_input = &_line_input;
120   u32 domain_id = 0;
121   u32 src_port = 0;
122   u8 enable = 1;
123   int rv = 0;
124   clib_error_t *error = 0;
125
126   /* Get a line of input. */
127   if (!unformat_user (input, unformat_line_input, line_input))
128     return 0;
129
130   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
131     {
132       if (unformat (line_input, "domain %d", &domain_id))
133         ;
134       else if (unformat (line_input, "src-port %d", &src_port))
135         ;
136       else if (unformat (line_input, "disable"))
137         enable = 0;
138       else
139         {
140           error = clib_error_return (0, "unknown input '%U'",
141                                      format_unformat_error, line_input);
142           goto done;
143         }
144     }
145
146   rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
147
148   if (rv)
149     {
150       error = clib_error_return (0, "ipfix logging enable failed");
151       goto done;
152     }
153
154 done:
155   unformat_free (line_input);
156
157   return error;
158 }
159
160 static clib_error_t *
161 nat44_set_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
162                                               unformat_input_t * input,
163                                               vlib_cli_command_t * cmd)
164 {
165   unformat_input_t _line_input, *line_input = &_line_input;
166   snat_main_t *sm = &snat_main;
167   clib_error_t *error = 0;
168   u32 psid, psid_offset, psid_length;
169
170   if (sm->deterministic)
171     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
172
173   /* Get a line of input. */
174   if (!unformat_user (input, unformat_line_input, line_input))
175     return 0;
176
177   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
178     {
179       if (unformat (line_input, "default"))
180         nat_set_alloc_addr_and_port_default ();
181       else
182         if (unformat
183             (line_input, "map-e psid %d psid-offset %d psid-len %d", &psid,
184              &psid_offset, &psid_length))
185         nat_set_alloc_addr_and_port_mape ((u16) psid, (u16) psid_offset,
186                                           (u16) psid_length);
187       else
188         {
189           error = clib_error_return (0, "unknown input '%U'",
190                                      format_unformat_error, line_input);
191           goto done;
192         }
193     }
194
195 done:
196   unformat_free (line_input);
197
198   return error;
199 };
200
201 static clib_error_t *
202 add_address_command_fn (vlib_main_t * vm,
203                         unformat_input_t * input, vlib_cli_command_t * cmd)
204 {
205   unformat_input_t _line_input, *line_input = &_line_input;
206   snat_main_t *sm = &snat_main;
207   ip4_address_t start_addr, end_addr, this_addr;
208   u32 start_host_order, end_host_order;
209   u32 vrf_id = ~0;
210   int i, count;
211   int is_add = 1;
212   int rv = 0;
213   clib_error_t *error = 0;
214   u8 twice_nat = 0;
215
216   if (sm->deterministic)
217     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
218
219   /* Get a line of input. */
220   if (!unformat_user (input, unformat_line_input, line_input))
221     return 0;
222
223   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
224     {
225       if (unformat (line_input, "%U - %U",
226                     unformat_ip4_address, &start_addr,
227                     unformat_ip4_address, &end_addr))
228         ;
229       else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
230         ;
231       else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
232         end_addr = start_addr;
233       else if (unformat (line_input, "twice-nat"))
234         twice_nat = 1;
235       else if (unformat (line_input, "del"))
236         is_add = 0;
237       else
238         {
239           error = clib_error_return (0, "unknown input '%U'",
240                                      format_unformat_error, line_input);
241           goto done;
242         }
243     }
244
245   if (sm->static_mapping_only)
246     {
247       error = clib_error_return (0, "static mapping only mode");
248       goto done;
249     }
250
251   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
252   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
253
254   if (end_host_order < start_host_order)
255     {
256       error = clib_error_return (0, "end address less than start address");
257       goto done;
258     }
259
260   count = (end_host_order - start_host_order) + 1;
261
262   if (count > 1024)
263     clib_warning ("%U - %U, %d addresses...",
264                   format_ip4_address, &start_addr,
265                   format_ip4_address, &end_addr, count);
266
267   this_addr = start_addr;
268
269   for (i = 0; i < count; i++)
270     {
271       if (is_add)
272         snat_add_address (sm, &this_addr, vrf_id, twice_nat);
273       else
274         rv = snat_del_address (sm, this_addr, 0, twice_nat);
275
276       switch (rv)
277         {
278         case VNET_API_ERROR_NO_SUCH_ENTRY:
279           error = clib_error_return (0, "S-NAT address not exist.");
280           goto done;
281         case VNET_API_ERROR_UNSPECIFIED:
282           error =
283             clib_error_return (0, "S-NAT address used in static mapping.");
284           goto done;
285         default:
286           break;
287         }
288
289       if (sm->out2in_dpo)
290         nat44_add_del_address_dpo (this_addr, is_add);
291
292       increment_v4_address (&this_addr);
293     }
294
295 done:
296   unformat_free (line_input);
297
298   return error;
299 }
300
301 static clib_error_t *
302 nat44_show_addresses_command_fn (vlib_main_t * vm, unformat_input_t * input,
303                                  vlib_cli_command_t * cmd)
304 {
305   snat_main_t *sm = &snat_main;
306   snat_address_t *ap;
307
308   if (sm->deterministic)
309     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
310
311   vlib_cli_output (vm, "NAT44 pool addresses:");
312   /* *INDENT-OFF* */
313   vec_foreach (ap, sm->addresses)
314     {
315       vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
316       if (ap->fib_index != ~0)
317           vlib_cli_output (vm, "  tenant VRF: %u",
318             fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
319       else
320         vlib_cli_output (vm, "  tenant VRF independent");
321     #define _(N, i, n, s) \
322       vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
323       foreach_snat_protocol
324     #undef _
325     }
326   vlib_cli_output (vm, "NAT44 twice-nat pool addresses:");
327   vec_foreach (ap, sm->twice_nat_addresses)
328     {
329       vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
330       if (ap->fib_index != ~0)
331           vlib_cli_output (vm, "  tenant VRF: %u",
332             fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
333       else
334         vlib_cli_output (vm, "  tenant VRF independent");
335     #define _(N, i, n, s) \
336       vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
337       foreach_snat_protocol
338     #undef _
339     }
340   /* *INDENT-ON* */
341   return 0;
342 }
343
344 static clib_error_t *
345 snat_feature_command_fn (vlib_main_t * vm,
346                          unformat_input_t * input, vlib_cli_command_t * cmd)
347 {
348   unformat_input_t _line_input, *line_input = &_line_input;
349   vnet_main_t *vnm = vnet_get_main ();
350   clib_error_t *error = 0;
351   u32 sw_if_index;
352   u32 *inside_sw_if_indices = 0;
353   u32 *outside_sw_if_indices = 0;
354   u8 is_output_feature = 0;
355   int is_del = 0;
356   int i;
357
358   sw_if_index = ~0;
359
360   /* Get a line of input. */
361   if (!unformat_user (input, unformat_line_input, line_input))
362     return 0;
363
364   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
365     {
366       if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
367                     vnm, &sw_if_index))
368         vec_add1 (inside_sw_if_indices, sw_if_index);
369       else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
370                          vnm, &sw_if_index))
371         vec_add1 (outside_sw_if_indices, sw_if_index);
372       else if (unformat (line_input, "output-feature"))
373         is_output_feature = 1;
374       else if (unformat (line_input, "del"))
375         is_del = 1;
376       else
377         {
378           error = clib_error_return (0, "unknown input '%U'",
379                                      format_unformat_error, line_input);
380           goto done;
381         }
382     }
383
384   if (vec_len (inside_sw_if_indices))
385     {
386       for (i = 0; i < vec_len (inside_sw_if_indices); i++)
387         {
388           sw_if_index = inside_sw_if_indices[i];
389           if (is_output_feature)
390             {
391               if (snat_interface_add_del_output_feature
392                   (sw_if_index, 1, is_del))
393                 {
394                   error = clib_error_return (0, "%s %U failed",
395                                              is_del ? "del" : "add",
396                                              format_vnet_sw_if_index_name,
397                                              vnm, sw_if_index);
398                   goto done;
399                 }
400             }
401           else
402             {
403               if (snat_interface_add_del (sw_if_index, 1, is_del))
404                 {
405                   error = clib_error_return (0, "%s %U failed",
406                                              is_del ? "del" : "add",
407                                              format_vnet_sw_if_index_name,
408                                              vnm, sw_if_index);
409                   goto done;
410                 }
411             }
412         }
413     }
414
415   if (vec_len (outside_sw_if_indices))
416     {
417       for (i = 0; i < vec_len (outside_sw_if_indices); i++)
418         {
419           sw_if_index = outside_sw_if_indices[i];
420           if (is_output_feature)
421             {
422               if (snat_interface_add_del_output_feature
423                   (sw_if_index, 0, is_del))
424                 {
425                   error = clib_error_return (0, "%s %U failed",
426                                              is_del ? "del" : "add",
427                                              format_vnet_sw_if_index_name,
428                                              vnm, sw_if_index);
429                   goto done;
430                 }
431             }
432           else
433             {
434               if (snat_interface_add_del (sw_if_index, 0, is_del))
435                 {
436                   error = clib_error_return (0, "%s %U failed",
437                                              is_del ? "del" : "add",
438                                              format_vnet_sw_if_index_name,
439                                              vnm, sw_if_index);
440                   goto done;
441                 }
442             }
443         }
444     }
445
446 done:
447   unformat_free (line_input);
448   vec_free (inside_sw_if_indices);
449   vec_free (outside_sw_if_indices);
450
451   return error;
452 }
453
454 static clib_error_t *
455 nat44_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
456                                   vlib_cli_command_t * cmd)
457 {
458   snat_main_t *sm = &snat_main;
459   snat_interface_t *i;
460   vnet_main_t *vnm = vnet_get_main ();
461
462   vlib_cli_output (vm, "NAT44 interfaces:");
463   /* *INDENT-OFF* */
464   pool_foreach (i, sm->interfaces,
465   ({
466     vlib_cli_output (vm, " %U %s", format_vnet_sw_if_index_name, vnm,
467                      i->sw_if_index,
468                      (nat_interface_is_inside(i) &&
469                       nat_interface_is_outside(i)) ? "in out" :
470                      (nat_interface_is_inside(i) ? "in" : "out"));
471   }));
472
473   pool_foreach (i, sm->output_feature_interfaces,
474   ({
475     vlib_cli_output (vm, " %U output-feature %s",
476                      format_vnet_sw_if_index_name, vnm,
477                      i->sw_if_index,
478                      (nat_interface_is_inside(i) &&
479                       nat_interface_is_outside(i)) ? "in out" :
480                      (nat_interface_is_inside(i) ? "in" : "out"));
481   }));
482   /* *INDENT-ON* */
483
484   return 0;
485 }
486
487 static clib_error_t *
488 add_static_mapping_command_fn (vlib_main_t * vm,
489                                unformat_input_t * input,
490                                vlib_cli_command_t * cmd)
491 {
492   unformat_input_t _line_input, *line_input = &_line_input;
493   snat_main_t *sm = &snat_main;
494   clib_error_t *error = 0;
495   ip4_address_t l_addr, e_addr;
496   u32 l_port = 0, e_port = 0, vrf_id = ~0;
497   int is_add = 1;
498   int addr_only = 1;
499   u32 sw_if_index = ~0;
500   vnet_main_t *vnm = vnet_get_main ();
501   int rv;
502   snat_protocol_t proto = ~0;
503   u8 proto_set = 0;
504   u8 twice_nat = 0;
505   u8 out2in_only = 0;
506
507   if (sm->deterministic)
508     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
509
510   /* Get a line of input. */
511   if (!unformat_user (input, unformat_line_input, line_input))
512     return 0;
513
514   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
515     {
516       if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
517                     &l_port))
518         addr_only = 0;
519       else
520         if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
521         ;
522       else if (unformat (line_input, "external %U %u", unformat_ip4_address,
523                          &e_addr, &e_port))
524         addr_only = 0;
525       else if (unformat (line_input, "external %U", unformat_ip4_address,
526                          &e_addr))
527         ;
528       else if (unformat (line_input, "external %U %u",
529                          unformat_vnet_sw_interface, vnm, &sw_if_index,
530                          &e_port))
531         addr_only = 0;
532
533       else if (unformat (line_input, "external %U",
534                          unformat_vnet_sw_interface, vnm, &sw_if_index))
535         ;
536       else if (unformat (line_input, "vrf %u", &vrf_id))
537         ;
538       else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
539         proto_set = 1;
540       else if (unformat (line_input, "twice-nat"))
541         twice_nat = 1;
542       else if (unformat (line_input, "out2in-only"))
543         out2in_only = 1;
544       else if (unformat (line_input, "del"))
545         is_add = 0;
546       else
547         {
548           error = clib_error_return (0, "unknown input: '%U'",
549                                      format_unformat_error, line_input);
550           goto done;
551         }
552     }
553
554   if (twice_nat && addr_only)
555     {
556       error = clib_error_return (0, "twice NAT only for 1:1 NAPT");
557       goto done;
558     }
559
560   if (!addr_only && !proto_set)
561     {
562       error = clib_error_return (0, "missing protocol");
563       goto done;
564     }
565
566   rv = snat_add_static_mapping (l_addr, e_addr, (u16) l_port, (u16) e_port,
567                                 vrf_id, addr_only, sw_if_index, proto, is_add,
568                                 twice_nat, out2in_only, 0);
569
570   switch (rv)
571     {
572     case VNET_API_ERROR_INVALID_VALUE:
573       error = clib_error_return (0, "External port already in use.");
574       goto done;
575     case VNET_API_ERROR_NO_SUCH_ENTRY:
576       if (is_add)
577         error = clib_error_return (0, "External addres must be allocated.");
578       else
579         error = clib_error_return (0, "Mapping not exist.");
580       goto done;
581     case VNET_API_ERROR_NO_SUCH_FIB:
582       error = clib_error_return (0, "No such VRF id.");
583       goto done;
584     case VNET_API_ERROR_VALUE_EXIST:
585       error = clib_error_return (0, "Mapping already exist.");
586       goto done;
587     default:
588       break;
589     }
590
591 done:
592   unformat_free (line_input);
593
594   return error;
595 }
596
597 static clib_error_t *
598 add_identity_mapping_command_fn (vlib_main_t * vm,
599                                  unformat_input_t * input,
600                                  vlib_cli_command_t * cmd)
601 {
602   unformat_input_t _line_input, *line_input = &_line_input;
603   snat_main_t *sm = &snat_main;
604   clib_error_t *error = 0;
605   ip4_address_t addr;
606   u32 port = 0, vrf_id = ~0;
607   int is_add = 1;
608   int addr_only = 1;
609   u32 sw_if_index = ~0;
610   vnet_main_t *vnm = vnet_get_main ();
611   int rv;
612   snat_protocol_t proto;
613
614   if (sm->deterministic)
615     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
616
617   addr.as_u32 = 0;
618
619   /* Get a line of input. */
620   if (!unformat_user (input, unformat_line_input, line_input))
621     return 0;
622
623   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
624     {
625       if (unformat (line_input, "%U", unformat_ip4_address, &addr))
626         ;
627       else if (unformat (line_input, "external %U",
628                          unformat_vnet_sw_interface, vnm, &sw_if_index))
629         ;
630       else if (unformat (line_input, "vrf %u", &vrf_id))
631         ;
632       else if (unformat (line_input, "%U %u", unformat_snat_protocol, &proto,
633                          &port))
634         addr_only = 0;
635       else if (unformat (line_input, "del"))
636         is_add = 0;
637       else
638         {
639           error = clib_error_return (0, "unknown input: '%U'",
640                                      format_unformat_error, line_input);
641           goto done;
642         }
643     }
644
645   rv = snat_add_static_mapping (addr, addr, (u16) port, (u16) port,
646                                 vrf_id, addr_only, sw_if_index, proto, is_add,
647                                 0, 0, 0);
648
649   switch (rv)
650     {
651     case VNET_API_ERROR_INVALID_VALUE:
652       error = clib_error_return (0, "External port already in use.");
653       goto done;
654     case VNET_API_ERROR_NO_SUCH_ENTRY:
655       if (is_add)
656         error = clib_error_return (0, "External addres must be allocated.");
657       else
658         error = clib_error_return (0, "Mapping not exist.");
659       goto done;
660     case VNET_API_ERROR_NO_SUCH_FIB:
661       error = clib_error_return (0, "No such VRF id.");
662       goto done;
663     case VNET_API_ERROR_VALUE_EXIST:
664       error = clib_error_return (0, "Mapping already exist.");
665       goto done;
666     default:
667       break;
668     }
669
670 done:
671   unformat_free (line_input);
672
673   return error;
674 }
675
676 static clib_error_t *
677 add_lb_static_mapping_command_fn (vlib_main_t * vm,
678                                   unformat_input_t * input,
679                                   vlib_cli_command_t * cmd)
680 {
681   unformat_input_t _line_input, *line_input = &_line_input;
682   snat_main_t *sm = &snat_main;
683   clib_error_t *error = 0;
684   ip4_address_t l_addr, e_addr;
685   u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
686   int is_add = 1;
687   int rv;
688   snat_protocol_t proto;
689   u8 proto_set = 0;
690   nat44_lb_addr_port_t *locals = 0, local;
691   u8 twice_nat = 0;
692   u8 out2in_only = 0;
693
694   if (sm->deterministic)
695     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
696
697   /* Get a line of input. */
698   if (!unformat_user (input, unformat_line_input, line_input))
699     return 0;
700
701   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
702     {
703       if (unformat (line_input, "local %U:%u probability %u",
704                     unformat_ip4_address, &l_addr, &l_port, &probability))
705         {
706           memset (&local, 0, sizeof (local));
707           local.addr = l_addr;
708           local.port = (u16) l_port;
709           local.probability = (u8) probability;
710           vec_add1 (locals, local);
711         }
712       else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
713                          &e_addr, &e_port))
714         ;
715       else if (unformat (line_input, "vrf %u", &vrf_id))
716         ;
717       else if (unformat (line_input, "protocol %U", unformat_snat_protocol,
718                          &proto))
719         proto_set = 1;
720       else if (unformat (line_input, "twice-nat"))
721         twice_nat = 1;
722       else if (unformat (line_input, "out2in-only"))
723         out2in_only = 1;
724       else if (unformat (line_input, "del"))
725         is_add = 0;
726       else
727         {
728           error = clib_error_return (0, "unknown input: '%U'",
729                                      format_unformat_error, line_input);
730           goto done;
731         }
732     }
733
734   if (vec_len (locals) < 2)
735     {
736       error = clib_error_return (0, "at least two local must be set");
737       goto done;
738     }
739
740   if (!proto_set)
741     {
742       error = clib_error_return (0, "missing protocol");
743       goto done;
744     }
745
746   rv = nat44_add_del_lb_static_mapping (e_addr, (u16) e_port, proto, vrf_id,
747                                         locals, is_add, twice_nat,
748                                         out2in_only, 0);
749
750   switch (rv)
751     {
752     case VNET_API_ERROR_INVALID_VALUE:
753       error = clib_error_return (0, "External port already in use.");
754       goto done;
755     case VNET_API_ERROR_NO_SUCH_ENTRY:
756       if (is_add)
757         error = clib_error_return (0, "External addres must be allocated.");
758       else
759         error = clib_error_return (0, "Mapping not exist.");
760       goto done;
761     case VNET_API_ERROR_VALUE_EXIST:
762       error = clib_error_return (0, "Mapping already exist.");
763       goto done;
764     default:
765       break;
766     }
767
768 done:
769   unformat_free (line_input);
770   vec_free (locals);
771
772   return error;
773 }
774
775 static clib_error_t *
776 nat44_show_static_mappings_command_fn (vlib_main_t * vm,
777                                        unformat_input_t * input,
778                                        vlib_cli_command_t * cmd)
779 {
780   snat_main_t *sm = &snat_main;
781   snat_static_mapping_t *m;
782   snat_static_map_resolve_t *rp;
783
784   if (sm->deterministic)
785     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
786
787   vlib_cli_output (vm, "NAT44 static mappings:");
788   /* *INDENT-OFF* */
789   pool_foreach (m, sm->static_mappings,
790   ({
791     vlib_cli_output (vm, " %U", format_snat_static_mapping, m);
792   }));
793   vec_foreach (rp, sm->to_resolve)
794     vlib_cli_output (vm, " %U", format_snat_static_map_to_resolve, rp);
795   /* *INDENT-ON* */
796
797   return 0;
798 }
799
800 static clib_error_t *
801 snat_add_interface_address_command_fn (vlib_main_t * vm,
802                                        unformat_input_t * input,
803                                        vlib_cli_command_t * cmd)
804 {
805   snat_main_t *sm = &snat_main;
806   unformat_input_t _line_input, *line_input = &_line_input;
807   u32 sw_if_index;
808   int rv;
809   int is_del = 0;
810   clib_error_t *error = 0;
811   u8 twice_nat = 0;
812
813   if (sm->deterministic)
814     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
815
816   /* Get a line of input. */
817   if (!unformat_user (input, unformat_line_input, line_input))
818     return 0;
819
820   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
821     {
822       if (unformat (line_input, "%U", unformat_vnet_sw_interface,
823                     sm->vnet_main, &sw_if_index))
824         ;
825       else if (unformat (line_input, "twice-nat"))
826         twice_nat = 1;
827       else if (unformat (line_input, "del"))
828         is_del = 1;
829       else
830         {
831           error = clib_error_return (0, "unknown input '%U'",
832                                      format_unformat_error, line_input);
833           goto done;
834         }
835     }
836
837   rv = snat_add_interface_address (sm, sw_if_index, is_del, twice_nat);
838
839   switch (rv)
840     {
841     case 0:
842       break;
843
844     default:
845       error = clib_error_return (0, "snat_add_interface_address returned %d",
846                                  rv);
847       goto done;
848     }
849
850 done:
851   unformat_free (line_input);
852
853   return error;
854 }
855
856 static clib_error_t *
857 nat44_show_interface_address_command_fn (vlib_main_t * vm,
858                                          unformat_input_t * input,
859                                          vlib_cli_command_t * cmd)
860 {
861   snat_main_t *sm = &snat_main;
862   vnet_main_t *vnm = vnet_get_main ();
863   u32 *sw_if_index;
864
865   if (sm->deterministic)
866     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
867
868   /* *INDENT-OFF* */
869   vlib_cli_output (vm, "NAT44 pool address interfaces:");
870   vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
871     {
872       vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
873                        *sw_if_index);
874     }
875   vlib_cli_output (vm, "NAT44 twice-nat pool address interfaces:");
876   vec_foreach (sw_if_index, sm->auto_add_sw_if_indices_twice_nat)
877     {
878       vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
879                        *sw_if_index);
880     }
881   /* *INDENT-ON* */
882
883   return 0;
884 }
885
886 static clib_error_t *
887 nat44_show_sessions_command_fn (vlib_main_t * vm, unformat_input_t * input,
888                                 vlib_cli_command_t * cmd)
889 {
890   int verbose = 0;
891   snat_main_t *sm = &snat_main;
892   snat_main_per_thread_data_t *tsm;
893   snat_user_t *u;
894   int i = 0;
895
896   if (sm->deterministic)
897     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
898
899   if (unformat (input, "detail"))
900     verbose = 1;
901
902   vlib_cli_output (vm, "NAT44 sessions:");
903
904   /* *INDENT-OFF* */
905   vec_foreach_index (i, sm->per_thread_data)
906     {
907       tsm = vec_elt_at_index (sm->per_thread_data, i);
908
909       pool_foreach (u, tsm->users,
910       ({
911         vlib_cli_output (vm, "  %U", format_snat_user, tsm, u, verbose);
912       }));
913     }
914   /* *INDENT-ON* */
915
916   return 0;
917 }
918
919 static clib_error_t *
920 nat44_del_session_command_fn (vlib_main_t * vm,
921                               unformat_input_t * input,
922                               vlib_cli_command_t * cmd)
923 {
924   snat_main_t *sm = &snat_main;
925   unformat_input_t _line_input, *line_input = &_line_input;
926   int is_in = 0;
927   clib_error_t *error = 0;
928   ip4_address_t addr;
929   u32 port = 0, vrf_id = sm->outside_vrf_id;
930   snat_protocol_t proto;
931   int rv;
932
933   if (sm->deterministic)
934     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
935
936   /* Get a line of input. */
937   if (!unformat_user (input, unformat_line_input, line_input))
938     return 0;
939
940   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
941     {
942       if (unformat
943           (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
944            unformat_snat_protocol, &proto))
945         ;
946       else if (unformat (line_input, "in"))
947         {
948           is_in = 1;
949           vrf_id = sm->inside_vrf_id;
950         }
951       else if (unformat (line_input, "vrf %u", &vrf_id))
952         ;
953       else
954         {
955           error = clib_error_return (0, "unknown input '%U'",
956                                      format_unformat_error, line_input);
957           goto done;
958         }
959     }
960
961   rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
962
963   switch (rv)
964     {
965     case 0:
966       break;
967
968     default:
969       error = clib_error_return (0, "nat44_del_session returned %d", rv);
970       goto done;
971     }
972
973 done:
974   unformat_free (line_input);
975
976   return error;
977 }
978
979 static clib_error_t *
980 snat_forwarding_set_command_fn (vlib_main_t * vm,
981                                 unformat_input_t * input,
982                                 vlib_cli_command_t * cmd)
983 {
984   snat_main_t *sm = &snat_main;
985   unformat_input_t _line_input, *line_input = &_line_input;
986   u8 forwarding_enable;
987   u8 forwarding_enable_set = 0;
988   clib_error_t *error = 0;
989
990   if (sm->deterministic)
991     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
992
993   /* Get a line of input. */
994   if (!unformat_user (input, unformat_line_input, line_input))
995     return clib_error_return (0, "'enable' or 'disable' expected");
996
997   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
998     {
999       if (!forwarding_enable_set && unformat (line_input, "enable"))
1000         {
1001           forwarding_enable = 1;
1002           forwarding_enable_set = 1;
1003         }
1004       else if (!forwarding_enable_set && unformat (line_input, "disable"))
1005         {
1006           forwarding_enable = 0;
1007           forwarding_enable_set = 1;
1008         }
1009       else
1010         {
1011           error = clib_error_return (0, "unknown input '%U'",
1012                                      format_unformat_error, line_input);
1013           goto done;
1014         }
1015     }
1016
1017   if (!forwarding_enable_set)
1018     {
1019       error = clib_error_return (0, "'enable' or 'disable' expected");
1020       goto done;
1021     }
1022
1023   sm->forwarding_enabled = forwarding_enable;
1024
1025 done:
1026   unformat_free (line_input);
1027
1028   return error;
1029 }
1030
1031 static clib_error_t *
1032 snat_det_map_command_fn (vlib_main_t * vm,
1033                          unformat_input_t * input, vlib_cli_command_t * cmd)
1034 {
1035   snat_main_t *sm = &snat_main;
1036   unformat_input_t _line_input, *line_input = &_line_input;
1037   ip4_address_t in_addr, out_addr;
1038   u32 in_plen, out_plen;
1039   int is_add = 1, rv;
1040   clib_error_t *error = 0;
1041
1042   if (!sm->deterministic)
1043     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1044
1045   /* Get a line of input. */
1046   if (!unformat_user (input, unformat_line_input, line_input))
1047     return 0;
1048
1049   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1050     {
1051       if (unformat
1052           (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
1053         ;
1054       else
1055         if (unformat
1056             (line_input, "out %U/%u", unformat_ip4_address, &out_addr,
1057              &out_plen))
1058         ;
1059       else if (unformat (line_input, "del"))
1060         is_add = 0;
1061       else
1062         {
1063           error = clib_error_return (0, "unknown input '%U'",
1064                                      format_unformat_error, line_input);
1065           goto done;
1066         }
1067     }
1068
1069   rv = snat_det_add_map (sm, &in_addr, (u8) in_plen, &out_addr, (u8) out_plen,
1070                          is_add);
1071
1072   if (rv)
1073     {
1074       error = clib_error_return (0, "snat_det_add_map return %d", rv);
1075       goto done;
1076     }
1077
1078 done:
1079   unformat_free (line_input);
1080
1081   return error;
1082 }
1083
1084 static clib_error_t *
1085 nat44_det_show_mappings_command_fn (vlib_main_t * vm,
1086                                     unformat_input_t * input,
1087                                     vlib_cli_command_t * cmd)
1088 {
1089   snat_main_t *sm = &snat_main;
1090   snat_det_map_t *dm;
1091
1092   if (!sm->deterministic)
1093     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1094
1095   vlib_cli_output (vm, "NAT44 deterministic mappings:");
1096   /* *INDENT-OFF* */
1097   pool_foreach (dm, sm->det_maps,
1098   ({
1099     vlib_cli_output (vm, " in %U/%d out %U/%d\n",
1100                      format_ip4_address, &dm->in_addr, dm->in_plen,
1101                      format_ip4_address, &dm->out_addr, dm->out_plen);
1102     vlib_cli_output (vm, "  outside address sharing ratio: %d\n",
1103                      dm->sharing_ratio);
1104     vlib_cli_output (vm, "  number of ports per inside host: %d\n",
1105                      dm->ports_per_host);
1106     vlib_cli_output (vm, "  sessions number: %d\n", dm->ses_num);
1107   }));
1108   /* *INDENT-ON* */
1109
1110   return 0;
1111 }
1112
1113 static clib_error_t *
1114 snat_det_forward_command_fn (vlib_main_t * vm,
1115                              unformat_input_t * input,
1116                              vlib_cli_command_t * cmd)
1117 {
1118   snat_main_t *sm = &snat_main;
1119   unformat_input_t _line_input, *line_input = &_line_input;
1120   ip4_address_t in_addr, out_addr;
1121   u16 lo_port;
1122   snat_det_map_t *dm;
1123   clib_error_t *error = 0;
1124
1125   if (!sm->deterministic)
1126     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1127
1128   /* Get a line of input. */
1129   if (!unformat_user (input, unformat_line_input, line_input))
1130     return 0;
1131
1132   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1133     {
1134       if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
1135         ;
1136       else
1137         {
1138           error = clib_error_return (0, "unknown input '%U'",
1139                                      format_unformat_error, line_input);
1140           goto done;
1141         }
1142     }
1143
1144   dm = snat_det_map_by_user (sm, &in_addr);
1145   if (!dm)
1146     vlib_cli_output (vm, "no match");
1147   else
1148     {
1149       snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
1150       vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
1151                        lo_port, lo_port + dm->ports_per_host - 1);
1152     }
1153
1154 done:
1155   unformat_free (line_input);
1156
1157   return error;
1158 }
1159
1160 static clib_error_t *
1161 snat_det_reverse_command_fn (vlib_main_t * vm,
1162                              unformat_input_t * input,
1163                              vlib_cli_command_t * cmd)
1164 {
1165   snat_main_t *sm = &snat_main;
1166   unformat_input_t _line_input, *line_input = &_line_input;
1167   ip4_address_t in_addr, out_addr;
1168   u32 out_port;
1169   snat_det_map_t *dm;
1170   clib_error_t *error = 0;
1171
1172   if (!sm->deterministic)
1173     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1174
1175   /* Get a line of input. */
1176   if (!unformat_user (input, unformat_line_input, line_input))
1177     return 0;
1178
1179   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1180     {
1181       if (unformat
1182           (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
1183         ;
1184       else
1185         {
1186           error = clib_error_return (0, "unknown input '%U'",
1187                                      format_unformat_error, line_input);
1188           goto done;
1189         }
1190     }
1191
1192   if (out_port < 1024 || out_port > 65535)
1193     {
1194       error = clib_error_return (0, "wrong port, must be <1024-65535>");
1195       goto done;
1196     }
1197
1198   dm = snat_det_map_by_out (sm, &out_addr);
1199   if (!dm)
1200     vlib_cli_output (vm, "no match");
1201   else
1202     {
1203       snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
1204       vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
1205     }
1206
1207 done:
1208   unformat_free (line_input);
1209
1210   return error;
1211 }
1212
1213 static clib_error_t *
1214 set_timeout_command_fn (vlib_main_t * vm,
1215                         unformat_input_t * input, vlib_cli_command_t * cmd)
1216 {
1217   snat_main_t *sm = &snat_main;
1218   unformat_input_t _line_input, *line_input = &_line_input;
1219   clib_error_t *error = 0;
1220
1221   if (!sm->deterministic)
1222     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1223
1224   /* Get a line of input. */
1225   if (!unformat_user (input, unformat_line_input, line_input))
1226     return 0;
1227
1228   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1229     {
1230       if (unformat (line_input, "udp %u", &sm->udp_timeout))
1231         ;
1232       else if (unformat (line_input, "tcp-established %u",
1233                          &sm->tcp_established_timeout))
1234         ;
1235       else if (unformat (line_input, "tcp-transitory %u",
1236                          &sm->tcp_transitory_timeout))
1237         ;
1238       else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
1239         ;
1240       else if (unformat (line_input, "reset"))
1241         {
1242           sm->udp_timeout = SNAT_UDP_TIMEOUT;
1243           sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1244           sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1245           sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1246         }
1247       else
1248         {
1249           error = clib_error_return (0, "unknown input '%U'",
1250                                      format_unformat_error, line_input);
1251           goto done;
1252         }
1253     }
1254
1255 done:
1256   unformat_free (line_input);
1257
1258   return error;
1259 }
1260
1261 static clib_error_t *
1262 nat44_det_show_timeouts_command_fn (vlib_main_t * vm,
1263                                     unformat_input_t * input,
1264                                     vlib_cli_command_t * cmd)
1265 {
1266   snat_main_t *sm = &snat_main;
1267
1268   if (!sm->deterministic)
1269     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1270
1271   vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
1272   vlib_cli_output (vm, "tcp-established timeout: %dsec",
1273                    sm->tcp_established_timeout);
1274   vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
1275                    sm->tcp_transitory_timeout);
1276   vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
1277
1278   return 0;
1279 }
1280
1281 static clib_error_t *
1282 nat44_det_show_sessions_command_fn (vlib_main_t * vm,
1283                                     unformat_input_t * input,
1284                                     vlib_cli_command_t * cmd)
1285 {
1286   snat_main_t *sm = &snat_main;
1287   snat_det_map_t *dm;
1288   snat_det_session_t *ses;
1289   int i;
1290
1291   if (!sm->deterministic)
1292     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1293
1294   vlib_cli_output (vm, "NAT44 deterministic sessions:");
1295   /* *INDENT-OFF* */
1296   pool_foreach (dm, sm->det_maps,
1297   ({
1298     vec_foreach_index (i, dm->sessions)
1299       {
1300         ses = vec_elt_at_index (dm->sessions, i);
1301         if (ses->in_port)
1302           vlib_cli_output (vm, "  %U", format_det_map_ses, dm, ses, &i);
1303       }
1304   }));
1305   /* *INDENT-ON* */
1306   return 0;
1307 }
1308
1309 static clib_error_t *
1310 snat_det_close_session_out_fn (vlib_main_t * vm,
1311                                unformat_input_t * input,
1312                                vlib_cli_command_t * cmd)
1313 {
1314   snat_main_t *sm = &snat_main;
1315   unformat_input_t _line_input, *line_input = &_line_input;
1316   ip4_address_t out_addr, ext_addr, in_addr;
1317   u32 out_port, ext_port;
1318   snat_det_map_t *dm;
1319   snat_det_session_t *ses;
1320   snat_det_out_key_t key;
1321   clib_error_t *error = 0;
1322
1323   if (!sm->deterministic)
1324     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1325
1326   /* Get a line of input. */
1327   if (!unformat_user (input, unformat_line_input, line_input))
1328     return 0;
1329
1330   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1331     {
1332       if (unformat (line_input, "%U:%d %U:%d",
1333                     unformat_ip4_address, &out_addr, &out_port,
1334                     unformat_ip4_address, &ext_addr, &ext_port))
1335         ;
1336       else
1337         {
1338           error = clib_error_return (0, "unknown input '%U'",
1339                                      format_unformat_error, line_input);
1340           goto done;
1341         }
1342     }
1343
1344   unformat_free (line_input);
1345
1346   dm = snat_det_map_by_out (sm, &out_addr);
1347   if (!dm)
1348     vlib_cli_output (vm, "no match");
1349   else
1350     {
1351       snat_det_reverse (dm, &ext_addr, (u16) out_port, &in_addr);
1352       key.ext_host_addr = out_addr;
1353       key.ext_host_port = ntohs ((u16) ext_port);
1354       key.out_port = ntohs ((u16) out_port);
1355       ses = snat_det_get_ses_by_out (dm, &out_addr, key.as_u64);
1356       if (!ses)
1357         vlib_cli_output (vm, "no match");
1358       else
1359         snat_det_ses_close (dm, ses);
1360     }
1361
1362 done:
1363   unformat_free (line_input);
1364
1365   return error;
1366 }
1367
1368 static clib_error_t *
1369 snat_det_close_session_in_fn (vlib_main_t * vm,
1370                               unformat_input_t * input,
1371                               vlib_cli_command_t * cmd)
1372 {
1373   snat_main_t *sm = &snat_main;
1374   unformat_input_t _line_input, *line_input = &_line_input;
1375   ip4_address_t in_addr, ext_addr;
1376   u32 in_port, ext_port;
1377   snat_det_map_t *dm;
1378   snat_det_session_t *ses;
1379   snat_det_out_key_t key;
1380   clib_error_t *error = 0;
1381
1382   if (!sm->deterministic)
1383     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1384
1385   /* Get a line of input. */
1386   if (!unformat_user (input, unformat_line_input, line_input))
1387     return 0;
1388
1389   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1390     {
1391       if (unformat (line_input, "%U:%d %U:%d",
1392                     unformat_ip4_address, &in_addr, &in_port,
1393                     unformat_ip4_address, &ext_addr, &ext_port))
1394         ;
1395       else
1396         {
1397           error = clib_error_return (0, "unknown input '%U'",
1398                                      format_unformat_error, line_input);
1399           goto done;
1400         }
1401     }
1402
1403   unformat_free (line_input);
1404
1405   dm = snat_det_map_by_user (sm, &in_addr);
1406   if (!dm)
1407     vlib_cli_output (vm, "no match");
1408   else
1409     {
1410       key.ext_host_addr = ext_addr;
1411       key.ext_host_port = ntohs ((u16) ext_port);
1412       ses =
1413         snat_det_find_ses_by_in (dm, &in_addr, ntohs ((u16) in_port), key);
1414       if (!ses)
1415         vlib_cli_output (vm, "no match");
1416       else
1417         snat_det_ses_close (dm, ses);
1418     }
1419
1420 done:
1421   unformat_free (line_input);
1422
1423   return error;
1424 }
1425 /* *INDENT-OFF* */
1426
1427 /*?
1428  * @cliexpar
1429  * @cliexstart{set snat workers}
1430  * Set NAT workers if 2 or more workers available, use:
1431  *  vpp# set snat workers 0-2,5
1432  * @cliexend
1433 ?*/
1434 VLIB_CLI_COMMAND (set_workers_command, static) = {
1435   .path = "set nat workers",
1436   .function = set_workers_command_fn,
1437   .short_help = "set nat workers <workers-list>",
1438 };
1439
1440 /*?
1441  * @cliexpar
1442  * @cliexstart{show nat workers}
1443  * Show NAT workers.
1444  *  vpp# show nat workers:
1445  *  2 workers
1446  *    vpp_wk_0
1447  *    vpp_wk_1
1448  * @cliexend
1449 ?*/
1450 VLIB_CLI_COMMAND (nat_show_workers_command, static) = {
1451   .path = "show nat workers",
1452   .short_help = "show nat workers",
1453   .function = nat_show_workers_commnad_fn,
1454 };
1455
1456 /*?
1457  * @cliexpar
1458  * @cliexstart{snat ipfix logging}
1459  * To enable NAT IPFIX logging use:
1460  *  vpp# nat ipfix logging
1461  * To set IPFIX exporter use:
1462  *  vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1463  * @cliexend
1464 ?*/
1465 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1466   .path = "nat ipfix logging",
1467   .function = snat_ipfix_logging_enable_disable_command_fn,
1468   .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1469 };
1470
1471 /*?
1472  * @cliexpar
1473  * @cliexstart{nat addr-port-assignment-alg}
1474  * Set address and port assignment algorithm
1475  * For the MAP-E CE limit port choice based on PSID use:
1476  *  vpp# nat addr-port-assignment-alg map-e psid 10 psid-offset 6 psid-len 6
1477  * To set standard (default) address and port assignment algorithm use:
1478  *  vpp# nat addr-port-assignment-alg default
1479  * @cliexend
1480 ?*/
1481 VLIB_CLI_COMMAND (nat44_set_alloc_addr_and_port_alg_command, static) = {
1482     .path = "nat addr-port-assignment-alg",
1483     .short_help = "nat addr-port-assignment-alg <alg-name> [<alg-params>]",
1484     .function = nat44_set_alloc_addr_and_port_alg_command_fn,
1485 };
1486
1487 /*?
1488  * @cliexpar
1489  * @cliexstart{nat44 add address}
1490  * Add/delete NAT44 pool address.
1491  * To add NAT44 pool address use:
1492  *  vpp# nat44 add address 172.16.1.3
1493  *  vpp# nat44 add address 172.16.2.2 - 172.16.2.24
1494  * To add NAT44 pool address for specific tenant (identified by VRF id) use:
1495  *  vpp# nat44 add address 172.16.1.3 tenant-vrf 10
1496  * @cliexend
1497 ?*/
1498 VLIB_CLI_COMMAND (add_address_command, static) = {
1499   .path = "nat44 add address",
1500   .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
1501                 "[tenant-vrf <vrf-id>] [twice-nat] [del]",
1502   .function = add_address_command_fn,
1503 };
1504
1505 /*?
1506  * @cliexpar
1507  * @cliexstart{show nat44 addresses}
1508  * Show NAT44 pool addresses.
1509  * vpp# show nat44 addresses
1510  * NAT44 pool addresses:
1511  * 172.16.2.2
1512  *   tenant VRF independent
1513  *   10 busy udp ports
1514  *   0 busy tcp ports
1515  *   0 busy icmp ports
1516  * 172.16.1.3
1517  *   tenant VRF: 10
1518  *   0 busy udp ports
1519  *   2 busy tcp ports
1520  *   0 busy icmp ports
1521  * NAT44 twice-nat pool addresses:
1522  * 10.20.30.72
1523  *   tenant VRF independent
1524  *   0 busy udp ports
1525  *   0 busy tcp ports
1526  *   0 busy icmp ports
1527  * @cliexend
1528 ?*/
1529 VLIB_CLI_COMMAND (nat44_show_addresses_command, static) = {
1530   .path = "show nat44 addresses",
1531   .short_help = "show nat44 addresses",
1532   .function = nat44_show_addresses_command_fn,
1533 };
1534
1535 /*?
1536  * @cliexpar
1537  * @cliexstart{set interface nat44}
1538  * Enable/disable NAT44 feature on the interface.
1539  * To enable NAT44 feature with local network interface use:
1540  *  vpp# set interface nat44 in GigabitEthernet0/8/0
1541  * To enable NAT44 feature with external network interface use:
1542  *  vpp# set interface nat44 out GigabitEthernet0/a/0
1543  * @cliexend
1544 ?*/
1545 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1546   .path = "set interface nat44",
1547   .function = snat_feature_command_fn,
1548   .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
1549                 "[del]",
1550 };
1551
1552 /*?
1553  * @cliexpar
1554  * @cliexstart{show nat44 interfaces}
1555  * Show interfaces with NAT44 feature.
1556  * vpp# show nat44 interfaces
1557  * NAT44 interfaces:
1558  *  GigabitEthernet0/8/0 in
1559  *  GigabitEthernet0/a/0 out
1560  * @cliexend
1561 ?*/
1562 VLIB_CLI_COMMAND (nat44_show_interfaces_command, static) = {
1563   .path = "show nat44 interfaces",
1564   .short_help = "show nat44 interfaces",
1565   .function = nat44_show_interfaces_command_fn,
1566 };
1567
1568 /*?
1569  * @cliexpar
1570  * @cliexstart{nat44 add static mapping}
1571  * Static mapping allows hosts on the external network to initiate connection
1572  * to to the local network host.
1573  * To create static mapping between local host address 10.0.0.3 port 6303 and
1574  * external address 4.4.4.4 port 3606 for TCP protocol use:
1575  *  vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
1576  * If not runnig "static mapping only" NAT plugin mode use before:
1577  *  vpp# nat44 add address 4.4.4.4
1578  * To create static mapping between local and external address use:
1579  *  vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
1580  * @cliexend
1581 ?*/
1582 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1583   .path = "nat44 add static mapping",
1584   .function = add_static_mapping_command_fn,
1585   .short_help =
1586     "nat44 add static mapping tcp|udp|icmp local <addr> [<port>] "
1587     "external <addr> [<port>] [vrf <table-id>] [twice-nat] [out2in-only] [del]",
1588 };
1589
1590 /*?
1591  * @cliexpar
1592  * @cliexstart{nat44 add identity mapping}
1593  * Identity mapping translate an IP address to itself.
1594  * To create identity mapping for address 10.0.0.3 port 6303 for TCP protocol
1595  * use:
1596  *  vpp# nat44 add identity mapping 10.0.0.3 tcp 6303
1597  * To create identity mapping for address 10.0.0.3 use:
1598  *  vpp# nat44 add identity mapping 10.0.0.3
1599  * To create identity mapping for DHCP addressed interface use:
1600  *  vpp# nat44 add identity mapping GigabitEthernet0/a/0 tcp 3606
1601  * @cliexend
1602 ?*/
1603 VLIB_CLI_COMMAND (add_identity_mapping_command, static) = {
1604   .path = "nat44 add identity mapping",
1605   .function = add_identity_mapping_command_fn,
1606   .short_help = "nat44 add identity mapping <interface>|<ip4-addr> "
1607     "[<protocol> <port>] [vrf <table-id>] [del]",
1608 };
1609
1610 /*?
1611  * @cliexpar
1612  * @cliexstart{nat44 add load-balancing static mapping}
1613  * Service load balancing using NAT44
1614  * To add static mapping with load balancing for service with external IP
1615  * address 1.2.3.4 and TCP port 80 and mapped to 2 local servers
1616  * 10.100.10.10:8080 and 10.100.10.20:8080 with probability 80% resp. 20% use:
1617  *  vpp# nat44 add load-balancing static mapping protocol tcp external 1.2.3.4:80 local 10.100.10.10:8080 probability 80 local 10.100.10.20:8080 probability 20
1618  * @cliexend
1619 ?*/
1620 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
1621   .path = "nat44 add load-balancing static mapping",
1622   .function = add_lb_static_mapping_command_fn,
1623   .short_help =
1624     "nat44 add load-balancing static mapping protocol tcp|udp "
1625     "external <addr>:<port> local <addr>:<port> probability <n> [twice-nat] "
1626     "[vrf <table-id>] [out2in-only] [del]",
1627 };
1628
1629 /*?
1630  * @cliexpar
1631  * @cliexstart{show nat44 static mappings}
1632  * Show NAT44 static mappings.
1633  * vpp# show nat44 static mappings
1634  * NAT44 static mappings:
1635  *  local 10.0.0.3 external 4.4.4.4 vrf 0
1636  *  tcp local 192.168.0.4:6303 external 4.4.4.3:3606 vrf 0
1637  *  tcp vrf 0 external 1.2.3.4:80  out2in-only
1638  *   local 10.100.10.10:8080 probability 80
1639  *   local 10.100.10.20:8080 probability 20
1640  *  tcp local 10.100.3.8:8080 external 169.10.10.1:80 vrf 0 twice-nat
1641  *  tcp local 10.0.0.10:3603 external GigabitEthernet0/a/0:6306 vrf 10
1642  * @cliexend
1643 ?*/
1644 VLIB_CLI_COMMAND (nat44_show_static_mappings_command, static) = {
1645   .path = "show nat44 static mappings",
1646   .short_help = "show nat44 static mappings",
1647   .function = nat44_show_static_mappings_command_fn,
1648 };
1649
1650 /*?
1651  * @cliexpar
1652  * @cliexstart{nat44 add interface address}
1653  * Use NAT44 pool address from specific interfce
1654  * To add NAT44 pool address from specific interface use:
1655  *  vpp# nat44 add interface address GigabitEthernet0/8/0
1656  * @cliexend
1657 ?*/
1658 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
1659     .path = "nat44 add interface address",
1660     .short_help = "nat44 add interface address <interface> [twice-nat] [del]",
1661     .function = snat_add_interface_address_command_fn,
1662 };
1663
1664 /*?
1665  * @cliexpar
1666  * @cliexstart{show nat44 interface address}
1667  * Show NAT44 pool address interfaces
1668  * vpp# show nat44 interface address
1669  * NAT44 pool address interfaces:
1670  *  GigabitEthernet0/a/0
1671  * NAT44 twice-nat pool address interfaces:
1672  *  GigabitEthernet0/8/0
1673  * @cliexend
1674 ?*/
1675 VLIB_CLI_COMMAND (nat44_show_interface_address_command, static) = {
1676   .path = "show nat44 interface address",
1677   .short_help = "show nat44 interface address",
1678   .function = nat44_show_interface_address_command_fn,
1679 };
1680
1681 /*?
1682  * @cliexpar
1683  * @cliexstart{show nat44 sessions}
1684  * Show NAT44 sessions.
1685  * @cliexend
1686 ?*/
1687 VLIB_CLI_COMMAND (nat44_show_sessions_command, static) = {
1688   .path = "show nat44 sessions",
1689   .short_help = "show nat44 sessions [detail]",
1690   .function = nat44_show_sessions_command_fn,
1691 };
1692
1693 /*?
1694  * @cliexpar
1695  * @cliexstart{nat44 del session}
1696  * To administratively delete NAT44 session by inside address and port use:
1697  *  vpp# nat44 del session in 10.0.0.3:6303 tcp
1698  * To administratively delete NAT44 session by outside address and port use:
1699  *  vpp# nat44 del session out 1.0.0.3:6033 udp
1700  * @cliexend
1701 ?*/
1702 VLIB_CLI_COMMAND (nat44_del_session_command, static) = {
1703     .path = "nat44 del session",
1704     .short_help = "nat44 del session in|out <addr>:<port> tcp|udp|icmp [vrf <id>]",
1705     .function = nat44_del_session_command_fn,
1706 };
1707
1708 /*?
1709  * @cliexpar
1710  * @cliexstart{nat44 forwarding}
1711  * Enable or disable forwarding
1712  * Forward packets which don't match existing translation
1713  * or static mapping instead of dropping them.
1714  * To enable forwarding, use:
1715  *  vpp# nat44 forwarding enable
1716  * To disable forwarding, use:
1717  *  vpp# nat44 forwarding disable
1718  * @cliexend
1719 ?*/
1720 VLIB_CLI_COMMAND (snat_forwarding_set_command, static) = {
1721   .path = "nat44 forwarding",
1722   .short_help = "nat44 forwarding enable|disable",
1723   .function = snat_forwarding_set_command_fn,
1724 };
1725
1726 /*?
1727  * @cliexpar
1728  * @cliexstart{nat44 deterministic add}
1729  * Create bijective mapping of inside address to outside address and port range
1730  * pairs, with the purpose of enabling deterministic NAT to reduce logging in
1731  * CGN deployments.
1732  * To create deterministic mapping between inside network 10.0.0.0/18 and
1733  * outside network 1.1.1.0/30 use:
1734  * # vpp# nat44 deterministic add in 10.0.0.0/18 out 1.1.1.0/30
1735  * @cliexend
1736 ?*/
1737 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
1738     .path = "nat44 deterministic add",
1739     .short_help = "nat44 deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
1740     .function = snat_det_map_command_fn,
1741 };
1742
1743 /*?
1744  * @cliexpar
1745  * @cliexpstart{show nat44 deterministic mappings}
1746  * Show NAT44 deterministic mappings
1747  * vpp# show nat44 deterministic mappings
1748  * NAT44 deterministic mappings:
1749  *  in 10.0.0.0/24 out 1.1.1.1/32
1750  *   outside address sharing ratio: 256
1751  *   number of ports per inside host: 252
1752  *   sessions number: 0
1753  * @cliexend
1754 ?*/
1755 VLIB_CLI_COMMAND (nat44_det_show_mappings_command, static) = {
1756     .path = "show nat44 deterministic mappings",
1757     .short_help = "show nat44 deterministic mappings",
1758     .function = nat44_det_show_mappings_command_fn,
1759 };
1760
1761 /*?
1762  * @cliexpar
1763  * @cliexstart{nat44 deterministic forward}
1764  * Return outside address and port range from inside address for deterministic
1765  * NAT.
1766  * To obtain outside address and port of inside host use:
1767  *  vpp# nat44 deterministic forward 10.0.0.2
1768  *  1.1.1.0:<1054-1068>
1769  * @cliexend
1770 ?*/
1771 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
1772     .path = "nat44 deterministic forward",
1773     .short_help = "nat44 deterministic forward <addr>",
1774     .function = snat_det_forward_command_fn,
1775 };
1776
1777 /*?
1778  * @cliexpar
1779  * @cliexstart{nat44 deterministic reverse}
1780  * Return inside address from outside address and port for deterministic NAT.
1781  * To obtain inside host address from outside address and port use:
1782  *  #vpp nat44 deterministic reverse 1.1.1.1:1276
1783  *  10.0.16.16
1784  * @cliexend
1785 ?*/
1786 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
1787     .path = "nat44 deterministic reverse",
1788     .short_help = "nat44 deterministic reverse <addr>:<port>",
1789     .function = snat_det_reverse_command_fn,
1790 };
1791
1792 /*?
1793  * @cliexpar
1794  * @cliexstart{set nat44 deterministic timeout}
1795  * Set values of timeouts for deterministic NAT (in seconds), use:
1796  *  vpp# set nat44 deterministic timeout udp 120 tcp-established 7500
1797  *  tcp-transitory 250 icmp 90
1798  * To reset default values use:
1799  *  vpp# set nat44 deterministic timeout reset
1800  * @cliexend
1801 ?*/
1802 VLIB_CLI_COMMAND (set_timeout_command, static) = {
1803   .path = "set nat44 deterministic timeout",
1804   .function = set_timeout_command_fn,
1805   .short_help =
1806     "set nat44 deterministic timeout [udp <sec> | tcp-established <sec> "
1807     "tcp-transitory <sec> | icmp <sec> | reset]",
1808 };
1809
1810 /*?
1811  * @cliexpar
1812  * @cliexstart{show nat44 deterministic timeouts}
1813  * Show values of timeouts for deterministic NAT.
1814  * vpp# show nat44 deterministic timeouts
1815  * udp timeout: 300sec
1816  * tcp-established timeout: 7440sec
1817  * tcp-transitory timeout: 240sec
1818  * icmp timeout: 60sec
1819  * @cliexend
1820 ?*/
1821 VLIB_CLI_COMMAND (nat44_det_show_timeouts_command, static) = {
1822   .path = "show nat44 deterministic timeouts",
1823   .short_help = "show nat44 deterministic timeouts",
1824   .function = nat44_det_show_timeouts_command_fn,
1825 };
1826
1827 /*?
1828  * @cliexpar
1829  * @cliexstart{show nat44 deterministic sessions}
1830  * Show NAT44 deterministic sessions.
1831  * vpp# show nat44 deterministic sessions
1832  * NAT44 deterministic sessions:
1833  *   in 10.0.0.3:3005 out 1.1.1.2:1146 external host 172.16.1.2:3006 state: udp-active expire: 306
1834  *   in 10.0.0.3:3000 out 1.1.1.2:1141 external host 172.16.1.2:3001 state: udp-active expire: 306
1835  *   in 10.0.0.4:3005 out 1.1.1.2:1177 external host 172.16.1.2:3006 state: udp-active expire: 306
1836  * @cliexend
1837 ?*/
1838 VLIB_CLI_COMMAND (nat44_det_show_sessions_command, static) = {
1839   .path = "show nat44 deterministic sessions",
1840   .short_help = "show nat44 deterministic sessions",
1841   .function = nat44_det_show_sessions_command_fn,
1842 };
1843
1844 /*?
1845  * @cliexpar
1846  * @cliexstart{nat44 deterministic close session out}
1847  * Close session using outside ip address and port
1848  * and external ip address and port, use:
1849  *  vpp# nat44 deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
1850  * @cliexend
1851 ?*/
1852 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
1853   .path = "nat44 deterministic close session out",
1854   .short_help = "nat44 deterministic close session out "
1855                 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
1856   .function = snat_det_close_session_out_fn,
1857 };
1858
1859 /*?
1860  * @cliexpar
1861  * @cliexstart{nat44 deterministic close session in}
1862  * Close session using inside ip address and port
1863  * and external ip address and port, use:
1864  *  vpp# nat44 deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
1865  * @cliexend
1866 ?*/
1867 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
1868   .path = "nat44 deterministic close session in",
1869   .short_help = "nat44 deterministic close session in "
1870                 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
1871   .function = snat_det_close_session_in_fn,
1872 };
1873
1874 /* *INDENT-ON* */
1875
1876 /*
1877  * fd.io coding-style-patch-verification: ON
1878  *
1879  * Local Variables:
1880  * eval: (c-set-style "gnu")
1881  * End:
1882  */