nat: user deletion function & extra metrics
[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 <nat/nat64.h>
24 #include <nat/nat_inlines.h>
25 #include <nat/nat44/inlines.h>
26 #include <nat/nat_affinity.h>
27 #include <vnet/fib/fib_table.h>
28 #include <nat/nat_ha.h>
29
30 #define UNSUPPORTED_IN_DET_MODE_STR \
31   "This command is unsupported in deterministic mode"
32 #define SUPPORTED_ONLY_IN_DET_MODE_STR \
33   "This command is supported only in deterministic mode"
34
35 static clib_error_t *
36 set_workers_command_fn (vlib_main_t * vm,
37                         unformat_input_t * input, vlib_cli_command_t * cmd)
38 {
39   unformat_input_t _line_input, *line_input = &_line_input;
40   snat_main_t *sm = &snat_main;
41   uword *bitmap = 0;
42   int rv = 0;
43   clib_error_t *error = 0;
44
45   if (sm->deterministic)
46     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
47
48   /* Get a line of input. */
49   if (!unformat_user (input, unformat_line_input, line_input))
50     return 0;
51
52   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
53     {
54       if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
55         ;
56       else
57         {
58           error = clib_error_return (0, "unknown input '%U'",
59                                      format_unformat_error, line_input);
60           goto done;
61         }
62     }
63
64   if (bitmap == 0)
65     {
66       error = clib_error_return (0, "List of workers must be specified.");
67       goto done;
68     }
69
70   rv = snat_set_workers (bitmap);
71
72   clib_bitmap_free (bitmap);
73
74   switch (rv)
75     {
76     case VNET_API_ERROR_INVALID_WORKER:
77       error = clib_error_return (0, "Invalid worker(s).");
78       goto done;
79     case VNET_API_ERROR_FEATURE_DISABLED:
80       error = clib_error_return (0,
81                                  "Supported only if 2 or more workes available.");
82       goto done;
83     default:
84       break;
85     }
86
87 done:
88   unformat_free (line_input);
89
90   return error;
91 }
92
93 static clib_error_t *
94 nat_show_workers_commnad_fn (vlib_main_t * vm, unformat_input_t * input,
95                              vlib_cli_command_t * cmd)
96 {
97   snat_main_t *sm = &snat_main;
98   u32 *worker;
99
100   if (sm->deterministic)
101     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
102
103   if (sm->num_workers > 1)
104     {
105       vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
106       /* *INDENT-OFF* */
107       vec_foreach (worker, sm->workers)
108         {
109           vlib_worker_thread_t *w =
110             vlib_worker_threads + *worker + sm->first_worker_index;
111           vlib_cli_output (vm, "  %s", w->name);
112         }
113       /* *INDENT-ON* */
114     }
115
116   return 0;
117 }
118
119 static clib_error_t *
120 nat44_session_cleanup_command_fn (vlib_main_t * vm,
121                                   unformat_input_t * input,
122                                   vlib_cli_command_t * cmd)
123 {
124   clib_error_t *error = 0;
125
126   nat44_force_session_cleanup ();
127
128   return error;
129 }
130
131 static clib_error_t *
132 snat_set_log_level_command_fn (vlib_main_t * vm,
133                                unformat_input_t * input,
134                                vlib_cli_command_t * cmd)
135 {
136   unformat_input_t _line_input, *line_input = &_line_input;
137   snat_main_t *sm = &snat_main;
138   u8 log_level = SNAT_LOG_NONE;
139   clib_error_t *error = 0;
140
141   /* Get a line of input. */
142   if (!unformat_user (input, unformat_line_input, line_input))
143     return 0;
144
145   if (!unformat (line_input, "%d", &log_level))
146     {
147       error = clib_error_return (0, "unknown input '%U'",
148                                  format_unformat_error, line_input);
149       goto done;
150     }
151   if (log_level > SNAT_LOG_DEBUG)
152     {
153       error = clib_error_return (0, "unknown logging level '%d'", log_level);
154       goto done;
155     }
156   sm->log_level = log_level;
157
158 done:
159   unformat_free (line_input);
160
161   return error;
162 }
163
164 static clib_error_t *
165 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
166                                               unformat_input_t * input,
167                                               vlib_cli_command_t * cmd)
168 {
169   unformat_input_t _line_input, *line_input = &_line_input;
170   u32 domain_id = 0;
171   u32 src_port = 0;
172   u8 enable = 1;
173   int rv = 0;
174   clib_error_t *error = 0;
175
176   /* Get a line of input. */
177   if (!unformat_user (input, unformat_line_input, line_input))
178     {
179       rv = snat_ipfix_logging_enable_disable (enable, domain_id,
180                                               (u16) src_port);
181       if (rv)
182         return clib_error_return (0, "ipfix logging enable failed");
183       return 0;
184     }
185
186   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
187     {
188       if (unformat (line_input, "domain %d", &domain_id))
189         ;
190       else if (unformat (line_input, "src-port %d", &src_port))
191         ;
192       else if (unformat (line_input, "disable"))
193         enable = 0;
194       else
195         {
196           error = clib_error_return (0, "unknown input '%U'",
197                                      format_unformat_error, line_input);
198           goto done;
199         }
200     }
201
202   rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
203
204   if (rv)
205     {
206       error = clib_error_return (0, "ipfix logging enable failed");
207       goto done;
208     }
209
210 done:
211   unformat_free (line_input);
212
213   return error;
214 }
215
216 static clib_error_t *
217 nat44_show_hash_commnad_fn (vlib_main_t * vm, unformat_input_t * input,
218                             vlib_cli_command_t * cmd)
219 {
220   snat_main_t *sm = &snat_main;
221   snat_main_per_thread_data_t *tsm;
222   nat_affinity_main_t *nam = &nat_affinity_main;
223   int i;
224   int verbose = 0;
225
226   if (unformat (input, "detail"))
227     verbose = 1;
228   else if (unformat (input, "verbose"))
229     verbose = 2;
230
231   vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->static_mapping_by_local,
232                    verbose);
233   vlib_cli_output (vm, "%U",
234                    format_bihash_8_8, &sm->static_mapping_by_external,
235                    verbose);
236   vec_foreach_index (i, sm->per_thread_data)
237   {
238     tsm = vec_elt_at_index (sm->per_thread_data, i);
239     vlib_cli_output (vm, "-------- thread %d %s --------\n",
240                      i, vlib_worker_threads[i].name);
241     if (sm->endpoint_dependent)
242       {
243         vlib_cli_output (vm, "%U", format_bihash_16_8, &tsm->in2out_ed,
244                          verbose);
245         vlib_cli_output (vm, "%U", format_bihash_16_8, &tsm->out2in_ed,
246                          verbose);
247       }
248     else
249       {
250         vlib_cli_output (vm, "%U", format_bihash_8_8, &tsm->in2out, verbose);
251         vlib_cli_output (vm, "%U", format_bihash_8_8, &tsm->out2in, verbose);
252       }
253     vlib_cli_output (vm, "%U", format_bihash_8_8, &tsm->user_hash, verbose);
254   }
255
256   if (sm->endpoint_dependent)
257     vlib_cli_output (vm, "%U", format_bihash_16_8, &nam->affinity_hash,
258                      verbose);
259   return 0;
260 }
261
262 static clib_error_t *
263 nat44_set_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
264                                               unformat_input_t * input,
265                                               vlib_cli_command_t * cmd)
266 {
267   unformat_input_t _line_input, *line_input = &_line_input;
268   snat_main_t *sm = &snat_main;
269   clib_error_t *error = 0;
270   u32 psid, psid_offset, psid_length, port_start, port_end;
271
272   if (sm->deterministic)
273     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
274
275   /* Get a line of input. */
276   if (!unformat_user (input, unformat_line_input, line_input))
277     return 0;
278
279   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
280     {
281       if (unformat (line_input, "default"))
282         nat_set_alloc_addr_and_port_default ();
283       else
284         if (unformat
285             (line_input, "map-e psid %d psid-offset %d psid-len %d", &psid,
286              &psid_offset, &psid_length))
287         nat_set_alloc_addr_and_port_mape ((u16) psid, (u16) psid_offset,
288                                           (u16) psid_length);
289       else
290         if (unformat
291             (line_input, "port-range %d - %d", &port_start, &port_end))
292         {
293           if (port_end <= port_start)
294             {
295               error =
296                 clib_error_return (0,
297                                    "The end-port must be greater than start-port");
298               goto done;
299             }
300           nat_set_alloc_addr_and_port_range ((u16) port_start,
301                                              (u16) port_end);
302         }
303       else
304         {
305           error = clib_error_return (0, "unknown input '%U'",
306                                      format_unformat_error, line_input);
307           goto done;
308         }
309     }
310
311 done:
312   unformat_free (line_input);
313
314   return error;
315 };
316
317 static clib_error_t *
318 nat44_show_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
319                                                unformat_input_t * input,
320                                                vlib_cli_command_t * cmd)
321 {
322   snat_main_t *sm = &snat_main;
323
324   if (sm->deterministic)
325     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
326
327   vlib_cli_output (vm, "NAT address and port: %U",
328                    format_nat_addr_and_port_alloc_alg,
329                    sm->addr_and_port_alloc_alg);
330   switch (sm->addr_and_port_alloc_alg)
331     {
332     case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
333       vlib_cli_output (vm, "  psid %d psid-offset %d psid-len %d", sm->psid,
334                        sm->psid_offset, sm->psid_length);
335       break;
336     case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
337       vlib_cli_output (vm, "  start-port %d end-port %d", sm->start_port,
338                        sm->end_port);
339       break;
340     default:
341       break;
342     }
343
344   return 0;
345 }
346
347 static clib_error_t *
348 nat_set_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
349                                  vlib_cli_command_t * cmd)
350 {
351   unformat_input_t _line_input, *line_input = &_line_input;
352   snat_main_t *sm = &snat_main;
353   clib_error_t *error = 0;
354   u32 mss;
355
356   /* Get a line of input. */
357   if (!unformat_user (input, unformat_line_input, line_input))
358     return 0;
359
360   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
361     {
362       if (unformat (line_input, "disable"))
363         sm->mss_clamping = 0;
364       else if (unformat (line_input, "%d", &mss))
365         {
366           sm->mss_clamping = (u16) mss;
367           sm->mss_value_net = clib_host_to_net_u16 (sm->mss_clamping);
368         }
369       else
370         {
371           error = clib_error_return (0, "unknown input '%U'",
372                                      format_unformat_error, line_input);
373           goto done;
374         }
375     }
376
377 done:
378   unformat_free (line_input);
379
380   return error;
381 }
382
383 static clib_error_t *
384 nat_show_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
385                                   vlib_cli_command_t * cmd)
386 {
387   snat_main_t *sm = &snat_main;
388
389   if (sm->mss_clamping)
390     vlib_cli_output (vm, "mss-clamping %d", sm->mss_clamping);
391   else
392     vlib_cli_output (vm, "mss-clamping disabled");
393
394   return 0;
395 }
396
397 static clib_error_t *
398 nat_ha_failover_command_fn (vlib_main_t * vm, unformat_input_t * input,
399                             vlib_cli_command_t * cmd)
400 {
401   unformat_input_t _line_input, *line_input = &_line_input;
402   ip4_address_t addr;
403   u32 port, session_refresh_interval = 10;
404   int rv;
405   clib_error_t *error = 0;
406
407   /* Get a line of input. */
408   if (!unformat_user (input, unformat_line_input, line_input))
409     return 0;
410
411   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
412     {
413       if (unformat (line_input, "%U:%u", unformat_ip4_address, &addr, &port))
414         ;
415       else
416         if (unformat
417             (line_input, "refresh-interval %u", &session_refresh_interval))
418         ;
419       else
420         {
421           error = clib_error_return (0, "unknown input '%U'",
422                                      format_unformat_error, line_input);
423           goto done;
424         }
425     }
426
427   rv = nat_ha_set_failover (&addr, (u16) port, session_refresh_interval);
428   if (rv)
429     error = clib_error_return (0, "set HA failover failed");
430
431 done:
432   unformat_free (line_input);
433
434   return error;
435 }
436
437 static clib_error_t *
438 nat_ha_listener_command_fn (vlib_main_t * vm, unformat_input_t * input,
439                             vlib_cli_command_t * cmd)
440 {
441   unformat_input_t _line_input, *line_input = &_line_input;
442   ip4_address_t addr;
443   u32 port, path_mtu = 512;
444   int rv;
445   clib_error_t *error = 0;
446
447   /* Get a line of input. */
448   if (!unformat_user (input, unformat_line_input, line_input))
449     return 0;
450
451   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
452     {
453       if (unformat (line_input, "%U:%u", unformat_ip4_address, &addr, &port))
454         ;
455       else if (unformat (line_input, "path-mtu %u", &path_mtu))
456         ;
457       else
458         {
459           error = clib_error_return (0, "unknown input '%U'",
460                                      format_unformat_error, line_input);
461           goto done;
462         }
463     }
464
465   rv = nat_ha_set_listener (&addr, (u16) port, path_mtu);
466   if (rv)
467     error = clib_error_return (0, "set HA listener failed");
468
469 done:
470   unformat_free (line_input);
471
472   return error;
473 }
474
475 static clib_error_t *
476 nat_show_ha_command_fn (vlib_main_t * vm, unformat_input_t * input,
477                         vlib_cli_command_t * cmd)
478 {
479   ip4_address_t addr;
480   u16 port;
481   u32 path_mtu, session_refresh_interval, resync_ack_missed;
482   u8 in_resync;
483
484   nat_ha_get_listener (&addr, &port, &path_mtu);
485   if (!port)
486     {
487       vlib_cli_output (vm, "NAT HA disabled\n");
488       return 0;
489     }
490
491   vlib_cli_output (vm, "LISTENER:\n");
492   vlib_cli_output (vm, "  %U:%u path-mtu %u\n",
493                    format_ip4_address, &addr, port, path_mtu);
494
495   nat_ha_get_failover (&addr, &port, &session_refresh_interval);
496   vlib_cli_output (vm, "FAILOVER:\n");
497   if (port)
498     vlib_cli_output (vm, "  %U:%u refresh-interval %usec\n",
499                      format_ip4_address, &addr, port,
500                      session_refresh_interval);
501   else
502     vlib_cli_output (vm, "  NA\n");
503
504   nat_ha_get_resync_status (&in_resync, &resync_ack_missed);
505   vlib_cli_output (vm, "RESYNC:\n");
506   if (in_resync)
507     vlib_cli_output (vm, "  in progress\n");
508   else
509     vlib_cli_output (vm, "  completed (%d ACK missed)\n", resync_ack_missed);
510
511   return 0;
512 }
513
514 static clib_error_t *
515 nat_ha_flush_command_fn (vlib_main_t * vm, unformat_input_t * input,
516                          vlib_cli_command_t * cmd)
517 {
518   nat_ha_flush (0);
519   return 0;
520 }
521
522 static clib_error_t *
523 nat_ha_resync_command_fn (vlib_main_t * vm, unformat_input_t * input,
524                           vlib_cli_command_t * cmd)
525 {
526   clib_error_t *error = 0;
527
528   if (nat_ha_resync (0, 0, 0))
529     error = clib_error_return (0, "NAT HA resync already running");
530
531   return error;
532 }
533
534 static clib_error_t *
535 add_address_command_fn (vlib_main_t * vm,
536                         unformat_input_t * input, vlib_cli_command_t * cmd)
537 {
538   unformat_input_t _line_input, *line_input = &_line_input;
539   snat_main_t *sm = &snat_main;
540   ip4_address_t start_addr, end_addr, this_addr;
541   u32 start_host_order, end_host_order;
542   u32 vrf_id = ~0;
543   int i, count;
544   int is_add = 1;
545   int rv = 0;
546   clib_error_t *error = 0;
547   u8 twice_nat = 0;
548
549   if (sm->deterministic)
550     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
551
552   /* Get a line of input. */
553   if (!unformat_user (input, unformat_line_input, line_input))
554     return 0;
555
556   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
557     {
558       if (unformat (line_input, "%U - %U",
559                     unformat_ip4_address, &start_addr,
560                     unformat_ip4_address, &end_addr))
561         ;
562       else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
563         ;
564       else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
565         end_addr = start_addr;
566       else if (unformat (line_input, "twice-nat"))
567         twice_nat = 1;
568       else if (unformat (line_input, "del"))
569         is_add = 0;
570       else
571         {
572           error = clib_error_return (0, "unknown input '%U'",
573                                      format_unformat_error, line_input);
574           goto done;
575         }
576     }
577
578   if (sm->static_mapping_only)
579     {
580       error = clib_error_return (0, "static mapping only mode");
581       goto done;
582     }
583
584   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
585   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
586
587   if (end_host_order < start_host_order)
588     {
589       error = clib_error_return (0, "end address less than start address");
590       goto done;
591     }
592
593   count = (end_host_order - start_host_order) + 1;
594
595   if (count > 1024)
596     nat_log_info ("%U - %U, %d addresses...",
597                   format_ip4_address, &start_addr,
598                   format_ip4_address, &end_addr, count);
599
600   this_addr = start_addr;
601
602   for (i = 0; i < count; i++)
603     {
604       if (is_add)
605         rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
606       else
607         rv = snat_del_address (sm, this_addr, 0, twice_nat);
608
609       switch (rv)
610         {
611         case VNET_API_ERROR_VALUE_EXIST:
612           error = clib_error_return (0, "NAT address already in use.");
613           goto done;
614         case VNET_API_ERROR_NO_SUCH_ENTRY:
615           error = clib_error_return (0, "NAT address not exist.");
616           goto done;
617         case VNET_API_ERROR_UNSPECIFIED:
618           error =
619             clib_error_return (0, "NAT address used in static mapping.");
620           goto done;
621         case VNET_API_ERROR_FEATURE_DISABLED:
622           error =
623             clib_error_return (0,
624                                "twice NAT available only for endpoint-dependent mode.");
625           goto done;
626         default:
627           break;
628         }
629
630       if (sm->out2in_dpo)
631         nat44_add_del_address_dpo (this_addr, is_add);
632
633       increment_v4_address (&this_addr);
634     }
635
636 done:
637   unformat_free (line_input);
638
639   return error;
640 }
641
642 static clib_error_t *
643 nat44_show_summary_command_fn (vlib_main_t * vm, unformat_input_t * input,
644                                vlib_cli_command_t * cmd)
645 {
646   snat_main_per_thread_data_t *tsm;
647   snat_main_t *sm = &snat_main;
648   snat_address_t *a;
649   snat_session_t *s;
650   u32 free_ports, free_ports_addr;
651
652   if (sm->deterministic)
653     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
654
655   if (sm->endpoint_dependent)
656     vlib_cli_output (vm, "mode: endpoint-depenent");
657
658   // print timeouts
659   vlib_cli_output (vm, "icmp timeout: %u", sm->icmp_timeout);
660   vlib_cli_output (vm, "udp timeout: %u", sm->udp_timeout);
661
662   vlib_cli_output (vm, "tcp established timeout: %u",
663                    sm->tcp_established_timeout);
664   vlib_cli_output (vm, "tcp transitory timeout: %u",
665                    sm->tcp_transitory_timeout);
666
667   // print session configuration values
668   vlib_cli_output (vm, "max translations: %u", sm->max_translations);
669   vlib_cli_output (vm, "max translations per user: %u",
670                    sm->max_translations_per_user);
671
672   // do we need also twice-nat addresses ??
673   if (vec_len (sm->addresses))
674     {
675       free_ports = 0;
676       /* *INDENT-OFF* */
677       vec_foreach (a, sm->addresses)
678         {
679           free_ports_addr = sm->port_per_thread;
680
681           #define _(N, i, n, s) \
682             free_ports_addr -= a->busy_##n##_ports;
683             foreach_snat_protocol
684           #undef _
685
686           vlib_cli_output (vm, "addr: %U free ports: %u out of: %u",
687               format_ip4_address, &a->addr, free_ports_addr, sm->port_per_thread);
688
689           free_ports += free_ports_addr;
690         }
691       /* *INDENT-ON* */
692       vlib_cli_output (vm, "free ports overall: %u out of: %u",
693                        free_ports,
694                        vec_len (sm->addresses) * sm->port_per_thread);
695     }
696
697   u32 count = 0;
698   u32 transitory = 0;
699   u32 established = 0;
700
701   if (sm->num_workers > 1)
702     {
703       /* *INDENT-OFF* */
704       vec_foreach (tsm, sm->per_thread_data)
705         {
706           count += vec_len (tsm->sessions);
707           pool_foreach (s, tsm->sessions,
708           ({
709             if (s->state)
710               transitory++;
711             else
712               established++;
713           }));
714         }
715       /* *INDENT-ON* */
716     }
717   else
718     {
719       tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
720       /* *INDENT-OFF* */
721       pool_foreach (s, tsm->sessions,
722       ({
723         if (s->state)
724           transitory++;
725         else
726           established++;
727       }));
728       /* *INDENT-ON* */
729       count = vec_len (tsm->sessions);
730     }
731   vlib_cli_output (vm, "established sessions: %u", established);
732   vlib_cli_output (vm, "transitory sessions: %u", transitory);
733   vlib_cli_output (vm, "sessions: %u", count);
734   return 0;
735 }
736
737 static clib_error_t *
738 nat44_show_addresses_command_fn (vlib_main_t * vm, unformat_input_t * input,
739                                  vlib_cli_command_t * cmd)
740 {
741   snat_main_t *sm = &snat_main;
742   snat_address_t *ap;
743
744   if (sm->deterministic)
745     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
746
747   vlib_cli_output (vm, "NAT44 pool addresses:");
748   /* *INDENT-OFF* */
749   vec_foreach (ap, sm->addresses)
750     {
751       vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
752       if (ap->fib_index != ~0)
753           vlib_cli_output (vm, "  tenant VRF: %u",
754             fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
755       else
756         vlib_cli_output (vm, "  tenant VRF independent");
757     #define _(N, i, n, s) \
758       vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
759       foreach_snat_protocol
760     #undef _
761     }
762   vlib_cli_output (vm, "NAT44 twice-nat pool addresses:");
763   vec_foreach (ap, sm->twice_nat_addresses)
764     {
765       vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
766       if (ap->fib_index != ~0)
767           vlib_cli_output (vm, "  tenant VRF: %u",
768             fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
769       else
770         vlib_cli_output (vm, "  tenant VRF independent");
771     #define _(N, i, n, s) \
772       vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
773       foreach_snat_protocol
774     #undef _
775     }
776   /* *INDENT-ON* */
777   return 0;
778 }
779
780 static clib_error_t *
781 snat_feature_command_fn (vlib_main_t * vm,
782                          unformat_input_t * input, vlib_cli_command_t * cmd)
783 {
784   unformat_input_t _line_input, *line_input = &_line_input;
785   vnet_main_t *vnm = vnet_get_main ();
786   clib_error_t *error = 0;
787   u32 sw_if_index;
788   u32 *inside_sw_if_indices = 0;
789   u32 *outside_sw_if_indices = 0;
790   u8 is_output_feature = 0;
791   int is_del = 0;
792   int i;
793
794   sw_if_index = ~0;
795
796   /* Get a line of input. */
797   if (!unformat_user (input, unformat_line_input, line_input))
798     return 0;
799
800   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
801     {
802       if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
803                     vnm, &sw_if_index))
804         vec_add1 (inside_sw_if_indices, sw_if_index);
805       else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
806                          vnm, &sw_if_index))
807         vec_add1 (outside_sw_if_indices, sw_if_index);
808       else if (unformat (line_input, "output-feature"))
809         is_output_feature = 1;
810       else if (unformat (line_input, "del"))
811         is_del = 1;
812       else
813         {
814           error = clib_error_return (0, "unknown input '%U'",
815                                      format_unformat_error, line_input);
816           goto done;
817         }
818     }
819
820   if (vec_len (inside_sw_if_indices))
821     {
822       for (i = 0; i < vec_len (inside_sw_if_indices); i++)
823         {
824           sw_if_index = inside_sw_if_indices[i];
825           if (is_output_feature)
826             {
827               if (snat_interface_add_del_output_feature
828                   (sw_if_index, 1, is_del))
829                 {
830                   error = clib_error_return (0, "%s %U failed",
831                                              is_del ? "del" : "add",
832                                              format_vnet_sw_if_index_name,
833                                              vnm, sw_if_index);
834                   goto done;
835                 }
836             }
837           else
838             {
839               if (snat_interface_add_del (sw_if_index, 1, is_del))
840                 {
841                   error = clib_error_return (0, "%s %U failed",
842                                              is_del ? "del" : "add",
843                                              format_vnet_sw_if_index_name,
844                                              vnm, sw_if_index);
845                   goto done;
846                 }
847             }
848         }
849     }
850
851   if (vec_len (outside_sw_if_indices))
852     {
853       for (i = 0; i < vec_len (outside_sw_if_indices); i++)
854         {
855           sw_if_index = outside_sw_if_indices[i];
856           if (is_output_feature)
857             {
858               if (snat_interface_add_del_output_feature
859                   (sw_if_index, 0, is_del))
860                 {
861                   error = clib_error_return (0, "%s %U failed",
862                                              is_del ? "del" : "add",
863                                              format_vnet_sw_if_index_name,
864                                              vnm, sw_if_index);
865                   goto done;
866                 }
867             }
868           else
869             {
870               if (snat_interface_add_del (sw_if_index, 0, is_del))
871                 {
872                   error = clib_error_return (0, "%s %U failed",
873                                              is_del ? "del" : "add",
874                                              format_vnet_sw_if_index_name,
875                                              vnm, sw_if_index);
876                   goto done;
877                 }
878             }
879         }
880     }
881
882 done:
883   unformat_free (line_input);
884   vec_free (inside_sw_if_indices);
885   vec_free (outside_sw_if_indices);
886
887   return error;
888 }
889
890 static clib_error_t *
891 nat44_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
892                                   vlib_cli_command_t * cmd)
893 {
894   snat_main_t *sm = &snat_main;
895   snat_interface_t *i;
896   vnet_main_t *vnm = vnet_get_main ();
897
898   vlib_cli_output (vm, "NAT44 interfaces:");
899   /* *INDENT-OFF* */
900   pool_foreach (i, sm->interfaces,
901   ({
902     vlib_cli_output (vm, " %U %s", format_vnet_sw_if_index_name, vnm,
903                      i->sw_if_index,
904                      (nat_interface_is_inside(i) &&
905                       nat_interface_is_outside(i)) ? "in out" :
906                      (nat_interface_is_inside(i) ? "in" : "out"));
907   }));
908
909   pool_foreach (i, sm->output_feature_interfaces,
910   ({
911     vlib_cli_output (vm, " %U output-feature %s",
912                      format_vnet_sw_if_index_name, vnm,
913                      i->sw_if_index,
914                      (nat_interface_is_inside(i) &&
915                       nat_interface_is_outside(i)) ? "in out" :
916                      (nat_interface_is_inside(i) ? "in" : "out"));
917   }));
918   /* *INDENT-ON* */
919
920   return 0;
921 }
922
923 static clib_error_t *
924 add_static_mapping_command_fn (vlib_main_t * vm,
925                                unformat_input_t * input,
926                                vlib_cli_command_t * cmd)
927 {
928   unformat_input_t _line_input, *line_input = &_line_input;
929   snat_main_t *sm = &snat_main;
930   clib_error_t *error = 0;
931   ip4_address_t l_addr, e_addr;
932   u32 l_port = 0, e_port = 0, vrf_id = ~0;
933   int is_add = 1;
934   int addr_only = 1;
935   u32 sw_if_index = ~0;
936   vnet_main_t *vnm = vnet_get_main ();
937   int rv;
938   snat_protocol_t proto = ~0;
939   u8 proto_set = 0;
940   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
941   u8 out2in_only = 0;
942
943   if (sm->deterministic)
944     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
945
946   /* Get a line of input. */
947   if (!unformat_user (input, unformat_line_input, line_input))
948     return 0;
949
950   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
951     {
952       if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
953                     &l_port))
954         addr_only = 0;
955       else
956         if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
957         ;
958       else if (unformat (line_input, "external %U %u", unformat_ip4_address,
959                          &e_addr, &e_port))
960         addr_only = 0;
961       else if (unformat (line_input, "external %U", unformat_ip4_address,
962                          &e_addr))
963         ;
964       else if (unformat (line_input, "external %U %u",
965                          unformat_vnet_sw_interface, vnm, &sw_if_index,
966                          &e_port))
967         addr_only = 0;
968
969       else if (unformat (line_input, "external %U",
970                          unformat_vnet_sw_interface, vnm, &sw_if_index))
971         ;
972       else if (unformat (line_input, "vrf %u", &vrf_id))
973         ;
974       else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
975         proto_set = 1;
976       else if (unformat (line_input, "twice-nat"))
977         twice_nat = TWICE_NAT;
978       else if (unformat (line_input, "self-twice-nat"))
979         twice_nat = TWICE_NAT_SELF;
980       else if (unformat (line_input, "out2in-only"))
981         out2in_only = 1;
982       else if (unformat (line_input, "del"))
983         is_add = 0;
984       else
985         {
986           error = clib_error_return (0, "unknown input: '%U'",
987                                      format_unformat_error, line_input);
988           goto done;
989         }
990     }
991
992   if (twice_nat && addr_only)
993     {
994       error = clib_error_return (0, "twice NAT only for 1:1 NAPT");
995       goto done;
996     }
997
998   if (!addr_only && !proto_set)
999     {
1000       error = clib_error_return (0, "missing protocol");
1001       goto done;
1002     }
1003
1004   rv = snat_add_static_mapping (l_addr, e_addr, (u16) l_port, (u16) e_port,
1005                                 vrf_id, addr_only, sw_if_index, proto, is_add,
1006                                 twice_nat, out2in_only, 0, 0);
1007
1008   switch (rv)
1009     {
1010     case VNET_API_ERROR_INVALID_VALUE:
1011       error = clib_error_return (0, "External port already in use.");
1012       goto done;
1013     case VNET_API_ERROR_NO_SUCH_ENTRY:
1014       if (is_add)
1015         error = clib_error_return (0, "External address must be allocated.");
1016       else
1017         error = clib_error_return (0, "Mapping not exist.");
1018       goto done;
1019     case VNET_API_ERROR_NO_SUCH_FIB:
1020       error = clib_error_return (0, "No such VRF id.");
1021       goto done;
1022     case VNET_API_ERROR_VALUE_EXIST:
1023       error = clib_error_return (0, "Mapping already exist.");
1024       goto done;
1025     case VNET_API_ERROR_FEATURE_DISABLED:
1026       error =
1027         clib_error_return (0,
1028                            "twice-nat/out2in-only available only for endpoint-dependent mode.");
1029       goto done;
1030     default:
1031       break;
1032     }
1033
1034 done:
1035   unformat_free (line_input);
1036
1037   return error;
1038 }
1039
1040 static clib_error_t *
1041 add_identity_mapping_command_fn (vlib_main_t * vm,
1042                                  unformat_input_t * input,
1043                                  vlib_cli_command_t * cmd)
1044 {
1045   unformat_input_t _line_input, *line_input = &_line_input;
1046   snat_main_t *sm = &snat_main;
1047   clib_error_t *error = 0;
1048   ip4_address_t addr;
1049   u32 port = 0, vrf_id = ~0;
1050   int is_add = 1;
1051   int addr_only = 1;
1052   u32 sw_if_index = ~0;
1053   vnet_main_t *vnm = vnet_get_main ();
1054   int rv;
1055   snat_protocol_t proto;
1056
1057   if (sm->deterministic)
1058     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1059
1060   addr.as_u32 = 0;
1061
1062   /* Get a line of input. */
1063   if (!unformat_user (input, unformat_line_input, line_input))
1064     return 0;
1065
1066   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1067     {
1068       if (unformat (line_input, "%U", unformat_ip4_address, &addr))
1069         ;
1070       else if (unformat (line_input, "external %U",
1071                          unformat_vnet_sw_interface, vnm, &sw_if_index))
1072         ;
1073       else if (unformat (line_input, "vrf %u", &vrf_id))
1074         ;
1075       else if (unformat (line_input, "%U %u", unformat_snat_protocol, &proto,
1076                          &port))
1077         addr_only = 0;
1078       else if (unformat (line_input, "del"))
1079         is_add = 0;
1080       else
1081         {
1082           error = clib_error_return (0, "unknown input: '%U'",
1083                                      format_unformat_error, line_input);
1084           goto done;
1085         }
1086     }
1087
1088   rv = snat_add_static_mapping (addr, addr, (u16) port, (u16) port,
1089                                 vrf_id, addr_only, sw_if_index, proto, is_add,
1090                                 0, 0, 0, 1);
1091
1092   switch (rv)
1093     {
1094     case VNET_API_ERROR_INVALID_VALUE:
1095       error = clib_error_return (0, "External port already in use.");
1096       goto done;
1097     case VNET_API_ERROR_NO_SUCH_ENTRY:
1098       if (is_add)
1099         error = clib_error_return (0, "External address must be allocated.");
1100       else
1101         error = clib_error_return (0, "Mapping not exist.");
1102       goto done;
1103     case VNET_API_ERROR_NO_SUCH_FIB:
1104       error = clib_error_return (0, "No such VRF id.");
1105       goto done;
1106     case VNET_API_ERROR_VALUE_EXIST:
1107       error = clib_error_return (0, "Mapping already exist.");
1108       goto done;
1109     default:
1110       break;
1111     }
1112
1113 done:
1114   unformat_free (line_input);
1115
1116   return error;
1117 }
1118
1119 static clib_error_t *
1120 add_lb_static_mapping_command_fn (vlib_main_t * vm,
1121                                   unformat_input_t * input,
1122                                   vlib_cli_command_t * cmd)
1123 {
1124   unformat_input_t _line_input, *line_input = &_line_input;
1125   snat_main_t *sm = &snat_main;
1126   clib_error_t *error = 0;
1127   ip4_address_t l_addr, e_addr;
1128   u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0, affinity = 0;
1129   int is_add = 1;
1130   int rv;
1131   snat_protocol_t proto;
1132   u8 proto_set = 0;
1133   nat44_lb_addr_port_t *locals = 0, local;
1134   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1135   u8 out2in_only = 0;
1136
1137   if (sm->deterministic)
1138     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1139
1140   /* Get a line of input. */
1141   if (!unformat_user (input, unformat_line_input, line_input))
1142     return 0;
1143
1144   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1145     {
1146       if (unformat (line_input, "local %U:%u probability %u",
1147                     unformat_ip4_address, &l_addr, &l_port, &probability))
1148         {
1149           clib_memset (&local, 0, sizeof (local));
1150           local.addr = l_addr;
1151           local.port = (u16) l_port;
1152           local.probability = (u8) probability;
1153           vec_add1 (locals, local);
1154         }
1155       else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1156                          unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1157                          &probability))
1158         {
1159           clib_memset (&local, 0, sizeof (local));
1160           local.addr = l_addr;
1161           local.port = (u16) l_port;
1162           local.probability = (u8) probability;
1163           local.vrf_id = vrf_id;
1164           vec_add1 (locals, local);
1165         }
1166       else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1167                          &e_addr, &e_port))
1168         ;
1169       else if (unformat (line_input, "protocol %U", unformat_snat_protocol,
1170                          &proto))
1171         proto_set = 1;
1172       else if (unformat (line_input, "twice-nat"))
1173         twice_nat = TWICE_NAT;
1174       else if (unformat (line_input, "self-twice-nat"))
1175         twice_nat = TWICE_NAT_SELF;
1176       else if (unformat (line_input, "out2in-only"))
1177         out2in_only = 1;
1178       else if (unformat (line_input, "del"))
1179         is_add = 0;
1180       else if (unformat (line_input, "affinity %u", &affinity))
1181         ;
1182       else
1183         {
1184           error = clib_error_return (0, "unknown input: '%U'",
1185                                      format_unformat_error, line_input);
1186           goto done;
1187         }
1188     }
1189
1190   if (vec_len (locals) < 2)
1191     {
1192       error = clib_error_return (0, "at least two local must be set");
1193       goto done;
1194     }
1195
1196   if (!proto_set)
1197     {
1198       error = clib_error_return (0, "missing protocol");
1199       goto done;
1200     }
1201
1202   rv = nat44_add_del_lb_static_mapping (e_addr, (u16) e_port, proto, locals,
1203                                         is_add, twice_nat, out2in_only, 0,
1204                                         affinity);
1205
1206   switch (rv)
1207     {
1208     case VNET_API_ERROR_INVALID_VALUE:
1209       error = clib_error_return (0, "External port already in use.");
1210       goto done;
1211     case VNET_API_ERROR_NO_SUCH_ENTRY:
1212       if (is_add)
1213         error = clib_error_return (0, "External address must be allocated.");
1214       else
1215         error = clib_error_return (0, "Mapping not exist.");
1216       goto done;
1217     case VNET_API_ERROR_VALUE_EXIST:
1218       error = clib_error_return (0, "Mapping already exist.");
1219       goto done;
1220     case VNET_API_ERROR_FEATURE_DISABLED:
1221       error =
1222         clib_error_return (0, "Available only for endpoint-dependent mode.");
1223       goto done;
1224     default:
1225       break;
1226     }
1227
1228 done:
1229   unformat_free (line_input);
1230   vec_free (locals);
1231
1232   return error;
1233 }
1234
1235 static clib_error_t *
1236 add_lb_backend_command_fn (vlib_main_t * vm,
1237                            unformat_input_t * input, vlib_cli_command_t * cmd)
1238 {
1239   unformat_input_t _line_input, *line_input = &_line_input;
1240   snat_main_t *sm = &snat_main;
1241   clib_error_t *error = 0;
1242   ip4_address_t l_addr, e_addr;
1243   u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
1244   int is_add = 1;
1245   int rv;
1246   snat_protocol_t proto;
1247   u8 proto_set = 0;
1248
1249   if (sm->deterministic)
1250     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1251
1252   /* Get a line of input. */
1253   if (!unformat_user (input, unformat_line_input, line_input))
1254     return 0;
1255
1256   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1257     {
1258       if (unformat (line_input, "local %U:%u probability %u",
1259                     unformat_ip4_address, &l_addr, &l_port, &probability))
1260         ;
1261       else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1262                          unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1263                          &probability))
1264         ;
1265       else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1266                          &e_addr, &e_port))
1267         ;
1268       else if (unformat (line_input, "protocol %U", unformat_snat_protocol,
1269                          &proto))
1270         proto_set = 1;
1271       else if (unformat (line_input, "del"))
1272         is_add = 0;
1273       else
1274         {
1275           error = clib_error_return (0, "unknown input: '%U'",
1276                                      format_unformat_error, line_input);
1277           goto done;
1278         }
1279     }
1280
1281   if (!l_port || !e_port)
1282     {
1283       error = clib_error_return (0, "local or external must be set");
1284       goto done;
1285     }
1286
1287   if (!proto_set)
1288     {
1289       error = clib_error_return (0, "missing protocol");
1290       goto done;
1291     }
1292
1293   rv =
1294     nat44_lb_static_mapping_add_del_local (e_addr, (u16) e_port, l_addr,
1295                                            l_port, proto, vrf_id, probability,
1296                                            is_add);
1297
1298   switch (rv)
1299     {
1300     case VNET_API_ERROR_INVALID_VALUE:
1301       error = clib_error_return (0, "External is not load-balancing static "
1302                                  "mapping.");
1303       goto done;
1304     case VNET_API_ERROR_NO_SUCH_ENTRY:
1305       error = clib_error_return (0, "Mapping or back-end not exist.");
1306       goto done;
1307     case VNET_API_ERROR_VALUE_EXIST:
1308       error = clib_error_return (0, "Back-end already exist.");
1309       goto done;
1310     case VNET_API_ERROR_FEATURE_DISABLED:
1311       error =
1312         clib_error_return (0, "Available only for endpoint-dependent mode.");
1313       goto done;
1314     case VNET_API_ERROR_UNSPECIFIED:
1315       error = clib_error_return (0, "At least two back-ends must remain");
1316       goto done;
1317     default:
1318       break;
1319     }
1320
1321 done:
1322   unformat_free (line_input);
1323
1324   return error;
1325 }
1326
1327 static clib_error_t *
1328 nat44_show_static_mappings_command_fn (vlib_main_t * vm,
1329                                        unformat_input_t * input,
1330                                        vlib_cli_command_t * cmd)
1331 {
1332   snat_main_t *sm = &snat_main;
1333   snat_static_mapping_t *m;
1334   snat_static_map_resolve_t *rp;
1335
1336   if (sm->deterministic)
1337     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1338
1339   vlib_cli_output (vm, "NAT44 static mappings:");
1340   /* *INDENT-OFF* */
1341   pool_foreach (m, sm->static_mappings,
1342   ({
1343     vlib_cli_output (vm, " %U", format_snat_static_mapping, m);
1344   }));
1345   vec_foreach (rp, sm->to_resolve)
1346     vlib_cli_output (vm, " %U", format_snat_static_map_to_resolve, rp);
1347   /* *INDENT-ON* */
1348
1349   return 0;
1350 }
1351
1352 static clib_error_t *
1353 snat_add_interface_address_command_fn (vlib_main_t * vm,
1354                                        unformat_input_t * input,
1355                                        vlib_cli_command_t * cmd)
1356 {
1357   snat_main_t *sm = &snat_main;
1358   unformat_input_t _line_input, *line_input = &_line_input;
1359   u32 sw_if_index;
1360   int rv;
1361   int is_del = 0;
1362   clib_error_t *error = 0;
1363   u8 twice_nat = 0;
1364
1365   if (sm->deterministic)
1366     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1367
1368   /* Get a line of input. */
1369   if (!unformat_user (input, unformat_line_input, line_input))
1370     return 0;
1371
1372   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1373     {
1374       if (unformat (line_input, "%U", unformat_vnet_sw_interface,
1375                     sm->vnet_main, &sw_if_index))
1376         ;
1377       else if (unformat (line_input, "twice-nat"))
1378         twice_nat = 1;
1379       else if (unformat (line_input, "del"))
1380         is_del = 1;
1381       else
1382         {
1383           error = clib_error_return (0, "unknown input '%U'",
1384                                      format_unformat_error, line_input);
1385           goto done;
1386         }
1387     }
1388
1389   rv = snat_add_interface_address (sm, sw_if_index, is_del, twice_nat);
1390
1391   switch (rv)
1392     {
1393     case 0:
1394       break;
1395
1396     default:
1397       error = clib_error_return (0, "snat_add_interface_address returned %d",
1398                                  rv);
1399       goto done;
1400     }
1401
1402 done:
1403   unformat_free (line_input);
1404
1405   return error;
1406 }
1407
1408 static clib_error_t *
1409 nat44_show_interface_address_command_fn (vlib_main_t * vm,
1410                                          unformat_input_t * input,
1411                                          vlib_cli_command_t * cmd)
1412 {
1413   snat_main_t *sm = &snat_main;
1414   vnet_main_t *vnm = vnet_get_main ();
1415   u32 *sw_if_index;
1416
1417   if (sm->deterministic)
1418     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1419
1420   /* *INDENT-OFF* */
1421   vlib_cli_output (vm, "NAT44 pool address interfaces:");
1422   vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
1423     {
1424       vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1425                        *sw_if_index);
1426     }
1427   vlib_cli_output (vm, "NAT44 twice-nat pool address interfaces:");
1428   vec_foreach (sw_if_index, sm->auto_add_sw_if_indices_twice_nat)
1429     {
1430       vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1431                        *sw_if_index);
1432     }
1433   /* *INDENT-ON* */
1434
1435   return 0;
1436 }
1437
1438 static clib_error_t *
1439 nat44_show_sessions_command_fn (vlib_main_t * vm, unformat_input_t * input,
1440                                 vlib_cli_command_t * cmd)
1441 {
1442   int verbose = 0;
1443   snat_main_t *sm = &snat_main;
1444   snat_main_per_thread_data_t *tsm;
1445   snat_user_t *u;
1446   int i = 0;
1447
1448   if (sm->deterministic)
1449     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1450
1451   if (unformat (input, "detail"))
1452     verbose = 1;
1453
1454   vlib_cli_output (vm, "NAT44 sessions:");
1455
1456   /* *INDENT-OFF* */
1457   vec_foreach_index (i, sm->per_thread_data)
1458     {
1459       tsm = vec_elt_at_index (sm->per_thread_data, i);
1460
1461       vlib_cli_output (vm, "-------- thread %d %s: %d sessions --------\n",
1462                        i, vlib_worker_threads[i].name,
1463                        pool_elts (tsm->sessions));
1464       pool_foreach (u, tsm->users,
1465       ({
1466         vlib_cli_output (vm, "  %U", format_snat_user, tsm, u, verbose);
1467       }));
1468     }
1469   /* *INDENT-ON* */
1470
1471   return 0;
1472 }
1473
1474 static clib_error_t *
1475 nat44_del_user_command_fn (vlib_main_t * vm,
1476                            unformat_input_t * input, vlib_cli_command_t * cmd)
1477 {
1478   snat_main_t *sm = &snat_main;
1479   unformat_input_t _line_input, *line_input = &_line_input;
1480   clib_error_t *error = 0;
1481   ip4_address_t addr;
1482   u32 fib_index = 0;
1483   int rv;
1484
1485   if (sm->deterministic)
1486     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1487
1488   /* Get a line of input. */
1489   if (!unformat_user (input, unformat_line_input, line_input))
1490     return 0;
1491
1492   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1493     {
1494       if (unformat (line_input, "%U", unformat_ip4_address, &addr))
1495         ;
1496       else if (unformat (line_input, "fib %u", &fib_index))
1497         ;
1498       else
1499         {
1500           error = clib_error_return (0, "unknown input '%U'",
1501                                      format_unformat_error, line_input);
1502           goto done;
1503         }
1504     }
1505
1506   rv = nat44_user_del (&addr, fib_index);
1507
1508   if (!rv)
1509     {
1510       error = clib_error_return (0, "nat44_user_del returned %d", rv);
1511     }
1512
1513 done:
1514   unformat_free (line_input);
1515
1516   return error;
1517 }
1518
1519 static clib_error_t *
1520 nat44_del_session_command_fn (vlib_main_t * vm,
1521                               unformat_input_t * input,
1522                               vlib_cli_command_t * cmd)
1523 {
1524   snat_main_t *sm = &snat_main;
1525   unformat_input_t _line_input, *line_input = &_line_input;
1526   int is_in = 0, is_ed = 0;
1527   clib_error_t *error = 0;
1528   ip4_address_t addr, eh_addr;
1529   u32 port = 0, eh_port = 0, vrf_id = sm->outside_vrf_id;
1530   snat_protocol_t proto;
1531   int rv;
1532
1533   if (sm->deterministic)
1534     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1535
1536   /* Get a line of input. */
1537   if (!unformat_user (input, unformat_line_input, line_input))
1538     return 0;
1539
1540   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1541     {
1542       if (unformat
1543           (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
1544            unformat_snat_protocol, &proto))
1545         ;
1546       else if (unformat (line_input, "in"))
1547         {
1548           is_in = 1;
1549           vrf_id = sm->inside_vrf_id;
1550         }
1551       else if (unformat (line_input, "out"))
1552         {
1553           is_in = 0;
1554           vrf_id = sm->outside_vrf_id;
1555         }
1556       else if (unformat (line_input, "vrf %u", &vrf_id))
1557         ;
1558       else
1559         if (unformat
1560             (line_input, "external-host %U:%u", unformat_ip4_address,
1561              &eh_addr, &eh_port))
1562         is_ed = 1;
1563       else
1564         {
1565           error = clib_error_return (0, "unknown input '%U'",
1566                                      format_unformat_error, line_input);
1567           goto done;
1568         }
1569     }
1570
1571   if (is_ed)
1572     rv =
1573       nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port,
1574                             snat_proto_to_ip_proto (proto), vrf_id, is_in);
1575   else
1576     rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
1577
1578   switch (rv)
1579     {
1580     case 0:
1581       break;
1582
1583     default:
1584       error = clib_error_return (0, "nat44_del_session returned %d", rv);
1585       goto done;
1586     }
1587
1588 done:
1589   unformat_free (line_input);
1590
1591   return error;
1592 }
1593
1594 static clib_error_t *
1595 snat_forwarding_set_command_fn (vlib_main_t * vm,
1596                                 unformat_input_t * input,
1597                                 vlib_cli_command_t * cmd)
1598 {
1599   snat_main_t *sm = &snat_main;
1600   unformat_input_t _line_input, *line_input = &_line_input;
1601   u8 forwarding_enable;
1602   u8 forwarding_enable_set = 0;
1603   clib_error_t *error = 0;
1604
1605   if (sm->deterministic)
1606     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1607
1608   /* Get a line of input. */
1609   if (!unformat_user (input, unformat_line_input, line_input))
1610     return clib_error_return (0, "'enable' or 'disable' expected");
1611
1612   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1613     {
1614       if (!forwarding_enable_set && unformat (line_input, "enable"))
1615         {
1616           forwarding_enable = 1;
1617           forwarding_enable_set = 1;
1618         }
1619       else if (!forwarding_enable_set && unformat (line_input, "disable"))
1620         {
1621           forwarding_enable = 0;
1622           forwarding_enable_set = 1;
1623         }
1624       else
1625         {
1626           error = clib_error_return (0, "unknown input '%U'",
1627                                      format_unformat_error, line_input);
1628           goto done;
1629         }
1630     }
1631
1632   if (!forwarding_enable_set)
1633     {
1634       error = clib_error_return (0, "'enable' or 'disable' expected");
1635       goto done;
1636     }
1637
1638   sm->forwarding_enabled = forwarding_enable;
1639
1640 done:
1641   unformat_free (line_input);
1642
1643   return error;
1644 }
1645
1646 static clib_error_t *
1647 snat_det_map_command_fn (vlib_main_t * vm,
1648                          unformat_input_t * input, vlib_cli_command_t * cmd)
1649 {
1650   snat_main_t *sm = &snat_main;
1651   unformat_input_t _line_input, *line_input = &_line_input;
1652   ip4_address_t in_addr, out_addr;
1653   u32 in_plen, out_plen;
1654   int is_add = 1, rv;
1655   clib_error_t *error = 0;
1656
1657   if (!sm->deterministic)
1658     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1659
1660   /* Get a line of input. */
1661   if (!unformat_user (input, unformat_line_input, line_input))
1662     return 0;
1663
1664   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1665     {
1666       if (unformat
1667           (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
1668         ;
1669       else
1670         if (unformat
1671             (line_input, "out %U/%u", unformat_ip4_address, &out_addr,
1672              &out_plen))
1673         ;
1674       else if (unformat (line_input, "del"))
1675         is_add = 0;
1676       else
1677         {
1678           error = clib_error_return (0, "unknown input '%U'",
1679                                      format_unformat_error, line_input);
1680           goto done;
1681         }
1682     }
1683
1684   rv = snat_det_add_map (sm, &in_addr, (u8) in_plen, &out_addr, (u8) out_plen,
1685                          is_add);
1686
1687   if (rv)
1688     {
1689       error = clib_error_return (0, "snat_det_add_map return %d", rv);
1690       goto done;
1691     }
1692
1693 done:
1694   unformat_free (line_input);
1695
1696   return error;
1697 }
1698
1699 static clib_error_t *
1700 nat44_det_show_mappings_command_fn (vlib_main_t * vm,
1701                                     unformat_input_t * input,
1702                                     vlib_cli_command_t * cmd)
1703 {
1704   snat_main_t *sm = &snat_main;
1705   snat_det_map_t *dm;
1706
1707   if (!sm->deterministic)
1708     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1709
1710   vlib_cli_output (vm, "NAT44 deterministic mappings:");
1711   /* *INDENT-OFF* */
1712   pool_foreach (dm, sm->det_maps,
1713   ({
1714     vlib_cli_output (vm, " in %U/%d out %U/%d\n",
1715                      format_ip4_address, &dm->in_addr, dm->in_plen,
1716                      format_ip4_address, &dm->out_addr, dm->out_plen);
1717     vlib_cli_output (vm, "  outside address sharing ratio: %d\n",
1718                      dm->sharing_ratio);
1719     vlib_cli_output (vm, "  number of ports per inside host: %d\n",
1720                      dm->ports_per_host);
1721     vlib_cli_output (vm, "  sessions number: %d\n", dm->ses_num);
1722   }));
1723   /* *INDENT-ON* */
1724
1725   return 0;
1726 }
1727
1728 static clib_error_t *
1729 snat_det_forward_command_fn (vlib_main_t * vm,
1730                              unformat_input_t * input,
1731                              vlib_cli_command_t * cmd)
1732 {
1733   snat_main_t *sm = &snat_main;
1734   unformat_input_t _line_input, *line_input = &_line_input;
1735   ip4_address_t in_addr, out_addr;
1736   u16 lo_port;
1737   snat_det_map_t *dm;
1738   clib_error_t *error = 0;
1739
1740   if (!sm->deterministic)
1741     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1742
1743   /* Get a line of input. */
1744   if (!unformat_user (input, unformat_line_input, line_input))
1745     return 0;
1746
1747   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1748     {
1749       if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
1750         ;
1751       else
1752         {
1753           error = clib_error_return (0, "unknown input '%U'",
1754                                      format_unformat_error, line_input);
1755           goto done;
1756         }
1757     }
1758
1759   dm = snat_det_map_by_user (sm, &in_addr);
1760   if (!dm)
1761     vlib_cli_output (vm, "no match");
1762   else
1763     {
1764       snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
1765       vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
1766                        lo_port, lo_port + dm->ports_per_host - 1);
1767     }
1768
1769 done:
1770   unformat_free (line_input);
1771
1772   return error;
1773 }
1774
1775 static clib_error_t *
1776 snat_det_reverse_command_fn (vlib_main_t * vm,
1777                              unformat_input_t * input,
1778                              vlib_cli_command_t * cmd)
1779 {
1780   snat_main_t *sm = &snat_main;
1781   unformat_input_t _line_input, *line_input = &_line_input;
1782   ip4_address_t in_addr, out_addr;
1783   u32 out_port;
1784   snat_det_map_t *dm;
1785   clib_error_t *error = 0;
1786
1787   if (!sm->deterministic)
1788     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1789
1790   /* Get a line of input. */
1791   if (!unformat_user (input, unformat_line_input, line_input))
1792     return 0;
1793
1794   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1795     {
1796       if (unformat
1797           (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
1798         ;
1799       else
1800         {
1801           error = clib_error_return (0, "unknown input '%U'",
1802                                      format_unformat_error, line_input);
1803           goto done;
1804         }
1805     }
1806
1807   if (out_port < 1024 || out_port > 65535)
1808     {
1809       error = clib_error_return (0, "wrong port, must be <1024-65535>");
1810       goto done;
1811     }
1812
1813   dm = snat_det_map_by_out (sm, &out_addr);
1814   if (!dm)
1815     vlib_cli_output (vm, "no match");
1816   else
1817     {
1818       snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
1819       vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
1820     }
1821
1822 done:
1823   unformat_free (line_input);
1824
1825   return error;
1826 }
1827
1828 static clib_error_t *
1829 set_timeout_command_fn (vlib_main_t * vm,
1830                         unformat_input_t * input, vlib_cli_command_t * cmd)
1831 {
1832   snat_main_t *sm = &snat_main;
1833   unformat_input_t _line_input, *line_input = &_line_input;
1834   clib_error_t *error = 0;
1835
1836   /* Get a line of input. */
1837   if (!unformat_user (input, unformat_line_input, line_input))
1838     return 0;
1839
1840   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1841     {
1842       if (unformat (line_input, "udp %u", &sm->udp_timeout))
1843         {
1844           if (nat64_set_udp_timeout (sm->udp_timeout))
1845             {
1846               error = clib_error_return (0, "Invalid UDP timeout value");
1847               goto done;
1848             }
1849         }
1850       else if (unformat (line_input, "tcp-established %u",
1851                          &sm->tcp_established_timeout))
1852         {
1853           if (nat64_set_tcp_timeouts
1854               (sm->tcp_transitory_timeout, sm->tcp_established_timeout))
1855             {
1856               error =
1857                 clib_error_return (0,
1858                                    "Invalid TCP established timeouts value");
1859               goto done;
1860             }
1861         }
1862       else if (unformat (line_input, "tcp-transitory %u",
1863                          &sm->tcp_transitory_timeout))
1864         {
1865           if (nat64_set_tcp_timeouts
1866               (sm->tcp_transitory_timeout, sm->tcp_established_timeout))
1867             {
1868               error =
1869                 clib_error_return (0,
1870                                    "Invalid TCP transitory timeouts value");
1871               goto done;
1872             }
1873         }
1874       else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
1875         {
1876           if (nat64_set_icmp_timeout (sm->icmp_timeout))
1877             {
1878               error = clib_error_return (0, "Invalid ICMP timeout value");
1879               goto done;
1880             }
1881         }
1882       else if (unformat (line_input, "reset"))
1883         {
1884           sm->udp_timeout = SNAT_UDP_TIMEOUT;
1885           sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1886           sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1887           sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1888           nat64_set_udp_timeout (0);
1889           nat64_set_icmp_timeout (0);
1890           nat64_set_tcp_timeouts (0, 0);
1891         }
1892       else
1893         {
1894           error = clib_error_return (0, "unknown input '%U'",
1895                                      format_unformat_error, line_input);
1896           goto done;
1897         }
1898     }
1899
1900 done:
1901   unformat_free (line_input);
1902
1903   return error;
1904 }
1905
1906 static clib_error_t *
1907 nat_show_timeouts_command_fn (vlib_main_t * vm,
1908                               unformat_input_t * input,
1909                               vlib_cli_command_t * cmd)
1910 {
1911   snat_main_t *sm = &snat_main;
1912
1913   vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
1914   vlib_cli_output (vm, "tcp-established timeout: %dsec",
1915                    sm->tcp_established_timeout);
1916   vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
1917                    sm->tcp_transitory_timeout);
1918   vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
1919
1920   return 0;
1921 }
1922
1923 static clib_error_t *
1924 nat44_det_show_sessions_command_fn (vlib_main_t * vm,
1925                                     unformat_input_t * input,
1926                                     vlib_cli_command_t * cmd)
1927 {
1928   snat_main_t *sm = &snat_main;
1929   snat_det_map_t *dm;
1930   snat_det_session_t *ses;
1931   int i;
1932
1933   if (!sm->deterministic)
1934     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1935
1936   vlib_cli_output (vm, "NAT44 deterministic sessions:");
1937   /* *INDENT-OFF* */
1938   pool_foreach (dm, sm->det_maps,
1939   ({
1940     vec_foreach_index (i, dm->sessions)
1941       {
1942         ses = vec_elt_at_index (dm->sessions, i);
1943         if (ses->in_port)
1944           vlib_cli_output (vm, "  %U", format_det_map_ses, dm, ses, &i);
1945       }
1946   }));
1947   /* *INDENT-ON* */
1948   return 0;
1949 }
1950
1951 static clib_error_t *
1952 snat_det_close_session_out_fn (vlib_main_t * vm,
1953                                unformat_input_t * input,
1954                                vlib_cli_command_t * cmd)
1955 {
1956   snat_main_t *sm = &snat_main;
1957   unformat_input_t _line_input, *line_input = &_line_input;
1958   ip4_address_t out_addr, ext_addr, in_addr;
1959   u32 out_port, ext_port;
1960   snat_det_map_t *dm;
1961   snat_det_session_t *ses;
1962   snat_det_out_key_t key;
1963   clib_error_t *error = 0;
1964
1965   if (!sm->deterministic)
1966     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1967
1968   /* Get a line of input. */
1969   if (!unformat_user (input, unformat_line_input, line_input))
1970     return 0;
1971
1972   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1973     {
1974       if (unformat (line_input, "%U:%d %U:%d",
1975                     unformat_ip4_address, &out_addr, &out_port,
1976                     unformat_ip4_address, &ext_addr, &ext_port))
1977         ;
1978       else
1979         {
1980           error = clib_error_return (0, "unknown input '%U'",
1981                                      format_unformat_error, line_input);
1982           goto done;
1983         }
1984     }
1985
1986   unformat_free (line_input);
1987
1988   dm = snat_det_map_by_out (sm, &out_addr);
1989   if (!dm)
1990     vlib_cli_output (vm, "no match");
1991   else
1992     {
1993       snat_det_reverse (dm, &ext_addr, (u16) out_port, &in_addr);
1994       key.ext_host_addr = out_addr;
1995       key.ext_host_port = ntohs ((u16) ext_port);
1996       key.out_port = ntohs ((u16) out_port);
1997       ses = snat_det_get_ses_by_out (dm, &out_addr, key.as_u64);
1998       if (!ses)
1999         vlib_cli_output (vm, "no match");
2000       else
2001         snat_det_ses_close (dm, ses);
2002     }
2003
2004 done:
2005   unformat_free (line_input);
2006
2007   return error;
2008 }
2009
2010 static clib_error_t *
2011 snat_det_close_session_in_fn (vlib_main_t * vm,
2012                               unformat_input_t * input,
2013                               vlib_cli_command_t * cmd)
2014 {
2015   snat_main_t *sm = &snat_main;
2016   unformat_input_t _line_input, *line_input = &_line_input;
2017   ip4_address_t in_addr, ext_addr;
2018   u32 in_port, ext_port;
2019   snat_det_map_t *dm;
2020   snat_det_session_t *ses;
2021   snat_det_out_key_t key;
2022   clib_error_t *error = 0;
2023
2024   if (!sm->deterministic)
2025     return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
2026
2027   /* Get a line of input. */
2028   if (!unformat_user (input, unformat_line_input, line_input))
2029     return 0;
2030
2031   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2032     {
2033       if (unformat (line_input, "%U:%d %U:%d",
2034                     unformat_ip4_address, &in_addr, &in_port,
2035                     unformat_ip4_address, &ext_addr, &ext_port))
2036         ;
2037       else
2038         {
2039           error = clib_error_return (0, "unknown input '%U'",
2040                                      format_unformat_error, line_input);
2041           goto done;
2042         }
2043     }
2044
2045   unformat_free (line_input);
2046
2047   dm = snat_det_map_by_user (sm, &in_addr);
2048   if (!dm)
2049     vlib_cli_output (vm, "no match");
2050   else
2051     {
2052       key.ext_host_addr = ext_addr;
2053       key.ext_host_port = ntohs ((u16) ext_port);
2054       ses =
2055         snat_det_find_ses_by_in (dm, &in_addr, ntohs ((u16) in_port), key);
2056       if (!ses)
2057         vlib_cli_output (vm, "no match");
2058       else
2059         snat_det_ses_close (dm, ses);
2060     }
2061
2062 done:
2063   unformat_free (line_input);
2064
2065   return error;
2066 }
2067 /* *INDENT-OFF* */
2068
2069 /*?
2070  * @cliexpar
2071  * @cliexstart{set snat workers}
2072  * Set NAT workers if 2 or more workers available, use:
2073  *  vpp# set snat workers 0-2,5
2074  * @cliexend
2075 ?*/
2076 VLIB_CLI_COMMAND (set_workers_command, static) = {
2077   .path = "set nat workers",
2078   .function = set_workers_command_fn,
2079   .short_help = "set nat workers <workers-list>",
2080 };
2081
2082 /*?
2083  * @cliexpar
2084  * @cliexstart{show nat workers}
2085  * Show NAT workers.
2086  *  vpp# show nat workers:
2087  *  2 workers
2088  *    vpp_wk_0
2089  *    vpp_wk_1
2090  * @cliexend
2091 ?*/
2092 VLIB_CLI_COMMAND (nat_show_workers_command, static) = {
2093   .path = "show nat workers",
2094   .short_help = "show nat workers",
2095   .function = nat_show_workers_commnad_fn,
2096 };
2097
2098 /*?
2099  * @cliexpar
2100  * @cliexstart{set nat timeout}
2101  * Set values of timeouts for NAT sessions (in seconds), use:
2102  *  vpp# set nat timeout udp 120 tcp-established 7500 tcp-transitory 250 icmp 90
2103  * To reset default values use:
2104  *  vpp# set nat44 deterministic timeout reset
2105  * @cliexend
2106 ?*/
2107 VLIB_CLI_COMMAND (set_timeout_command, static) = {
2108   .path = "set nat timeout",
2109   .function = set_timeout_command_fn,
2110   .short_help =
2111     "set nat timeout [udp <sec> | tcp-established <sec> "
2112     "tcp-transitory <sec> | icmp <sec> | reset]",
2113 };
2114
2115 /*?
2116  * @cliexpar
2117  * @cliexstart{show nat timeouts}
2118  * Show values of timeouts for NAT sessions.
2119  * vpp# show nat timeouts
2120  * udp timeout: 300sec
2121  * tcp-established timeout: 7440sec
2122  * tcp-transitory timeout: 240sec
2123  * icmp timeout: 60sec
2124  * @cliexend
2125 ?*/
2126 VLIB_CLI_COMMAND (nat_show_timeouts_command, static) = {
2127   .path = "show nat timeouts",
2128   .short_help = "show nat timeouts",
2129   .function = nat_show_timeouts_command_fn,
2130 };
2131
2132 /*?
2133  * @cliexpar
2134  * @cliexstart{nat set logging level}
2135  * To force garbage collection of nat sessions
2136  *  vpp# nat44 session cleanup
2137  * @cliexend
2138 ?*/
2139 VLIB_CLI_COMMAND (nat44_session_cleanup_command, static) = {
2140   .path = "nat44 session cleanup",
2141   .function = nat44_session_cleanup_command_fn,
2142   .short_help = "nat44 session cleanup",
2143 };
2144
2145 /*?
2146  * @cliexpar
2147  * @cliexstart{nat set logging level}
2148  * To set NAT logging level use:
2149  * Set nat logging level
2150  * @cliexend
2151 ?*/
2152 VLIB_CLI_COMMAND (snat_set_log_level_command, static) = {
2153   .path = "nat set logging level",
2154   .function = snat_set_log_level_command_fn,
2155   .short_help = "nat set logging level <level>",
2156 };
2157
2158 /*?
2159  * @cliexpar
2160  * @cliexstart{snat ipfix logging}
2161  * To enable NAT IPFIX logging use:
2162  *  vpp# nat ipfix logging
2163  * To set IPFIX exporter use:
2164  *  vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
2165  * @cliexend
2166 ?*/
2167 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
2168   .path = "nat ipfix logging",
2169   .function = snat_ipfix_logging_enable_disable_command_fn,
2170   .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
2171 };
2172
2173 /*?
2174  * @cliexpar
2175  * @cliexstart{nat addr-port-assignment-alg}
2176  * Set address and port assignment algorithm
2177  * For the MAP-E CE limit port choice based on PSID use:
2178  *  vpp# nat addr-port-assignment-alg map-e psid 10 psid-offset 6 psid-len 6
2179  * For port range use:
2180  *  vpp# nat addr-port-assignment-alg port-range <start-port> - <end-port>
2181  * To set standard (default) address and port assignment algorithm use:
2182  *  vpp# nat addr-port-assignment-alg default
2183  * @cliexend
2184 ?*/
2185 VLIB_CLI_COMMAND (nat44_set_alloc_addr_and_port_alg_command, static) = {
2186     .path = "nat addr-port-assignment-alg",
2187     .short_help = "nat addr-port-assignment-alg <alg-name> [<alg-params>]",
2188     .function = nat44_set_alloc_addr_and_port_alg_command_fn,
2189 };
2190
2191 /*?
2192  * @cliexpar
2193  * @cliexstart{show nat addr-port-assignment-alg}
2194  * Show address and port assignment algorithm
2195  * @cliexend
2196 ?*/
2197 VLIB_CLI_COMMAND (nat44_show_alloc_addr_and_port_alg_command, static) = {
2198     .path = "show nat addr-port-assignment-alg",
2199     .short_help = "show nat addr-port-assignment-alg",
2200     .function = nat44_show_alloc_addr_and_port_alg_command_fn,
2201 };
2202
2203 /*?
2204  * @cliexpar
2205  * @cliexstart{nat mss-clamping}
2206  * Set TCP MSS rewriting configuration
2207  * To enable TCP MSS rewriting use:
2208  *  vpp# nat mss-clamping 1452
2209  * To disbale TCP MSS rewriting use:
2210  *  vpp# nat mss-clamping disable
2211  * @cliexend
2212 ?*/
2213 VLIB_CLI_COMMAND (nat_set_mss_clamping_command, static) = {
2214     .path = "nat mss-clamping",
2215     .short_help = "nat mss-clamping <mss-value>|disable",
2216     .function = nat_set_mss_clamping_command_fn,
2217 };
2218
2219 /*?
2220  * @cliexpar
2221  * @cliexstart{show nat mss-clamping}
2222  * Show TCP MSS rewriting configuration
2223  * @cliexend
2224 ?*/
2225 VLIB_CLI_COMMAND (nat_show_mss_clamping_command, static) = {
2226     .path = "show nat mss-clamping",
2227     .short_help = "show nat mss-clamping",
2228     .function = nat_show_mss_clamping_command_fn,
2229 };
2230
2231 /*?
2232  * @cliexpar
2233  * @cliexstart{nat ha failover}
2234  * Set HA failover (remote settings)
2235  * @cliexend
2236 ?*/
2237 VLIB_CLI_COMMAND (nat_ha_failover_command, static) = {
2238     .path = "nat ha failover",
2239     .short_help = "nat ha failover <ip4-address>:<port> [refresh-interval <sec>]",
2240     .function = nat_ha_failover_command_fn,
2241 };
2242
2243 /*?
2244  * @cliexpar
2245  * @cliexstart{nat ha listener}
2246  * Set HA listener (local settings)
2247  * @cliexend
2248 ?*/
2249 VLIB_CLI_COMMAND (nat_ha_listener_command, static) = {
2250     .path = "nat ha listener",
2251     .short_help = "nat ha listener <ip4-address>:<port> [path-mtu <path-mtu>]",
2252     .function = nat_ha_listener_command_fn,
2253 };
2254
2255 /*?
2256  * @cliexpar
2257  * @cliexstart{show nat ha}
2258  * Show HA configuration/status
2259  * @cliexend
2260 ?*/
2261 VLIB_CLI_COMMAND (nat_show_ha_command, static) = {
2262     .path = "show nat ha",
2263     .short_help = "show nat ha",
2264     .function = nat_show_ha_command_fn,
2265 };
2266
2267 /*?
2268  * @cliexpar
2269  * @cliexstart{nat ha flush}
2270  * Flush the current HA data (for testing)
2271  * @cliexend
2272 ?*/
2273 VLIB_CLI_COMMAND (nat_ha_flush_command, static) = {
2274     .path = "nat ha flush",
2275     .short_help = "nat ha flush",
2276     .function = nat_ha_flush_command_fn,
2277 };
2278
2279 /*?
2280  * @cliexpar
2281  * @cliexstart{nat ha resync}
2282  * Resync HA (resend existing sessions to new failover)
2283  * @cliexend
2284 ?*/
2285 VLIB_CLI_COMMAND (nat_ha_resync_command, static) = {
2286     .path = "nat ha resync",
2287     .short_help = "nat ha resync",
2288     .function = nat_ha_resync_command_fn,
2289 };
2290
2291 /*?
2292  * @cliexpar
2293  * @cliexstart{show nat44 hash tables}
2294  * Show NAT44 hash tables
2295  * @cliexend
2296 ?*/
2297 VLIB_CLI_COMMAND (nat44_show_hash, static) = {
2298   .path = "show nat44 hash tables",
2299   .short_help = "show nat44 hash tables [detail|verbose]",
2300   .function = nat44_show_hash_commnad_fn,
2301 };
2302
2303 /*?
2304  * @cliexpar
2305  * @cliexstart{nat44 add address}
2306  * Add/delete NAT44 pool address.
2307  * To add NAT44 pool address use:
2308  *  vpp# nat44 add address 172.16.1.3
2309  *  vpp# nat44 add address 172.16.2.2 - 172.16.2.24
2310  * To add NAT44 pool address for specific tenant (identified by VRF id) use:
2311  *  vpp# nat44 add address 172.16.1.3 tenant-vrf 10
2312  * @cliexend
2313 ?*/
2314 VLIB_CLI_COMMAND (add_address_command, static) = {
2315   .path = "nat44 add address",
2316   .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
2317                 "[tenant-vrf <vrf-id>] [twice-nat] [del]",
2318   .function = add_address_command_fn,
2319 };
2320
2321 /*?
2322  * @cliexpar
2323  * @cliexstart{show nat44 summary}
2324  * Show NAT44 summary
2325  * vpp# show nat44 summary
2326  * @cliexend
2327 ?*/
2328 VLIB_CLI_COMMAND (nat44_show_summary_command, static) = {
2329   .path = "show nat44 summary",
2330   .short_help = "show nat44 summary",
2331   .function = nat44_show_summary_command_fn,
2332 };
2333
2334 /*?
2335  * @cliexpar
2336  * @cliexstart{show nat44 addresses}
2337  * Show NAT44 pool addresses.
2338  * vpp# show nat44 addresses
2339  * NAT44 pool addresses:
2340  * 172.16.2.2
2341  *   tenant VRF independent
2342  *   10 busy udp ports
2343  *   0 busy tcp ports
2344  *   0 busy icmp ports
2345  * 172.16.1.3
2346  *   tenant VRF: 10
2347  *   0 busy udp ports
2348  *   2 busy tcp ports
2349  *   0 busy icmp ports
2350  * NAT44 twice-nat pool addresses:
2351  * 10.20.30.72
2352  *   tenant VRF independent
2353  *   0 busy udp ports
2354  *   0 busy tcp ports
2355  *   0 busy icmp ports
2356  * @cliexend
2357 ?*/
2358 VLIB_CLI_COMMAND (nat44_show_addresses_command, static) = {
2359   .path = "show nat44 addresses",
2360   .short_help = "show nat44 addresses",
2361   .function = nat44_show_addresses_command_fn,
2362 };
2363
2364 /*?
2365  * @cliexpar
2366  * @cliexstart{set interface nat44}
2367  * Enable/disable NAT44 feature on the interface.
2368  * To enable NAT44 feature with local network interface use:
2369  *  vpp# set interface nat44 in GigabitEthernet0/8/0
2370  * To enable NAT44 feature with external network interface use:
2371  *  vpp# set interface nat44 out GigabitEthernet0/a/0
2372  * @cliexend
2373 ?*/
2374 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
2375   .path = "set interface nat44",
2376   .function = snat_feature_command_fn,
2377   .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
2378                 "[del]",
2379 };
2380
2381 /*?
2382  * @cliexpar
2383  * @cliexstart{show nat44 interfaces}
2384  * Show interfaces with NAT44 feature.
2385  * vpp# show nat44 interfaces
2386  * NAT44 interfaces:
2387  *  GigabitEthernet0/8/0 in
2388  *  GigabitEthernet0/a/0 out
2389  * @cliexend
2390 ?*/
2391 VLIB_CLI_COMMAND (nat44_show_interfaces_command, static) = {
2392   .path = "show nat44 interfaces",
2393   .short_help = "show nat44 interfaces",
2394   .function = nat44_show_interfaces_command_fn,
2395 };
2396
2397 /*?
2398  * @cliexpar
2399  * @cliexstart{nat44 add static mapping}
2400  * Static mapping allows hosts on the external network to initiate connection
2401  * to to the local network host.
2402  * To create static mapping between local host address 10.0.0.3 port 6303 and
2403  * external address 4.4.4.4 port 3606 for TCP protocol use:
2404  *  vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
2405  * If not runnig "static mapping only" NAT plugin mode use before:
2406  *  vpp# nat44 add address 4.4.4.4
2407  * To create static mapping between local and external address use:
2408  *  vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
2409  * @cliexend
2410 ?*/
2411 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
2412   .path = "nat44 add static mapping",
2413   .function = add_static_mapping_command_fn,
2414   .short_help =
2415     "nat44 add static mapping tcp|udp|icmp local <addr> [<port>] "
2416     "external <addr> [<port>] [vrf <table-id>] [twice-nat|self-twice-nat] "
2417     "[out2in-only] [del]",
2418 };
2419
2420 /*?
2421  * @cliexpar
2422  * @cliexstart{nat44 add identity mapping}
2423  * Identity mapping translate an IP address to itself.
2424  * To create identity mapping for address 10.0.0.3 port 6303 for TCP protocol
2425  * use:
2426  *  vpp# nat44 add identity mapping 10.0.0.3 tcp 6303
2427  * To create identity mapping for address 10.0.0.3 use:
2428  *  vpp# nat44 add identity mapping 10.0.0.3
2429  * To create identity mapping for DHCP addressed interface use:
2430  *  vpp# nat44 add identity mapping external GigabitEthernet0/a/0 tcp 3606
2431  * @cliexend
2432 ?*/
2433 VLIB_CLI_COMMAND (add_identity_mapping_command, static) = {
2434   .path = "nat44 add identity mapping",
2435   .function = add_identity_mapping_command_fn,
2436   .short_help = "nat44 add identity mapping <ip4-addr>|external <interface> "
2437     "[<protocol> <port>] [vrf <table-id>] [del]",
2438 };
2439
2440 /*?
2441  * @cliexpar
2442  * @cliexstart{nat44 add load-balancing static mapping}
2443  * Service load balancing using NAT44
2444  * To add static mapping with load balancing for service with external IP
2445  * address 1.2.3.4 and TCP port 80 and mapped to 2 local servers
2446  * 10.100.10.10:8080 and 10.100.10.20:8080 with probability 80% resp. 20% use:
2447  *  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
2448  * @cliexend
2449 ?*/
2450 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
2451   .path = "nat44 add load-balancing static mapping",
2452   .function = add_lb_static_mapping_command_fn,
2453   .short_help =
2454     "nat44 add load-balancing static mapping protocol tcp|udp "
2455     "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
2456     "probability <n> [twice-nat|self-twice-nat] [out2in-only] "
2457     "[affinity <timeout-seconds>] [del]",
2458 };
2459
2460 /*?
2461  * @cliexpar
2462  * @cliexstart{nat44 add load-balancing static mapping}
2463  * Modify service load balancing using NAT44
2464  * To add new back-end server 10.100.10.30:8080 for service load balancing
2465  * static mapping with external IP address 1.2.3.4 and TCP port 80 use:
2466  *  vpp# nat44 add load-balancing back-end protocol tcp external 1.2.3.4:80 local 10.100.10.30:8080 probability 25
2467  * @cliexend
2468 ?*/
2469 VLIB_CLI_COMMAND (add_lb_backend_command, static) = {
2470   .path = "nat44 add load-balancing back-end",
2471   .function = add_lb_backend_command_fn,
2472   .short_help =
2473     "nat44 add load-balancing back-end protocol tcp|udp "
2474     "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
2475     "probability <n> [del]",
2476 };
2477
2478 /*?
2479  * @cliexpar
2480  * @cliexstart{show nat44 static mappings}
2481  * Show NAT44 static mappings.
2482  * vpp# show nat44 static mappings
2483  * NAT44 static mappings:
2484  *  local 10.0.0.3 external 4.4.4.4 vrf 0
2485  *  tcp local 192.168.0.4:6303 external 4.4.4.3:3606 vrf 0
2486  *  tcp vrf 0 external 1.2.3.4:80  out2in-only
2487  *   local 10.100.10.10:8080 probability 80
2488  *   local 10.100.10.20:8080 probability 20
2489  *  tcp local 10.100.3.8:8080 external 169.10.10.1:80 vrf 0 twice-nat
2490  *  tcp local 10.0.0.10:3603 external GigabitEthernet0/a/0:6306 vrf 10
2491  * @cliexend
2492 ?*/
2493 VLIB_CLI_COMMAND (nat44_show_static_mappings_command, static) = {
2494   .path = "show nat44 static mappings",
2495   .short_help = "show nat44 static mappings",
2496   .function = nat44_show_static_mappings_command_fn,
2497 };
2498
2499 /*?
2500  * @cliexpar
2501  * @cliexstart{nat44 add interface address}
2502  * Use NAT44 pool address from specific interfce
2503  * To add NAT44 pool address from specific interface use:
2504  *  vpp# nat44 add interface address GigabitEthernet0/8/0
2505  * @cliexend
2506 ?*/
2507 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2508     .path = "nat44 add interface address",
2509     .short_help = "nat44 add interface address <interface> [twice-nat] [del]",
2510     .function = snat_add_interface_address_command_fn,
2511 };
2512
2513 /*?
2514  * @cliexpar
2515  * @cliexstart{show nat44 interface address}
2516  * Show NAT44 pool address interfaces
2517  * vpp# show nat44 interface address
2518  * NAT44 pool address interfaces:
2519  *  GigabitEthernet0/a/0
2520  * NAT44 twice-nat pool address interfaces:
2521  *  GigabitEthernet0/8/0
2522  * @cliexend
2523 ?*/
2524 VLIB_CLI_COMMAND (nat44_show_interface_address_command, static) = {
2525   .path = "show nat44 interface address",
2526   .short_help = "show nat44 interface address",
2527   .function = nat44_show_interface_address_command_fn,
2528 };
2529
2530 /*?
2531  * @cliexpar
2532  * @cliexstart{show nat44 sessions}
2533  * Show NAT44 sessions.
2534  * @cliexend
2535 ?*/
2536 VLIB_CLI_COMMAND (nat44_show_sessions_command, static) = {
2537   .path = "show nat44 sessions",
2538   .short_help = "show nat44 sessions [detail]",
2539   .function = nat44_show_sessions_command_fn,
2540 };
2541
2542 /*?
2543  * @cliexpar
2544  * @cliexstart{nat44 del user}
2545  * To delete all NAT44 user sessions:
2546  *  vpp# nat44 del user 10.0.0.3
2547  * @cliexend
2548 ?*/
2549 VLIB_CLI_COMMAND (nat44_del_user_command, static) = {
2550     .path = "nat44 del user",
2551     .short_help = "nat44 del user <addr> [fib <index>]",
2552     .function = nat44_del_user_command_fn,
2553 };
2554
2555 /*?
2556  * @cliexpar
2557  * @cliexstart{nat44 del session}
2558  * To administratively delete NAT44 session by inside address and port use:
2559  *  vpp# nat44 del session in 10.0.0.3:6303 tcp
2560  * To administratively delete NAT44 session by outside address and port use:
2561  *  vpp# nat44 del session out 1.0.0.3:6033 udp
2562  * @cliexend
2563 ?*/
2564 VLIB_CLI_COMMAND (nat44_del_session_command, static) = {
2565     .path = "nat44 del session",
2566     .short_help = "nat44 del session in|out <addr>:<port> tcp|udp|icmp [vrf <id>] [external-host <addr>:<port>]",
2567     .function = nat44_del_session_command_fn,
2568 };
2569
2570 /*?
2571  * @cliexpar
2572  * @cliexstart{nat44 forwarding}
2573  * Enable or disable forwarding
2574  * Forward packets which don't match existing translation
2575  * or static mapping instead of dropping them.
2576  * To enable forwarding, use:
2577  *  vpp# nat44 forwarding enable
2578  * To disable forwarding, use:
2579  *  vpp# nat44 forwarding disable
2580  * @cliexend
2581 ?*/
2582 VLIB_CLI_COMMAND (snat_forwarding_set_command, static) = {
2583   .path = "nat44 forwarding",
2584   .short_help = "nat44 forwarding enable|disable",
2585   .function = snat_forwarding_set_command_fn,
2586 };
2587
2588 /*?
2589  * @cliexpar
2590  * @cliexstart{nat44 deterministic add}
2591  * Create bijective mapping of inside address to outside address and port range
2592  * pairs, with the purpose of enabling deterministic NAT to reduce logging in
2593  * CGN deployments.
2594  * To create deterministic mapping between inside network 10.0.0.0/18 and
2595  * outside network 1.1.1.0/30 use:
2596  * # vpp# nat44 deterministic add in 10.0.0.0/18 out 1.1.1.0/30
2597  * @cliexend
2598 ?*/
2599 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
2600     .path = "nat44 deterministic add",
2601     .short_help = "nat44 deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
2602     .function = snat_det_map_command_fn,
2603 };
2604
2605 /*?
2606  * @cliexpar
2607  * @cliexpstart{show nat44 deterministic mappings}
2608  * Show NAT44 deterministic mappings
2609  * vpp# show nat44 deterministic mappings
2610  * NAT44 deterministic mappings:
2611  *  in 10.0.0.0/24 out 1.1.1.1/32
2612  *   outside address sharing ratio: 256
2613  *   number of ports per inside host: 252
2614  *   sessions number: 0
2615  * @cliexend
2616 ?*/
2617 VLIB_CLI_COMMAND (nat44_det_show_mappings_command, static) = {
2618     .path = "show nat44 deterministic mappings",
2619     .short_help = "show nat44 deterministic mappings",
2620     .function = nat44_det_show_mappings_command_fn,
2621 };
2622
2623 /*?
2624  * @cliexpar
2625  * @cliexstart{nat44 deterministic forward}
2626  * Return outside address and port range from inside address for deterministic
2627  * NAT.
2628  * To obtain outside address and port of inside host use:
2629  *  vpp# nat44 deterministic forward 10.0.0.2
2630  *  1.1.1.0:<1054-1068>
2631  * @cliexend
2632 ?*/
2633 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
2634     .path = "nat44 deterministic forward",
2635     .short_help = "nat44 deterministic forward <addr>",
2636     .function = snat_det_forward_command_fn,
2637 };
2638
2639 /*?
2640  * @cliexpar
2641  * @cliexstart{nat44 deterministic reverse}
2642  * Return inside address from outside address and port for deterministic NAT.
2643  * To obtain inside host address from outside address and port use:
2644  *  #vpp nat44 deterministic reverse 1.1.1.1:1276
2645  *  10.0.16.16
2646  * @cliexend
2647 ?*/
2648 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
2649     .path = "nat44 deterministic reverse",
2650     .short_help = "nat44 deterministic reverse <addr>:<port>",
2651     .function = snat_det_reverse_command_fn,
2652 };
2653
2654 /*?
2655  * @cliexpar
2656  * @cliexstart{show nat44 deterministic sessions}
2657  * Show NAT44 deterministic sessions.
2658  * vpp# show nat44 deterministic sessions
2659  * NAT44 deterministic sessions:
2660  *   in 10.0.0.3:3005 out 1.1.1.2:1146 external host 172.16.1.2:3006 state: udp-active expire: 306
2661  *   in 10.0.0.3:3000 out 1.1.1.2:1141 external host 172.16.1.2:3001 state: udp-active expire: 306
2662  *   in 10.0.0.4:3005 out 1.1.1.2:1177 external host 172.16.1.2:3006 state: udp-active expire: 306
2663  * @cliexend
2664 ?*/
2665 VLIB_CLI_COMMAND (nat44_det_show_sessions_command, static) = {
2666   .path = "show nat44 deterministic sessions",
2667   .short_help = "show nat44 deterministic sessions",
2668   .function = nat44_det_show_sessions_command_fn,
2669 };
2670
2671 /*?
2672  * @cliexpar
2673  * @cliexstart{nat44 deterministic close session out}
2674  * Close session using outside ip address and port
2675  * and external ip address and port, use:
2676  *  vpp# nat44 deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
2677  * @cliexend
2678 ?*/
2679 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
2680   .path = "nat44 deterministic close session out",
2681   .short_help = "nat44 deterministic close session out "
2682                 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
2683   .function = snat_det_close_session_out_fn,
2684 };
2685
2686 /*?
2687  * @cliexpar
2688  * @cliexstart{nat44 deterministic close session in}
2689  * Close session using inside ip address and port
2690  * and external ip address and port, use:
2691  *  vpp# nat44 deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
2692  * @cliexend
2693 ?*/
2694 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
2695   .path = "nat44 deterministic close session in",
2696   .short_help = "nat44 deterministic close session in "
2697                 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
2698   .function = snat_det_close_session_in_fn,
2699 };
2700
2701 /* *INDENT-ON* */
2702
2703 /*
2704  * fd.io coding-style-patch-verification: ON
2705  *
2706  * Local Variables:
2707  * eval: (c-set-style "gnu")
2708  * End:
2709  */