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