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