Add special Twice-NAT feature (VPP-1221)
[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   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
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 = TWICE_NAT;
542       else if (unformat (line_input, "self-twice-nat"))
543         twice_nat = TWICE_NAT_SELF;
544       else if (unformat (line_input, "out2in-only"))
545         out2in_only = 1;
546       else if (unformat (line_input, "del"))
547         is_add = 0;
548       else
549         {
550           error = clib_error_return (0, "unknown input: '%U'",
551                                      format_unformat_error, line_input);
552           goto done;
553         }
554     }
555
556   if (twice_nat && addr_only)
557     {
558       error = clib_error_return (0, "twice NAT only for 1:1 NAPT");
559       goto done;
560     }
561
562   if (!addr_only && !proto_set)
563     {
564       error = clib_error_return (0, "missing protocol");
565       goto done;
566     }
567
568   rv = snat_add_static_mapping (l_addr, e_addr, (u16) l_port, (u16) e_port,
569                                 vrf_id, addr_only, sw_if_index, proto, is_add,
570                                 twice_nat, out2in_only, 0);
571
572   switch (rv)
573     {
574     case VNET_API_ERROR_INVALID_VALUE:
575       error = clib_error_return (0, "External port already in use.");
576       goto done;
577     case VNET_API_ERROR_NO_SUCH_ENTRY:
578       if (is_add)
579         error = clib_error_return (0, "External addres must be allocated.");
580       else
581         error = clib_error_return (0, "Mapping not exist.");
582       goto done;
583     case VNET_API_ERROR_NO_SUCH_FIB:
584       error = clib_error_return (0, "No such VRF id.");
585       goto done;
586     case VNET_API_ERROR_VALUE_EXIST:
587       error = clib_error_return (0, "Mapping already exist.");
588       goto done;
589     default:
590       break;
591     }
592
593 done:
594   unformat_free (line_input);
595
596   return error;
597 }
598
599 static clib_error_t *
600 add_identity_mapping_command_fn (vlib_main_t * vm,
601                                  unformat_input_t * input,
602                                  vlib_cli_command_t * cmd)
603 {
604   unformat_input_t _line_input, *line_input = &_line_input;
605   snat_main_t *sm = &snat_main;
606   clib_error_t *error = 0;
607   ip4_address_t addr;
608   u32 port = 0, vrf_id = ~0;
609   int is_add = 1;
610   int addr_only = 1;
611   u32 sw_if_index = ~0;
612   vnet_main_t *vnm = vnet_get_main ();
613   int rv;
614   snat_protocol_t proto;
615
616   if (sm->deterministic)
617     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
618
619   addr.as_u32 = 0;
620
621   /* Get a line of input. */
622   if (!unformat_user (input, unformat_line_input, line_input))
623     return 0;
624
625   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
626     {
627       if (unformat (line_input, "%U", unformat_ip4_address, &addr))
628         ;
629       else if (unformat (line_input, "external %U",
630                          unformat_vnet_sw_interface, vnm, &sw_if_index))
631         ;
632       else if (unformat (line_input, "vrf %u", &vrf_id))
633         ;
634       else if (unformat (line_input, "%U %u", unformat_snat_protocol, &proto,
635                          &port))
636         addr_only = 0;
637       else if (unformat (line_input, "del"))
638         is_add = 0;
639       else
640         {
641           error = clib_error_return (0, "unknown input: '%U'",
642                                      format_unformat_error, line_input);
643           goto done;
644         }
645     }
646
647   rv = snat_add_static_mapping (addr, addr, (u16) port, (u16) port,
648                                 vrf_id, addr_only, sw_if_index, proto, is_add,
649                                 0, 0, 0);
650
651   switch (rv)
652     {
653     case VNET_API_ERROR_INVALID_VALUE:
654       error = clib_error_return (0, "External port already in use.");
655       goto done;
656     case VNET_API_ERROR_NO_SUCH_ENTRY:
657       if (is_add)
658         error = clib_error_return (0, "External addres must be allocated.");
659       else
660         error = clib_error_return (0, "Mapping not exist.");
661       goto done;
662     case VNET_API_ERROR_NO_SUCH_FIB:
663       error = clib_error_return (0, "No such VRF id.");
664       goto done;
665     case VNET_API_ERROR_VALUE_EXIST:
666       error = clib_error_return (0, "Mapping already exist.");
667       goto done;
668     default:
669       break;
670     }
671
672 done:
673   unformat_free (line_input);
674
675   return error;
676 }
677
678 static clib_error_t *
679 add_lb_static_mapping_command_fn (vlib_main_t * vm,
680                                   unformat_input_t * input,
681                                   vlib_cli_command_t * cmd)
682 {
683   unformat_input_t _line_input, *line_input = &_line_input;
684   snat_main_t *sm = &snat_main;
685   clib_error_t *error = 0;
686   ip4_address_t l_addr, e_addr;
687   u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
688   int is_add = 1;
689   int rv;
690   snat_protocol_t proto;
691   u8 proto_set = 0;
692   nat44_lb_addr_port_t *locals = 0, local;
693   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
694   u8 out2in_only = 0;
695
696   if (sm->deterministic)
697     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
698
699   /* Get a line of input. */
700   if (!unformat_user (input, unformat_line_input, line_input))
701     return 0;
702
703   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
704     {
705       if (unformat (line_input, "local %U:%u probability %u",
706                     unformat_ip4_address, &l_addr, &l_port, &probability))
707         {
708           memset (&local, 0, sizeof (local));
709           local.addr = l_addr;
710           local.port = (u16) l_port;
711           local.probability = (u8) probability;
712           vec_add1 (locals, local);
713         }
714       else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
715                          &e_addr, &e_port))
716         ;
717       else if (unformat (line_input, "vrf %u", &vrf_id))
718         ;
719       else if (unformat (line_input, "protocol %U", unformat_snat_protocol,
720                          &proto))
721         proto_set = 1;
722       else if (unformat (line_input, "twice-nat"))
723         twice_nat = TWICE_NAT;
724       else if (unformat (line_input, "self-twice-nat"))
725         twice_nat = TWICE_NAT_SELF;
726       else if (unformat (line_input, "out2in-only"))
727         out2in_only = 1;
728       else if (unformat (line_input, "del"))
729         is_add = 0;
730       else
731         {
732           error = clib_error_return (0, "unknown input: '%U'",
733                                      format_unformat_error, line_input);
734           goto done;
735         }
736     }
737
738   if (vec_len (locals) < 2)
739     {
740       error = clib_error_return (0, "at least two local must be set");
741       goto done;
742     }
743
744   if (!proto_set)
745     {
746       error = clib_error_return (0, "missing protocol");
747       goto done;
748     }
749
750   rv = nat44_add_del_lb_static_mapping (e_addr, (u16) e_port, proto, vrf_id,
751                                         locals, is_add, twice_nat,
752                                         out2in_only, 0);
753
754   switch (rv)
755     {
756     case VNET_API_ERROR_INVALID_VALUE:
757       error = clib_error_return (0, "External port already in use.");
758       goto done;
759     case VNET_API_ERROR_NO_SUCH_ENTRY:
760       if (is_add)
761         error = clib_error_return (0, "External addres must be allocated.");
762       else
763         error = clib_error_return (0, "Mapping not exist.");
764       goto done;
765     case VNET_API_ERROR_VALUE_EXIST:
766       error = clib_error_return (0, "Mapping already exist.");
767       goto done;
768     default:
769       break;
770     }
771
772 done:
773   unformat_free (line_input);
774   vec_free (locals);
775
776   return error;
777 }
778
779 static clib_error_t *
780 nat44_show_static_mappings_command_fn (vlib_main_t * vm,
781                                        unformat_input_t * input,
782                                        vlib_cli_command_t * cmd)
783 {
784   snat_main_t *sm = &snat_main;
785   snat_static_mapping_t *m;
786   snat_static_map_resolve_t *rp;
787
788   if (sm->deterministic)
789     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
790
791   vlib_cli_output (vm, "NAT44 static mappings:");
792   /* *INDENT-OFF* */
793   pool_foreach (m, sm->static_mappings,
794   ({
795     vlib_cli_output (vm, " %U", format_snat_static_mapping, m);
796   }));
797   vec_foreach (rp, sm->to_resolve)
798     vlib_cli_output (vm, " %U", format_snat_static_map_to_resolve, rp);
799   /* *INDENT-ON* */
800
801   return 0;
802 }
803
804 static clib_error_t *
805 snat_add_interface_address_command_fn (vlib_main_t * vm,
806                                        unformat_input_t * input,
807                                        vlib_cli_command_t * cmd)
808 {
809   snat_main_t *sm = &snat_main;
810   unformat_input_t _line_input, *line_input = &_line_input;
811   u32 sw_if_index;
812   int rv;
813   int is_del = 0;
814   clib_error_t *error = 0;
815   u8 twice_nat = 0;
816
817   if (sm->deterministic)
818     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
819
820   /* Get a line of input. */
821   if (!unformat_user (input, unformat_line_input, line_input))
822     return 0;
823
824   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
825     {
826       if (unformat (line_input, "%U", unformat_vnet_sw_interface,
827                     sm->vnet_main, &sw_if_index))
828         ;
829       else if (unformat (line_input, "twice-nat"))
830         twice_nat = 1;
831       else if (unformat (line_input, "del"))
832         is_del = 1;
833       else
834         {
835           error = clib_error_return (0, "unknown input '%U'",
836                                      format_unformat_error, line_input);
837           goto done;
838         }
839     }
840
841   rv = snat_add_interface_address (sm, sw_if_index, is_del, twice_nat);
842
843   switch (rv)
844     {
845     case 0:
846       break;
847
848     default:
849       error = clib_error_return (0, "snat_add_interface_address returned %d",
850                                  rv);
851       goto done;
852     }
853
854 done:
855   unformat_free (line_input);
856
857   return error;
858 }
859
860 static clib_error_t *
861 nat44_show_interface_address_command_fn (vlib_main_t * vm,
862                                          unformat_input_t * input,
863                                          vlib_cli_command_t * cmd)
864 {
865   snat_main_t *sm = &snat_main;
866   vnet_main_t *vnm = vnet_get_main ();
867   u32 *sw_if_index;
868
869   if (sm->deterministic)
870     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
871
872   /* *INDENT-OFF* */
873   vlib_cli_output (vm, "NAT44 pool address interfaces:");
874   vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
875     {
876       vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
877                        *sw_if_index);
878     }
879   vlib_cli_output (vm, "NAT44 twice-nat pool address interfaces:");
880   vec_foreach (sw_if_index, sm->auto_add_sw_if_indices_twice_nat)
881     {
882       vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
883                        *sw_if_index);
884     }
885   /* *INDENT-ON* */
886
887   return 0;
888 }
889
890 static clib_error_t *
891 nat44_show_sessions_command_fn (vlib_main_t * vm, unformat_input_t * input,
892                                 vlib_cli_command_t * cmd)
893 {
894   int verbose = 0;
895   snat_main_t *sm = &snat_main;
896   snat_main_per_thread_data_t *tsm;
897   snat_user_t *u;
898   int i = 0;
899
900   if (sm->deterministic)
901     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
902
903   if (unformat (input, "detail"))
904     verbose = 1;
905
906   vlib_cli_output (vm, "NAT44 sessions:");
907
908   /* *INDENT-OFF* */
909   vec_foreach_index (i, sm->per_thread_data)
910     {
911       tsm = vec_elt_at_index (sm->per_thread_data, i);
912
913       pool_foreach (u, tsm->users,
914       ({
915         vlib_cli_output (vm, "  %U", format_snat_user, tsm, u, verbose);
916       }));
917     }
918   /* *INDENT-ON* */
919
920   return 0;
921 }
922
923 static clib_error_t *
924 nat44_del_session_command_fn (vlib_main_t * vm,
925                               unformat_input_t * input,
926                               vlib_cli_command_t * cmd)
927 {
928   snat_main_t *sm = &snat_main;
929   unformat_input_t _line_input, *line_input = &_line_input;
930   int is_in = 0;
931   clib_error_t *error = 0;
932   ip4_address_t addr;
933   u32 port = 0, vrf_id = sm->outside_vrf_id;
934   snat_protocol_t proto;
935   int rv;
936
937   if (sm->deterministic)
938     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
939
940   /* Get a line of input. */
941   if (!unformat_user (input, unformat_line_input, line_input))
942     return 0;
943
944   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
945     {
946       if (unformat
947           (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
948            unformat_snat_protocol, &proto))
949         ;
950       else if (unformat (line_input, "in"))
951         {
952           is_in = 1;
953           vrf_id = sm->inside_vrf_id;
954         }
955       else if (unformat (line_input, "vrf %u", &vrf_id))
956         ;
957       else
958         {
959           error = clib_error_return (0, "unknown input '%U'",
960                                      format_unformat_error, line_input);
961           goto done;
962         }
963     }
964
965   rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
966
967   switch (rv)
968     {
969     case 0:
970       break;
971
972     default:
973       error = clib_error_return (0, "nat44_del_session returned %d", rv);
974       goto done;
975     }
976
977 done:
978   unformat_free (line_input);
979
980   return error;
981 }
982
983 static clib_error_t *
984 snat_forwarding_set_command_fn (vlib_main_t * vm,
985                                 unformat_input_t * input,
986                                 vlib_cli_command_t * cmd)
987 {
988   snat_main_t *sm = &snat_main;
989   unformat_input_t _line_input, *line_input = &_line_input;
990   u8 forwarding_enable;
991   u8 forwarding_enable_set = 0;
992   clib_error_t *error = 0;
993
994   if (sm->deterministic)
995     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
996
997   /* Get a line of input. */
998   if (!unformat_user (input, unformat_line_input, line_input))
999     return clib_error_return (0, "'enable' or 'disable' expected");
1000
1001   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1002     {
1003       if (!forwarding_enable_set && unformat (line_input, "enable"))
1004         {
1005           forwarding_enable = 1;
1006           forwarding_enable_set = 1;
1007         }
1008       else if (!forwarding_enable_set && unformat (line_input, "disable"))
1009         {
1010           forwarding_enable = 0;
1011           forwarding_enable_set = 1;
1012         }
1013       else
1014         {
1015           error = clib_error_return (0, "unknown input '%U'",
1016                                      format_unformat_error, line_input);
1017           goto done;
1018         }
1019     }
1020
1021   if (!forwarding_enable_set)
1022     {
1023       error = clib_error_return (0, "'enable' or 'disable' expected");
1024       goto done;
1025     }
1026
1027   sm->forwarding_enabled = forwarding_enable;
1028
1029 done:
1030   unformat_free (line_input);
1031
1032   return error;
1033 }
1034
1035 static clib_error_t *
1036 snat_det_map_command_fn (vlib_main_t * vm,
1037                          unformat_input_t * input, vlib_cli_command_t * cmd)
1038 {
1039   snat_main_t *sm = &snat_main;
1040   unformat_input_t _line_input, *line_input = &_line_input;
1041   ip4_address_t in_addr, out_addr;
1042   u32 in_plen, out_plen;
1043   int is_add = 1, rv;
1044   clib_error_t *error = 0;
1045
1046   if (!sm->deterministic)
1047     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1048
1049   /* Get a line of input. */
1050   if (!unformat_user (input, unformat_line_input, line_input))
1051     return 0;
1052
1053   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1054     {
1055       if (unformat
1056           (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
1057         ;
1058       else
1059         if (unformat
1060             (line_input, "out %U/%u", unformat_ip4_address, &out_addr,
1061              &out_plen))
1062         ;
1063       else if (unformat (line_input, "del"))
1064         is_add = 0;
1065       else
1066         {
1067           error = clib_error_return (0, "unknown input '%U'",
1068                                      format_unformat_error, line_input);
1069           goto done;
1070         }
1071     }
1072
1073   rv = snat_det_add_map (sm, &in_addr, (u8) in_plen, &out_addr, (u8) out_plen,
1074                          is_add);
1075
1076   if (rv)
1077     {
1078       error = clib_error_return (0, "snat_det_add_map return %d", rv);
1079       goto done;
1080     }
1081
1082 done:
1083   unformat_free (line_input);
1084
1085   return error;
1086 }
1087
1088 static clib_error_t *
1089 nat44_det_show_mappings_command_fn (vlib_main_t * vm,
1090                                     unformat_input_t * input,
1091                                     vlib_cli_command_t * cmd)
1092 {
1093   snat_main_t *sm = &snat_main;
1094   snat_det_map_t *dm;
1095
1096   if (!sm->deterministic)
1097     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1098
1099   vlib_cli_output (vm, "NAT44 deterministic mappings:");
1100   /* *INDENT-OFF* */
1101   pool_foreach (dm, sm->det_maps,
1102   ({
1103     vlib_cli_output (vm, " in %U/%d out %U/%d\n",
1104                      format_ip4_address, &dm->in_addr, dm->in_plen,
1105                      format_ip4_address, &dm->out_addr, dm->out_plen);
1106     vlib_cli_output (vm, "  outside address sharing ratio: %d\n",
1107                      dm->sharing_ratio);
1108     vlib_cli_output (vm, "  number of ports per inside host: %d\n",
1109                      dm->ports_per_host);
1110     vlib_cli_output (vm, "  sessions number: %d\n", dm->ses_num);
1111   }));
1112   /* *INDENT-ON* */
1113
1114   return 0;
1115 }
1116
1117 static clib_error_t *
1118 snat_det_forward_command_fn (vlib_main_t * vm,
1119                              unformat_input_t * input,
1120                              vlib_cli_command_t * cmd)
1121 {
1122   snat_main_t *sm = &snat_main;
1123   unformat_input_t _line_input, *line_input = &_line_input;
1124   ip4_address_t in_addr, out_addr;
1125   u16 lo_port;
1126   snat_det_map_t *dm;
1127   clib_error_t *error = 0;
1128
1129   if (!sm->deterministic)
1130     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1131
1132   /* Get a line of input. */
1133   if (!unformat_user (input, unformat_line_input, line_input))
1134     return 0;
1135
1136   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1137     {
1138       if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
1139         ;
1140       else
1141         {
1142           error = clib_error_return (0, "unknown input '%U'",
1143                                      format_unformat_error, line_input);
1144           goto done;
1145         }
1146     }
1147
1148   dm = snat_det_map_by_user (sm, &in_addr);
1149   if (!dm)
1150     vlib_cli_output (vm, "no match");
1151   else
1152     {
1153       snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
1154       vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
1155                        lo_port, lo_port + dm->ports_per_host - 1);
1156     }
1157
1158 done:
1159   unformat_free (line_input);
1160
1161   return error;
1162 }
1163
1164 static clib_error_t *
1165 snat_det_reverse_command_fn (vlib_main_t * vm,
1166                              unformat_input_t * input,
1167                              vlib_cli_command_t * cmd)
1168 {
1169   snat_main_t *sm = &snat_main;
1170   unformat_input_t _line_input, *line_input = &_line_input;
1171   ip4_address_t in_addr, out_addr;
1172   u32 out_port;
1173   snat_det_map_t *dm;
1174   clib_error_t *error = 0;
1175
1176   if (!sm->deterministic)
1177     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1178
1179   /* Get a line of input. */
1180   if (!unformat_user (input, unformat_line_input, line_input))
1181     return 0;
1182
1183   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1184     {
1185       if (unformat
1186           (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
1187         ;
1188       else
1189         {
1190           error = clib_error_return (0, "unknown input '%U'",
1191                                      format_unformat_error, line_input);
1192           goto done;
1193         }
1194     }
1195
1196   if (out_port < 1024 || out_port > 65535)
1197     {
1198       error = clib_error_return (0, "wrong port, must be <1024-65535>");
1199       goto done;
1200     }
1201
1202   dm = snat_det_map_by_out (sm, &out_addr);
1203   if (!dm)
1204     vlib_cli_output (vm, "no match");
1205   else
1206     {
1207       snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
1208       vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
1209     }
1210
1211 done:
1212   unformat_free (line_input);
1213
1214   return error;
1215 }
1216
1217 static clib_error_t *
1218 set_timeout_command_fn (vlib_main_t * vm,
1219                         unformat_input_t * input, vlib_cli_command_t * cmd)
1220 {
1221   snat_main_t *sm = &snat_main;
1222   unformat_input_t _line_input, *line_input = &_line_input;
1223   clib_error_t *error = 0;
1224
1225   if (!sm->deterministic)
1226     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1227
1228   /* Get a line of input. */
1229   if (!unformat_user (input, unformat_line_input, line_input))
1230     return 0;
1231
1232   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1233     {
1234       if (unformat (line_input, "udp %u", &sm->udp_timeout))
1235         ;
1236       else if (unformat (line_input, "tcp-established %u",
1237                          &sm->tcp_established_timeout))
1238         ;
1239       else if (unformat (line_input, "tcp-transitory %u",
1240                          &sm->tcp_transitory_timeout))
1241         ;
1242       else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
1243         ;
1244       else if (unformat (line_input, "reset"))
1245         {
1246           sm->udp_timeout = SNAT_UDP_TIMEOUT;
1247           sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1248           sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1249           sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1250         }
1251       else
1252         {
1253           error = clib_error_return (0, "unknown input '%U'",
1254                                      format_unformat_error, line_input);
1255           goto done;
1256         }
1257     }
1258
1259 done:
1260   unformat_free (line_input);
1261
1262   return error;
1263 }
1264
1265 static clib_error_t *
1266 nat44_det_show_timeouts_command_fn (vlib_main_t * vm,
1267                                     unformat_input_t * input,
1268                                     vlib_cli_command_t * cmd)
1269 {
1270   snat_main_t *sm = &snat_main;
1271
1272   if (!sm->deterministic)
1273     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1274
1275   vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
1276   vlib_cli_output (vm, "tcp-established timeout: %dsec",
1277                    sm->tcp_established_timeout);
1278   vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
1279                    sm->tcp_transitory_timeout);
1280   vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
1281
1282   return 0;
1283 }
1284
1285 static clib_error_t *
1286 nat44_det_show_sessions_command_fn (vlib_main_t * vm,
1287                                     unformat_input_t * input,
1288                                     vlib_cli_command_t * cmd)
1289 {
1290   snat_main_t *sm = &snat_main;
1291   snat_det_map_t *dm;
1292   snat_det_session_t *ses;
1293   int i;
1294
1295   if (!sm->deterministic)
1296     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1297
1298   vlib_cli_output (vm, "NAT44 deterministic sessions:");
1299   /* *INDENT-OFF* */
1300   pool_foreach (dm, sm->det_maps,
1301   ({
1302     vec_foreach_index (i, dm->sessions)
1303       {
1304         ses = vec_elt_at_index (dm->sessions, i);
1305         if (ses->in_port)
1306           vlib_cli_output (vm, "  %U", format_det_map_ses, dm, ses, &i);
1307       }
1308   }));
1309   /* *INDENT-ON* */
1310   return 0;
1311 }
1312
1313 static clib_error_t *
1314 snat_det_close_session_out_fn (vlib_main_t * vm,
1315                                unformat_input_t * input,
1316                                vlib_cli_command_t * cmd)
1317 {
1318   snat_main_t *sm = &snat_main;
1319   unformat_input_t _line_input, *line_input = &_line_input;
1320   ip4_address_t out_addr, ext_addr, in_addr;
1321   u32 out_port, ext_port;
1322   snat_det_map_t *dm;
1323   snat_det_session_t *ses;
1324   snat_det_out_key_t key;
1325   clib_error_t *error = 0;
1326
1327   if (!sm->deterministic)
1328     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1329
1330   /* Get a line of input. */
1331   if (!unformat_user (input, unformat_line_input, line_input))
1332     return 0;
1333
1334   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1335     {
1336       if (unformat (line_input, "%U:%d %U:%d",
1337                     unformat_ip4_address, &out_addr, &out_port,
1338                     unformat_ip4_address, &ext_addr, &ext_port))
1339         ;
1340       else
1341         {
1342           error = clib_error_return (0, "unknown input '%U'",
1343                                      format_unformat_error, line_input);
1344           goto done;
1345         }
1346     }
1347
1348   unformat_free (line_input);
1349
1350   dm = snat_det_map_by_out (sm, &out_addr);
1351   if (!dm)
1352     vlib_cli_output (vm, "no match");
1353   else
1354     {
1355       snat_det_reverse (dm, &ext_addr, (u16) out_port, &in_addr);
1356       key.ext_host_addr = out_addr;
1357       key.ext_host_port = ntohs ((u16) ext_port);
1358       key.out_port = ntohs ((u16) out_port);
1359       ses = snat_det_get_ses_by_out (dm, &out_addr, key.as_u64);
1360       if (!ses)
1361         vlib_cli_output (vm, "no match");
1362       else
1363         snat_det_ses_close (dm, ses);
1364     }
1365
1366 done:
1367   unformat_free (line_input);
1368
1369   return error;
1370 }
1371
1372 static clib_error_t *
1373 snat_det_close_session_in_fn (vlib_main_t * vm,
1374                               unformat_input_t * input,
1375                               vlib_cli_command_t * cmd)
1376 {
1377   snat_main_t *sm = &snat_main;
1378   unformat_input_t _line_input, *line_input = &_line_input;
1379   ip4_address_t in_addr, ext_addr;
1380   u32 in_port, ext_port;
1381   snat_det_map_t *dm;
1382   snat_det_session_t *ses;
1383   snat_det_out_key_t key;
1384   clib_error_t *error = 0;
1385
1386   if (!sm->deterministic)
1387     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1388
1389   /* Get a line of input. */
1390   if (!unformat_user (input, unformat_line_input, line_input))
1391     return 0;
1392
1393   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1394     {
1395       if (unformat (line_input, "%U:%d %U:%d",
1396                     unformat_ip4_address, &in_addr, &in_port,
1397                     unformat_ip4_address, &ext_addr, &ext_port))
1398         ;
1399       else
1400         {
1401           error = clib_error_return (0, "unknown input '%U'",
1402                                      format_unformat_error, line_input);
1403           goto done;
1404         }
1405     }
1406
1407   unformat_free (line_input);
1408
1409   dm = snat_det_map_by_user (sm, &in_addr);
1410   if (!dm)
1411     vlib_cli_output (vm, "no match");
1412   else
1413     {
1414       key.ext_host_addr = ext_addr;
1415       key.ext_host_port = ntohs ((u16) ext_port);
1416       ses =
1417         snat_det_find_ses_by_in (dm, &in_addr, ntohs ((u16) in_port), key);
1418       if (!ses)
1419         vlib_cli_output (vm, "no match");
1420       else
1421         snat_det_ses_close (dm, ses);
1422     }
1423
1424 done:
1425   unformat_free (line_input);
1426
1427   return error;
1428 }
1429 /* *INDENT-OFF* */
1430
1431 /*?
1432  * @cliexpar
1433  * @cliexstart{set snat workers}
1434  * Set NAT workers if 2 or more workers available, use:
1435  *  vpp# set snat workers 0-2,5
1436  * @cliexend
1437 ?*/
1438 VLIB_CLI_COMMAND (set_workers_command, static) = {
1439   .path = "set nat workers",
1440   .function = set_workers_command_fn,
1441   .short_help = "set nat workers <workers-list>",
1442 };
1443
1444 /*?
1445  * @cliexpar
1446  * @cliexstart{show nat workers}
1447  * Show NAT workers.
1448  *  vpp# show nat workers:
1449  *  2 workers
1450  *    vpp_wk_0
1451  *    vpp_wk_1
1452  * @cliexend
1453 ?*/
1454 VLIB_CLI_COMMAND (nat_show_workers_command, static) = {
1455   .path = "show nat workers",
1456   .short_help = "show nat workers",
1457   .function = nat_show_workers_commnad_fn,
1458 };
1459
1460 /*?
1461  * @cliexpar
1462  * @cliexstart{snat ipfix logging}
1463  * To enable NAT IPFIX logging use:
1464  *  vpp# nat ipfix logging
1465  * To set IPFIX exporter use:
1466  *  vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1467  * @cliexend
1468 ?*/
1469 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1470   .path = "nat ipfix logging",
1471   .function = snat_ipfix_logging_enable_disable_command_fn,
1472   .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1473 };
1474
1475 /*?
1476  * @cliexpar
1477  * @cliexstart{nat addr-port-assignment-alg}
1478  * Set address and port assignment algorithm
1479  * For the MAP-E CE limit port choice based on PSID use:
1480  *  vpp# nat addr-port-assignment-alg map-e psid 10 psid-offset 6 psid-len 6
1481  * To set standard (default) address and port assignment algorithm use:
1482  *  vpp# nat addr-port-assignment-alg default
1483  * @cliexend
1484 ?*/
1485 VLIB_CLI_COMMAND (nat44_set_alloc_addr_and_port_alg_command, static) = {
1486     .path = "nat addr-port-assignment-alg",
1487     .short_help = "nat addr-port-assignment-alg <alg-name> [<alg-params>]",
1488     .function = nat44_set_alloc_addr_and_port_alg_command_fn,
1489 };
1490
1491 /*?
1492  * @cliexpar
1493  * @cliexstart{nat44 add address}
1494  * Add/delete NAT44 pool address.
1495  * To add NAT44 pool address use:
1496  *  vpp# nat44 add address 172.16.1.3
1497  *  vpp# nat44 add address 172.16.2.2 - 172.16.2.24
1498  * To add NAT44 pool address for specific tenant (identified by VRF id) use:
1499  *  vpp# nat44 add address 172.16.1.3 tenant-vrf 10
1500  * @cliexend
1501 ?*/
1502 VLIB_CLI_COMMAND (add_address_command, static) = {
1503   .path = "nat44 add address",
1504   .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
1505                 "[tenant-vrf <vrf-id>] [twice-nat] [del]",
1506   .function = add_address_command_fn,
1507 };
1508
1509 /*?
1510  * @cliexpar
1511  * @cliexstart{show nat44 addresses}
1512  * Show NAT44 pool addresses.
1513  * vpp# show nat44 addresses
1514  * NAT44 pool addresses:
1515  * 172.16.2.2
1516  *   tenant VRF independent
1517  *   10 busy udp ports
1518  *   0 busy tcp ports
1519  *   0 busy icmp ports
1520  * 172.16.1.3
1521  *   tenant VRF: 10
1522  *   0 busy udp ports
1523  *   2 busy tcp ports
1524  *   0 busy icmp ports
1525  * NAT44 twice-nat pool addresses:
1526  * 10.20.30.72
1527  *   tenant VRF independent
1528  *   0 busy udp ports
1529  *   0 busy tcp ports
1530  *   0 busy icmp ports
1531  * @cliexend
1532 ?*/
1533 VLIB_CLI_COMMAND (nat44_show_addresses_command, static) = {
1534   .path = "show nat44 addresses",
1535   .short_help = "show nat44 addresses",
1536   .function = nat44_show_addresses_command_fn,
1537 };
1538
1539 /*?
1540  * @cliexpar
1541  * @cliexstart{set interface nat44}
1542  * Enable/disable NAT44 feature on the interface.
1543  * To enable NAT44 feature with local network interface use:
1544  *  vpp# set interface nat44 in GigabitEthernet0/8/0
1545  * To enable NAT44 feature with external network interface use:
1546  *  vpp# set interface nat44 out GigabitEthernet0/a/0
1547  * @cliexend
1548 ?*/
1549 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1550   .path = "set interface nat44",
1551   .function = snat_feature_command_fn,
1552   .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
1553                 "[del]",
1554 };
1555
1556 /*?
1557  * @cliexpar
1558  * @cliexstart{show nat44 interfaces}
1559  * Show interfaces with NAT44 feature.
1560  * vpp# show nat44 interfaces
1561  * NAT44 interfaces:
1562  *  GigabitEthernet0/8/0 in
1563  *  GigabitEthernet0/a/0 out
1564  * @cliexend
1565 ?*/
1566 VLIB_CLI_COMMAND (nat44_show_interfaces_command, static) = {
1567   .path = "show nat44 interfaces",
1568   .short_help = "show nat44 interfaces",
1569   .function = nat44_show_interfaces_command_fn,
1570 };
1571
1572 /*?
1573  * @cliexpar
1574  * @cliexstart{nat44 add static mapping}
1575  * Static mapping allows hosts on the external network to initiate connection
1576  * to to the local network host.
1577  * To create static mapping between local host address 10.0.0.3 port 6303 and
1578  * external address 4.4.4.4 port 3606 for TCP protocol use:
1579  *  vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
1580  * If not runnig "static mapping only" NAT plugin mode use before:
1581  *  vpp# nat44 add address 4.4.4.4
1582  * To create static mapping between local and external address use:
1583  *  vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
1584  * @cliexend
1585 ?*/
1586 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1587   .path = "nat44 add static mapping",
1588   .function = add_static_mapping_command_fn,
1589   .short_help =
1590     "nat44 add static mapping tcp|udp|icmp local <addr> [<port>] "
1591     "external <addr> [<port>] [vrf <table-id>] [twice-nat|self-twice-nat] "
1592     "[out2in-only] [del]",
1593 };
1594
1595 /*?
1596  * @cliexpar
1597  * @cliexstart{nat44 add identity mapping}
1598  * Identity mapping translate an IP address to itself.
1599  * To create identity mapping for address 10.0.0.3 port 6303 for TCP protocol
1600  * use:
1601  *  vpp# nat44 add identity mapping 10.0.0.3 tcp 6303
1602  * To create identity mapping for address 10.0.0.3 use:
1603  *  vpp# nat44 add identity mapping 10.0.0.3
1604  * To create identity mapping for DHCP addressed interface use:
1605  *  vpp# nat44 add identity mapping GigabitEthernet0/a/0 tcp 3606
1606  * @cliexend
1607 ?*/
1608 VLIB_CLI_COMMAND (add_identity_mapping_command, static) = {
1609   .path = "nat44 add identity mapping",
1610   .function = add_identity_mapping_command_fn,
1611   .short_help = "nat44 add identity mapping <interface>|<ip4-addr> "
1612     "[<protocol> <port>] [vrf <table-id>] [del]",
1613 };
1614
1615 /*?
1616  * @cliexpar
1617  * @cliexstart{nat44 add load-balancing static mapping}
1618  * Service load balancing using NAT44
1619  * To add static mapping with load balancing for service with external IP
1620  * address 1.2.3.4 and TCP port 80 and mapped to 2 local servers
1621  * 10.100.10.10:8080 and 10.100.10.20:8080 with probability 80% resp. 20% use:
1622  *  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
1623  * @cliexend
1624 ?*/
1625 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
1626   .path = "nat44 add load-balancing static mapping",
1627   .function = add_lb_static_mapping_command_fn,
1628   .short_help =
1629     "nat44 add load-balancing static mapping protocol tcp|udp "
1630     "external <addr>:<port> local <addr>:<port> probability <n> "
1631     "[twice-nat|self-twice-nat] [vrf <table-id>] [out2in-only] [del]",
1632 };
1633
1634 /*?
1635  * @cliexpar
1636  * @cliexstart{show nat44 static mappings}
1637  * Show NAT44 static mappings.
1638  * vpp# show nat44 static mappings
1639  * NAT44 static mappings:
1640  *  local 10.0.0.3 external 4.4.4.4 vrf 0
1641  *  tcp local 192.168.0.4:6303 external 4.4.4.3:3606 vrf 0
1642  *  tcp vrf 0 external 1.2.3.4:80  out2in-only
1643  *   local 10.100.10.10:8080 probability 80
1644  *   local 10.100.10.20:8080 probability 20
1645  *  tcp local 10.100.3.8:8080 external 169.10.10.1:80 vrf 0 twice-nat
1646  *  tcp local 10.0.0.10:3603 external GigabitEthernet0/a/0:6306 vrf 10
1647  * @cliexend
1648 ?*/
1649 VLIB_CLI_COMMAND (nat44_show_static_mappings_command, static) = {
1650   .path = "show nat44 static mappings",
1651   .short_help = "show nat44 static mappings",
1652   .function = nat44_show_static_mappings_command_fn,
1653 };
1654
1655 /*?
1656  * @cliexpar
1657  * @cliexstart{nat44 add interface address}
1658  * Use NAT44 pool address from specific interfce
1659  * To add NAT44 pool address from specific interface use:
1660  *  vpp# nat44 add interface address GigabitEthernet0/8/0
1661  * @cliexend
1662 ?*/
1663 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
1664     .path = "nat44 add interface address",
1665     .short_help = "nat44 add interface address <interface> [twice-nat] [del]",
1666     .function = snat_add_interface_address_command_fn,
1667 };
1668
1669 /*?
1670  * @cliexpar
1671  * @cliexstart{show nat44 interface address}
1672  * Show NAT44 pool address interfaces
1673  * vpp# show nat44 interface address
1674  * NAT44 pool address interfaces:
1675  *  GigabitEthernet0/a/0
1676  * NAT44 twice-nat pool address interfaces:
1677  *  GigabitEthernet0/8/0
1678  * @cliexend
1679 ?*/
1680 VLIB_CLI_COMMAND (nat44_show_interface_address_command, static) = {
1681   .path = "show nat44 interface address",
1682   .short_help = "show nat44 interface address",
1683   .function = nat44_show_interface_address_command_fn,
1684 };
1685
1686 /*?
1687  * @cliexpar
1688  * @cliexstart{show nat44 sessions}
1689  * Show NAT44 sessions.
1690  * @cliexend
1691 ?*/
1692 VLIB_CLI_COMMAND (nat44_show_sessions_command, static) = {
1693   .path = "show nat44 sessions",
1694   .short_help = "show nat44 sessions [detail]",
1695   .function = nat44_show_sessions_command_fn,
1696 };
1697
1698 /*?
1699  * @cliexpar
1700  * @cliexstart{nat44 del session}
1701  * To administratively delete NAT44 session by inside address and port use:
1702  *  vpp# nat44 del session in 10.0.0.3:6303 tcp
1703  * To administratively delete NAT44 session by outside address and port use:
1704  *  vpp# nat44 del session out 1.0.0.3:6033 udp
1705  * @cliexend
1706 ?*/
1707 VLIB_CLI_COMMAND (nat44_del_session_command, static) = {
1708     .path = "nat44 del session",
1709     .short_help = "nat44 del session in|out <addr>:<port> tcp|udp|icmp [vrf <id>]",
1710     .function = nat44_del_session_command_fn,
1711 };
1712
1713 /*?
1714  * @cliexpar
1715  * @cliexstart{nat44 forwarding}
1716  * Enable or disable forwarding
1717  * Forward packets which don't match existing translation
1718  * or static mapping instead of dropping them.
1719  * To enable forwarding, use:
1720  *  vpp# nat44 forwarding enable
1721  * To disable forwarding, use:
1722  *  vpp# nat44 forwarding disable
1723  * @cliexend
1724 ?*/
1725 VLIB_CLI_COMMAND (snat_forwarding_set_command, static) = {
1726   .path = "nat44 forwarding",
1727   .short_help = "nat44 forwarding enable|disable",
1728   .function = snat_forwarding_set_command_fn,
1729 };
1730
1731 /*?
1732  * @cliexpar
1733  * @cliexstart{nat44 deterministic add}
1734  * Create bijective mapping of inside address to outside address and port range
1735  * pairs, with the purpose of enabling deterministic NAT to reduce logging in
1736  * CGN deployments.
1737  * To create deterministic mapping between inside network 10.0.0.0/18 and
1738  * outside network 1.1.1.0/30 use:
1739  * # vpp# nat44 deterministic add in 10.0.0.0/18 out 1.1.1.0/30
1740  * @cliexend
1741 ?*/
1742 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
1743     .path = "nat44 deterministic add",
1744     .short_help = "nat44 deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
1745     .function = snat_det_map_command_fn,
1746 };
1747
1748 /*?
1749  * @cliexpar
1750  * @cliexpstart{show nat44 deterministic mappings}
1751  * Show NAT44 deterministic mappings
1752  * vpp# show nat44 deterministic mappings
1753  * NAT44 deterministic mappings:
1754  *  in 10.0.0.0/24 out 1.1.1.1/32
1755  *   outside address sharing ratio: 256
1756  *   number of ports per inside host: 252
1757  *   sessions number: 0
1758  * @cliexend
1759 ?*/
1760 VLIB_CLI_COMMAND (nat44_det_show_mappings_command, static) = {
1761     .path = "show nat44 deterministic mappings",
1762     .short_help = "show nat44 deterministic mappings",
1763     .function = nat44_det_show_mappings_command_fn,
1764 };
1765
1766 /*?
1767  * @cliexpar
1768  * @cliexstart{nat44 deterministic forward}
1769  * Return outside address and port range from inside address for deterministic
1770  * NAT.
1771  * To obtain outside address and port of inside host use:
1772  *  vpp# nat44 deterministic forward 10.0.0.2
1773  *  1.1.1.0:<1054-1068>
1774  * @cliexend
1775 ?*/
1776 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
1777     .path = "nat44 deterministic forward",
1778     .short_help = "nat44 deterministic forward <addr>",
1779     .function = snat_det_forward_command_fn,
1780 };
1781
1782 /*?
1783  * @cliexpar
1784  * @cliexstart{nat44 deterministic reverse}
1785  * Return inside address from outside address and port for deterministic NAT.
1786  * To obtain inside host address from outside address and port use:
1787  *  #vpp nat44 deterministic reverse 1.1.1.1:1276
1788  *  10.0.16.16
1789  * @cliexend
1790 ?*/
1791 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
1792     .path = "nat44 deterministic reverse",
1793     .short_help = "nat44 deterministic reverse <addr>:<port>",
1794     .function = snat_det_reverse_command_fn,
1795 };
1796
1797 /*?
1798  * @cliexpar
1799  * @cliexstart{set nat44 deterministic timeout}
1800  * Set values of timeouts for deterministic NAT (in seconds), use:
1801  *  vpp# set nat44 deterministic timeout udp 120 tcp-established 7500
1802  *  tcp-transitory 250 icmp 90
1803  * To reset default values use:
1804  *  vpp# set nat44 deterministic timeout reset
1805  * @cliexend
1806 ?*/
1807 VLIB_CLI_COMMAND (set_timeout_command, static) = {
1808   .path = "set nat44 deterministic timeout",
1809   .function = set_timeout_command_fn,
1810   .short_help =
1811     "set nat44 deterministic timeout [udp <sec> | tcp-established <sec> "
1812     "tcp-transitory <sec> | icmp <sec> | reset]",
1813 };
1814
1815 /*?
1816  * @cliexpar
1817  * @cliexstart{show nat44 deterministic timeouts}
1818  * Show values of timeouts for deterministic NAT.
1819  * vpp# show nat44 deterministic timeouts
1820  * udp timeout: 300sec
1821  * tcp-established timeout: 7440sec
1822  * tcp-transitory timeout: 240sec
1823  * icmp timeout: 60sec
1824  * @cliexend
1825 ?*/
1826 VLIB_CLI_COMMAND (nat44_det_show_timeouts_command, static) = {
1827   .path = "show nat44 deterministic timeouts",
1828   .short_help = "show nat44 deterministic timeouts",
1829   .function = nat44_det_show_timeouts_command_fn,
1830 };
1831
1832 /*?
1833  * @cliexpar
1834  * @cliexstart{show nat44 deterministic sessions}
1835  * Show NAT44 deterministic sessions.
1836  * vpp# show nat44 deterministic sessions
1837  * NAT44 deterministic sessions:
1838  *   in 10.0.0.3:3005 out 1.1.1.2:1146 external host 172.16.1.2:3006 state: udp-active expire: 306
1839  *   in 10.0.0.3:3000 out 1.1.1.2:1141 external host 172.16.1.2:3001 state: udp-active expire: 306
1840  *   in 10.0.0.4:3005 out 1.1.1.2:1177 external host 172.16.1.2:3006 state: udp-active expire: 306
1841  * @cliexend
1842 ?*/
1843 VLIB_CLI_COMMAND (nat44_det_show_sessions_command, static) = {
1844   .path = "show nat44 deterministic sessions",
1845   .short_help = "show nat44 deterministic sessions",
1846   .function = nat44_det_show_sessions_command_fn,
1847 };
1848
1849 /*?
1850  * @cliexpar
1851  * @cliexstart{nat44 deterministic close session out}
1852  * Close session using outside ip address and port
1853  * and external ip address and port, use:
1854  *  vpp# nat44 deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
1855  * @cliexend
1856 ?*/
1857 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
1858   .path = "nat44 deterministic close session out",
1859   .short_help = "nat44 deterministic close session out "
1860                 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
1861   .function = snat_det_close_session_out_fn,
1862 };
1863
1864 /*?
1865  * @cliexpar
1866  * @cliexstart{nat44 deterministic close session in}
1867  * Close session using inside ip address and port
1868  * and external ip address and port, use:
1869  *  vpp# nat44 deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
1870  * @cliexend
1871 ?*/
1872 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
1873   .path = "nat44 deterministic close session in",
1874   .short_help = "nat44 deterministic close session in "
1875                 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
1876   .function = snat_det_close_session_in_fn,
1877 };
1878
1879 /* *INDENT-ON* */
1880
1881 /*
1882  * fd.io coding-style-patch-verification: ON
1883  *
1884  * Local Variables:
1885  * eval: (c-set-style "gnu")
1886  * End:
1887  */