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