SNAT: in2out translation as an output feature (VPP-903)
[vpp.git] / src / plugins / snat / snat.c
1 /*
2  * snat.c - simple nat plugin
3  *
4  * Copyright (c) 2016 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/plugin/plugin.h>
22 #include <snat/snat.h>
23 #include <snat/snat_ipfix_logging.h>
24 #include <snat/snat_det.h>
25 #include <snat/nat64.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/fib/ip4_fib.h>
28
29 #include <vpp/app/version.h>
30
31 snat_main_t snat_main;
32
33
34 /* Hook up input features */
35 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
36   .arc_name = "ip4-unicast",
37   .node_name = "snat-in2out",
38   .runs_before = VNET_FEATURES ("snat-out2in"),
39 };
40 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
41   .arc_name = "ip4-unicast",
42   .node_name = "snat-out2in",
43   .runs_before = VNET_FEATURES ("ip4-lookup"),
44 };
45 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
46   .arc_name = "ip4-unicast",
47   .node_name = "snat-det-in2out",
48   .runs_before = VNET_FEATURES ("snat-det-out2in"),
49 };
50 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
51   .arc_name = "ip4-unicast",
52   .node_name = "snat-det-out2in",
53   .runs_before = VNET_FEATURES ("ip4-lookup"),
54 };
55 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
56   .arc_name = "ip4-unicast",
57   .node_name = "snat-in2out-worker-handoff",
58   .runs_before = VNET_FEATURES ("snat-out2in-worker-handoff"),
59 };
60 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
61   .arc_name = "ip4-unicast",
62   .node_name = "snat-out2in-worker-handoff",
63   .runs_before = VNET_FEATURES ("ip4-lookup"),
64 };
65 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
66   .arc_name = "ip4-unicast",
67   .node_name = "snat-in2out-fast",
68   .runs_before = VNET_FEATURES ("snat-out2in-fast"),
69 };
70 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
71   .arc_name = "ip4-unicast",
72   .node_name = "snat-out2in-fast",
73   .runs_before = VNET_FEATURES ("ip4-lookup"),
74 };
75
76 /* Hook up output features */
77 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
78   .arc_name = "ip4-output",
79   .node_name = "snat-in2out-output",
80   .runs_before = VNET_FEATURES ("interface-output"),
81 };
82
83 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
84   .arc_name = "ip4-output",
85   .node_name = "snat-in2out-output-worker-handoff",
86   .runs_before = VNET_FEATURES ("interface-output"),
87 };
88
89
90 /* *INDENT-OFF* */
91 VLIB_PLUGIN_REGISTER () = {
92     .version = VPP_BUILD_VER,
93     .description = "Network Address Translation",
94 };
95 /* *INDENT-ON* */
96
97 /**
98  * @brief Add/del NAT address to FIB.
99  *
100  * Add the external NAT address to the FIB as receive entries. This ensures
101  * that VPP will reply to ARP for this address and we don't need to enable
102  * proxy ARP on the outside interface.
103  *
104  * @param addr IPv4 address.
105  * @param plen address prefix length
106  * @param sw_if_index Interface.
107  * @param is_add If 0 delete, otherwise add.
108  */
109 void
110 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
111                           int is_add)
112 {
113   fib_prefix_t prefix = {
114     .fp_len = p_len,
115     .fp_proto = FIB_PROTOCOL_IP4,
116     .fp_addr = {
117         .ip4.as_u32 = addr->as_u32,
118     },
119   };
120   u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
121
122   if (is_add)
123     fib_table_entry_update_one_path(fib_index,
124                                     &prefix,
125                                     FIB_SOURCE_PLUGIN_HI,
126                                     (FIB_ENTRY_FLAG_CONNECTED |
127                                      FIB_ENTRY_FLAG_LOCAL |
128                                      FIB_ENTRY_FLAG_EXCLUSIVE),
129                                     FIB_PROTOCOL_IP4,
130                                     NULL,
131                                     sw_if_index,
132                                     ~0,
133                                     1,
134                                     NULL,
135                                     FIB_ROUTE_PATH_FLAG_NONE);
136   else
137     fib_table_entry_delete(fib_index,
138                            &prefix,
139                            FIB_SOURCE_PLUGIN_HI);
140 }
141
142 void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id)
143 {
144   snat_address_t * ap;
145   snat_interface_t *i;
146
147   if (vrf_id != ~0)
148     sm->vrf_mode = 1;
149
150   /* Check if address already exists */
151   vec_foreach (ap, sm->addresses)
152     {
153       if (ap->addr.as_u32 == addr->as_u32)
154         return;
155     }
156
157   vec_add2 (sm->addresses, ap, 1);
158   ap->addr = *addr;
159   ap->fib_index = ip4_fib_index_from_table_id(vrf_id);
160 #define _(N, i, n, s) \
161   clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535);
162   foreach_snat_protocol
163 #undef _
164
165   /* Add external address to FIB */
166   pool_foreach (i, sm->interfaces,
167   ({
168     if (i->is_inside)
169       continue;
170
171     snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
172     break;
173   }));
174   pool_foreach (i, sm->output_feature_interfaces,
175   ({
176     if (i->is_inside)
177       continue;
178
179     snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
180     break;
181   }));
182 }
183
184 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
185                                                    ip4_address_t addr)
186 {
187   snat_static_mapping_t *m;
188   pool_foreach (m, sm->static_mappings,
189   ({
190       if (m->external_addr.as_u32 == addr.as_u32)
191         return 1;
192   }));
193
194   return 0;
195 }
196
197 void increment_v4_address (ip4_address_t * a)
198 {
199   u32 v;
200   
201   v = clib_net_to_host_u32(a->as_u32) + 1;
202   a->as_u32 = clib_host_to_net_u32(v);
203 }
204
205 static void 
206 snat_add_static_mapping_when_resolved (snat_main_t * sm, 
207                                        ip4_address_t l_addr, 
208                                        u16 l_port, 
209                                        u32 sw_if_index, 
210                                        u16 e_port, 
211                                        u32 vrf_id,
212                                        snat_protocol_t proto,
213                                        int addr_only,  
214                                        int is_add)
215 {
216   snat_static_map_resolve_t *rp;
217
218   vec_add2 (sm->to_resolve, rp, 1);
219   rp->l_addr.as_u32 = l_addr.as_u32;
220   rp->l_port = l_port;
221   rp->sw_if_index = sw_if_index;
222   rp->e_port = e_port;
223   rp->vrf_id = vrf_id;
224   rp->proto = proto;
225   rp->addr_only = addr_only;
226   rp->is_add = is_add;
227 }
228
229 /**
230  * @brief Add static mapping.
231  *
232  * Create static mapping between local addr+port and external addr+port.
233  *
234  * @param l_addr Local IPv4 address.
235  * @param e_addr External IPv4 address.
236  * @param l_port Local port number.
237  * @param e_port External port number.
238  * @param vrf_id VRF ID.
239  * @param addr_only If 0 address port and pair mapping, otherwise address only.
240  * @param sw_if_index External port instead of specific IP address.
241  * @param is_add If 0 delete static mapping, otherwise add.
242  *
243  * @returns
244  */
245 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
246                             u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
247                             u32 sw_if_index, snat_protocol_t proto, int is_add)
248 {
249   snat_main_t * sm = &snat_main;
250   snat_static_mapping_t *m;
251   snat_session_key_t m_key;
252   clib_bihash_kv_8_8_t kv, value;
253   snat_address_t *a = 0;
254   u32 fib_index = ~0;
255   uword * p;
256   snat_interface_t *interface;
257   int i;
258
259   /* If the external address is a specific interface address */
260   if (sw_if_index != ~0)
261     {
262       ip4_address_t * first_int_addr;
263
264       /* Might be already set... */
265       first_int_addr = ip4_interface_first_address 
266         (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
267
268       /* DHCP resolution required? */
269       if (first_int_addr == 0)
270         {
271           snat_add_static_mapping_when_resolved 
272             (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
273              addr_only,  is_add);
274           return 0;
275         }
276         else
277           e_addr.as_u32 = first_int_addr->as_u32;
278     }
279
280   m_key.addr = e_addr;
281   m_key.port = addr_only ? 0 : e_port;
282   m_key.protocol = addr_only ? 0 : proto;
283   m_key.fib_index = sm->outside_fib_index;
284   kv.key = m_key.as_u64;
285   if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
286     m = 0;
287   else
288     m = pool_elt_at_index (sm->static_mappings, value.value);
289
290   if (is_add)
291     {
292       if (m)
293         return VNET_API_ERROR_VALUE_EXIST;
294
295       /* Convert VRF id to FIB index */
296       if (vrf_id != ~0)
297         {
298           p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
299           if (!p)
300             return VNET_API_ERROR_NO_SUCH_FIB;
301           fib_index = p[0];
302         }
303       /* If not specified use inside VRF id from SNAT plugin startup config */
304       else
305         {
306           fib_index = sm->inside_fib_index;
307           vrf_id = sm->inside_vrf_id;
308         }
309
310       /* Find external address in allocated addresses and reserve port for
311          address and port pair mapping when dynamic translations enabled */
312       if (!addr_only && !(sm->static_mapping_only))
313         {
314           for (i = 0; i < vec_len (sm->addresses); i++)
315             {
316               if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
317                 {
318                   a = sm->addresses + i;
319                   /* External port must be unused */
320                   switch (proto)
321                     {
322 #define _(N, j, n, s) \
323                     case SNAT_PROTOCOL_##N: \
324                       if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
325                         return VNET_API_ERROR_INVALID_VALUE; \
326                       clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
327                       if (e_port > 1024) \
328                         a->busy_##n##_ports++; \
329                       break;
330                       foreach_snat_protocol
331 #undef _
332                     default:
333                       clib_warning("unknown_protocol");
334                       return VNET_API_ERROR_INVALID_VALUE_2;
335                     }
336                   break;
337                 }
338             }
339           /* External address must be allocated */
340           if (!a)
341             return VNET_API_ERROR_NO_SUCH_ENTRY;
342         }
343
344       pool_get (sm->static_mappings, m);
345       memset (m, 0, sizeof (*m));
346       m->local_addr = l_addr;
347       m->external_addr = e_addr;
348       m->addr_only = addr_only;
349       m->vrf_id = vrf_id;
350       m->fib_index = fib_index;
351       if (!addr_only)
352         {
353           m->local_port = l_port;
354           m->external_port = e_port;
355           m->proto = proto;
356         }
357
358       m_key.addr = m->local_addr;
359       m_key.port = m->local_port;
360       m_key.protocol = m->proto;
361       m_key.fib_index = m->fib_index;
362       kv.key = m_key.as_u64;
363       kv.value = m - sm->static_mappings;
364       clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
365
366       m_key.addr = m->external_addr;
367       m_key.port = m->external_port;
368       m_key.fib_index = sm->outside_fib_index;
369       kv.key = m_key.as_u64;
370       kv.value = m - sm->static_mappings;
371       clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
372
373       /* Assign worker */
374       if (sm->workers)
375         {
376           snat_user_key_t w_key0;
377           snat_worker_key_t w_key1;
378
379           w_key0.addr = m->local_addr;
380           w_key0.fib_index = m->fib_index;
381           kv.key = w_key0.as_u64;
382
383           if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
384             {
385               kv.value = sm->first_worker_index +
386                 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
387
388               clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
389             }
390           else
391             {
392               kv.value = value.value;
393             }
394
395           w_key1.addr = m->external_addr;
396           w_key1.port = clib_host_to_net_u16 (m->external_port);
397           w_key1.fib_index = sm->outside_fib_index;
398           kv.key = w_key1.as_u64;
399           clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
400         }
401     }
402   else
403     {
404       if (!m)
405         return VNET_API_ERROR_NO_SUCH_ENTRY;
406
407       /* Free external address port */
408       if (!addr_only && !(sm->static_mapping_only))
409         {
410           for (i = 0; i < vec_len (sm->addresses); i++)
411             {
412               if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
413                 {
414                   a = sm->addresses + i;
415                   switch (proto)
416                     {
417 #define _(N, j, n, s) \
418                     case SNAT_PROTOCOL_##N: \
419                       clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
420                       if (e_port > 1024) \
421                         a->busy_##n##_ports--; \
422                       break;
423                       foreach_snat_protocol
424 #undef _
425                     default:
426                       clib_warning("unknown_protocol");
427                       return VNET_API_ERROR_INVALID_VALUE_2;
428                     }
429                   break;
430                 }
431             }
432         }
433
434       m_key.addr = m->local_addr;
435       m_key.port = m->local_port;
436       m_key.protocol = m->proto;
437       m_key.fib_index = m->fib_index;
438       kv.key = m_key.as_u64;
439       clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
440
441       m_key.addr = m->external_addr;
442       m_key.port = m->external_port;
443       m_key.fib_index = sm->outside_fib_index;
444       kv.key = m_key.as_u64;
445       clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
446
447       /* Delete session(s) for static mapping if exist */
448       if (!(sm->static_mapping_only) ||
449           (sm->static_mapping_only && sm->static_mapping_connection_tracking))
450         {
451           snat_user_key_t u_key;
452           snat_user_t *u;
453           dlist_elt_t * head, * elt;
454           u32 elt_index, head_index, del_elt_index;
455           u32 ses_index;
456           u64 user_index;
457           snat_session_t * s;
458           snat_main_per_thread_data_t *tsm;
459
460           u_key.addr = m->local_addr;
461           u_key.fib_index = m->fib_index;
462           kv.key = u_key.as_u64;
463           if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
464             {
465               user_index = value.value;
466               if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
467                 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
468               else
469                 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
470               u = pool_elt_at_index (tsm->users, user_index);
471               if (u->nstaticsessions)
472                 {
473                   head_index = u->sessions_per_user_list_head_index;
474                   head = pool_elt_at_index (tsm->list_pool, head_index);
475                   elt_index = head->next;
476                   elt = pool_elt_at_index (tsm->list_pool, elt_index);
477                   ses_index = elt->value;
478                   while (ses_index != ~0)
479                     {
480                       s =  pool_elt_at_index (tsm->sessions, ses_index);
481                       del_elt_index = elt_index;
482                       elt_index = elt->next;
483                       elt = pool_elt_at_index (tsm->list_pool, elt_index);
484                       ses_index = elt->value;
485
486                       if (!addr_only)
487                         {
488                           if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
489                               (clib_net_to_host_u16 (s->out2in.port) != e_port))
490                             continue;
491                         }
492
493                       if (snat_is_unk_proto_session (s))
494                         {
495                           clib_bihash_kv_16_8_t up_kv;
496                           snat_unk_proto_ses_key_t up_key;
497                           up_key.l_addr = s->in2out.addr;
498                           up_key.r_addr = s->ext_host_addr;
499                           up_key.fib_index = s->in2out.fib_index;
500                           up_key.proto = s->in2out.port;
501                           up_key.rsvd[0] = up_key.rsvd[1] = up_key.rsvd[2] = 0;
502                           up_kv.key[0] = up_key.as_u64[0];
503                           up_kv.key[1] = up_key.as_u64[1];
504                           if (clib_bihash_add_del_16_8 (&sm->in2out_unk_proto,
505                                                         &up_kv, 0))
506                             clib_warning ("in2out key del failed");
507
508                           up_key.l_addr = s->out2in.addr;
509                           up_key.fib_index = s->out2in.fib_index;
510                           up_kv.key[0] = up_key.as_u64[0];
511                           up_kv.key[1] = up_key.as_u64[1];
512                           if (clib_bihash_add_del_16_8 (&sm->out2in_unk_proto,
513                                                         &up_kv, 0))
514                             clib_warning ("out2in key del failed");
515
516                           goto delete;
517                         }
518                       /* log NAT event */
519                       snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
520                                                           s->out2in.addr.as_u32,
521                                                           s->in2out.protocol,
522                                                           s->in2out.port,
523                                                           s->out2in.port,
524                                                           s->in2out.fib_index);
525
526                       value.key = s->in2out.as_u64;
527                       if (clib_bihash_add_del_8_8 (&sm->in2out, &value, 0))
528                         clib_warning ("in2out key del failed");
529                       value.key = s->out2in.as_u64;
530                       if (clib_bihash_add_del_8_8 (&sm->out2in, &value, 0))
531                         clib_warning ("out2in key del failed");
532 delete:
533                       pool_put (tsm->sessions, s);
534
535                       clib_dlist_remove (tsm->list_pool, del_elt_index);
536                       pool_put_index (tsm->list_pool, del_elt_index);
537                       u->nstaticsessions--;
538
539                       if (!addr_only)
540                         break;
541                     }
542                   if (addr_only)
543                     {
544                       pool_put (tsm->users, u);
545                       clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
546                     }
547                 }
548             }
549         }
550
551       /* Delete static mapping from pool */
552       pool_put (sm->static_mappings, m);
553     }
554
555   if (!addr_only)
556     return 0;
557
558   /* Add/delete external address to FIB */
559   pool_foreach (interface, sm->interfaces,
560   ({
561     if (interface->is_inside)
562       continue;
563
564     snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
565     break;
566   }));
567   pool_foreach (interface, sm->output_feature_interfaces,
568   ({
569     if (interface->is_inside)
570       continue;
571
572     snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
573     break;
574   }));
575
576   return 0;
577 }
578
579 int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
580 {
581   snat_address_t *a = 0;
582   snat_session_t *ses;
583   u32 *ses_to_be_removed = 0, *ses_index;
584   clib_bihash_kv_8_8_t kv, value;
585   snat_user_key_t user_key;
586   snat_user_t *u;
587   snat_main_per_thread_data_t *tsm;
588   snat_static_mapping_t *m;
589   snat_interface_t *interface;
590   int i;
591
592   /* Find SNAT address */
593   for (i=0; i < vec_len (sm->addresses); i++)
594     {
595       if (sm->addresses[i].addr.as_u32 == addr.as_u32)
596         {
597           a = sm->addresses + i;
598           break;
599         }
600     }
601   if (!a)
602     return VNET_API_ERROR_NO_SUCH_ENTRY;
603
604   if (delete_sm)
605     {
606       pool_foreach (m, sm->static_mappings,
607       ({
608           if (m->external_addr.as_u32 == addr.as_u32)
609             (void) snat_add_static_mapping (m->local_addr, m->external_addr,
610                                             m->local_port, m->external_port,
611                                             m->vrf_id, m->addr_only, ~0,
612                                             m->proto, 0);
613       }));
614     }
615   else
616     {
617       /* Check if address is used in some static mapping */
618       if (is_snat_address_used_in_static_mapping(sm, addr))
619         {
620           clib_warning ("address used in static mapping");
621           return VNET_API_ERROR_UNSPECIFIED;
622         }
623     }
624
625   /* Delete sessions using address */
626   if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
627     {
628       vec_foreach (tsm, sm->per_thread_data)
629         {
630           pool_foreach (ses, tsm->sessions, ({
631             if (ses->out2in.addr.as_u32 == addr.as_u32)
632               {
633                 if (snat_is_unk_proto_session (ses))
634                   {
635                     clib_bihash_kv_16_8_t up_kv;
636                     snat_unk_proto_ses_key_t up_key;
637                     up_key.l_addr = ses->in2out.addr;
638                     up_key.r_addr = ses->ext_host_addr;
639                     up_key.fib_index = ses->in2out.fib_index;
640                     up_key.proto = ses->in2out.port;
641                     up_key.rsvd[0] = up_key.rsvd[1] = up_key.rsvd[2] = 0;
642                     up_kv.key[0] = up_key.as_u64[0];
643                     up_kv.key[1] = up_key.as_u64[1];
644                     if (clib_bihash_add_del_16_8 (&sm->in2out_unk_proto,
645                                                   &up_kv, 0))
646                       clib_warning ("in2out key del failed");
647
648                     up_key.l_addr = ses->out2in.addr;
649                     up_key.fib_index = ses->out2in.fib_index;
650                     up_kv.key[0] = up_key.as_u64[0];
651                     up_kv.key[1] = up_key.as_u64[1];
652                     if (clib_bihash_add_del_16_8 (&sm->out2in_unk_proto,
653                                                   &up_kv, 0))
654                       clib_warning ("out2in key del failed");
655                   }
656                 else
657                   {
658                     /* log NAT event */
659                     snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
660                                                         ses->out2in.addr.as_u32,
661                                                         ses->in2out.protocol,
662                                                         ses->in2out.port,
663                                                         ses->out2in.port,
664                                                         ses->in2out.fib_index);
665                     kv.key = ses->in2out.as_u64;
666                     clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
667                     kv.key = ses->out2in.as_u64;
668                     clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
669                   }
670                 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
671                 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
672                 user_key.addr = ses->in2out.addr;
673                 user_key.fib_index = ses->in2out.fib_index;
674                 kv.key = user_key.as_u64;
675                 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
676                   {
677                     u = pool_elt_at_index (tsm->users, value.value);
678                     u->nsessions--;
679                   }
680               }
681           }));
682
683           vec_foreach (ses_index, ses_to_be_removed)
684             pool_put_index (tsm->sessions, ses_index[0]);
685
686           vec_free (ses_to_be_removed);
687        }
688     }
689
690   vec_del1 (sm->addresses, i);
691
692   /* Delete external address from FIB */
693   pool_foreach (interface, sm->interfaces,
694   ({
695     if (interface->is_inside)
696       continue;
697
698     snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
699     break;
700   }));
701   pool_foreach (interface, sm->output_feature_interfaces,
702   ({
703     if (interface->is_inside)
704       continue;
705
706     snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
707     break;
708   }));
709
710   return 0;
711 }
712
713 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
714 {
715   snat_main_t *sm = &snat_main;
716   snat_interface_t *i;
717   const char * feature_name;
718   snat_address_t * ap;
719   snat_static_mapping_t * m;
720   snat_det_map_t * dm;
721
722   if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
723     feature_name = is_inside ?  "snat-in2out-fast" : "snat-out2in-fast";
724   else
725     {
726       if (sm->num_workers > 1 && !sm->deterministic)
727         feature_name = is_inside ?  "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
728       else if (sm->deterministic)
729         feature_name = is_inside ?  "snat-det-in2out" : "snat-det-out2in";
730       else
731         feature_name = is_inside ?  "snat-in2out" : "snat-out2in";
732     }
733
734   vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
735                                !is_del, 0, 0);
736
737   if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
738     sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
739
740   if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
741     sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
742
743   pool_foreach (i, sm->interfaces,
744   ({
745     if (i->sw_if_index == sw_if_index)
746       {
747         if (is_del)
748           pool_put (sm->interfaces, i);
749         else
750           return VNET_API_ERROR_VALUE_EXIST;
751
752         goto fib;
753       }
754   }));
755
756   if (is_del)
757     return VNET_API_ERROR_NO_SUCH_ENTRY;
758
759   pool_get (sm->interfaces, i);
760   i->sw_if_index = sw_if_index;
761   i->is_inside = is_inside;
762
763   /* Add/delete external addresses to FIB */
764 fib:
765   if (is_inside)
766     return 0;
767
768   vec_foreach (ap, sm->addresses)
769     snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
770
771   pool_foreach (m, sm->static_mappings,
772   ({
773     if (!(m->addr_only))
774       continue;
775
776     snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
777   }));
778
779   pool_foreach (dm, sm->det_maps,
780   ({
781     snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
782   }));
783
784   return 0;
785 }
786
787 int snat_interface_add_del_output_feature (u32 sw_if_index,
788                                            u8 is_inside,
789                                            int is_del)
790 {
791   snat_main_t *sm = &snat_main;
792   snat_interface_t *i;
793   snat_address_t * ap;
794   snat_static_mapping_t * m;
795
796   if (sm->deterministic ||
797       (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
798     return VNET_API_ERROR_UNSUPPORTED;
799
800   if (is_inside)
801     goto find;
802
803   if (sm->num_workers > 1)
804     {
805       vnet_feature_enable_disable ("ip4-unicast", "snat-out2in-worker-handoff",
806                                    sw_if_index, !is_del, 0, 0);
807       vnet_feature_enable_disable ("ip4-output",
808                                    "snat-in2out-output-worker-handoff",
809                                    sw_if_index, !is_del, 0, 0);
810     }
811   else
812     {
813       vnet_feature_enable_disable ("ip4-unicast", "snat-out2in", sw_if_index,
814                                    !is_del, 0, 0);
815       vnet_feature_enable_disable ("ip4-output", "snat-in2out-output",
816                                    sw_if_index, !is_del, 0, 0);
817     }
818
819   if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
820     sm->fq_in2out_output_index =
821       vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
822
823   if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
824     sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
825
826 find:
827   pool_foreach (i, sm->output_feature_interfaces,
828   ({
829     if (i->sw_if_index == sw_if_index)
830       {
831         if (is_del)
832           pool_put (sm->output_feature_interfaces, i);
833         else
834           return VNET_API_ERROR_VALUE_EXIST;
835
836         goto fib;
837       }
838   }));
839
840   if (is_del)
841     return VNET_API_ERROR_NO_SUCH_ENTRY;
842
843   pool_get (sm->output_feature_interfaces, i);
844   i->sw_if_index = sw_if_index;
845   i->is_inside = is_inside;
846
847   /* Add/delete external addresses to FIB */
848 fib:
849   if (is_inside)
850     return 0;
851
852   vec_foreach (ap, sm->addresses)
853     snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
854
855   pool_foreach (m, sm->static_mappings,
856   ({
857     if (!(m->addr_only))
858       continue;
859
860     snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
861   }));
862
863   return 0;
864 }
865
866 int snat_set_workers (uword * bitmap)
867 {
868   snat_main_t *sm = &snat_main;
869   int i;
870
871   if (sm->num_workers < 2)
872     return VNET_API_ERROR_FEATURE_DISABLED;
873
874   if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
875     return VNET_API_ERROR_INVALID_WORKER;
876
877   vec_free (sm->workers);
878   clib_bitmap_foreach (i, bitmap,
879     ({
880       vec_add1(sm->workers, i);
881     }));
882
883   return 0;
884 }
885
886
887 static void
888 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
889                                        uword opaque,
890                                        u32 sw_if_index,
891                                        ip4_address_t * address,
892                                        u32 address_length,
893                                        u32 if_address_index,
894                                        u32 is_delete);
895
896 static clib_error_t * snat_init (vlib_main_t * vm)
897 {
898   snat_main_t * sm = &snat_main;
899   clib_error_t * error = 0;
900   ip4_main_t * im = &ip4_main;
901   ip_lookup_main_t * lm = &im->lookup_main;
902   uword *p;
903   vlib_thread_registration_t *tr;
904   vlib_thread_main_t *tm = vlib_get_thread_main ();
905   uword *bitmap = 0;
906   u32 i;
907   ip4_add_del_interface_address_callback_t cb4;
908
909   sm->vlib_main = vm;
910   sm->vnet_main = vnet_get_main();
911   sm->ip4_main = im;
912   sm->ip4_lookup_main = lm;
913   sm->api_main = &api_main;
914   sm->first_worker_index = 0;
915   sm->next_worker = 0;
916   sm->num_workers = 0;
917   sm->workers = 0;
918   sm->fq_in2out_index = ~0;
919   sm->fq_out2in_index = ~0;
920   sm->udp_timeout = SNAT_UDP_TIMEOUT;
921   sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
922   sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
923   sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
924
925   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
926   if (p)
927     {
928       tr = (vlib_thread_registration_t *) p[0];
929       if (tr)
930         {
931           sm->num_workers = tr->count;
932           sm->first_worker_index = tr->first_index;
933         }
934     }
935
936   /* Use all available workers by default */
937   if (sm->num_workers > 1)
938     {
939       for (i=0; i < sm->num_workers; i++)
940         bitmap = clib_bitmap_set (bitmap, i, 1);
941       snat_set_workers(bitmap);
942       clib_bitmap_free (bitmap);
943     }
944
945   error = snat_api_init(vm, sm);
946   if (error)
947     return error;
948
949   /* Set up the interface address add/del callback */
950   cb4.function = snat_ip4_add_del_interface_address_cb;
951   cb4.function_opaque = 0;
952
953   vec_add1 (im->add_del_interface_address_callbacks, cb4);
954
955   /* Init IPFIX logging */
956   snat_ipfix_logging_init(vm);
957
958   error = nat64_init(vm);
959
960   return error;
961 }
962
963 VLIB_INIT_FUNCTION (snat_init);
964
965 void snat_free_outside_address_and_port (snat_main_t * sm, 
966                                          snat_session_key_t * k, 
967                                          u32 address_index)
968 {
969   snat_address_t *a;
970   u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
971   
972   ASSERT (address_index < vec_len (sm->addresses));
973
974   a = sm->addresses + address_index;
975
976   switch (k->protocol)
977     {
978 #define _(N, i, n, s) \
979     case SNAT_PROTOCOL_##N: \
980       ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
981         port_host_byte_order) == 1); \
982       clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
983         port_host_byte_order, 0); \
984       a->busy_##n##_ports--; \
985       break;
986       foreach_snat_protocol
987 #undef _
988     default:
989       clib_warning("unknown_protocol");
990       return;
991     }
992 }  
993
994 /**
995  * @brief Match SNAT static mapping.
996  *
997  * @param sm          SNAT main.
998  * @param match       Address and port to match.
999  * @param mapping     External or local address and port of the matched mapping.
1000  * @param by_external If 0 match by local address otherwise match by external
1001  *                    address.
1002  * @param is_addr_only If matched mapping is address only
1003  *
1004  * @returns 0 if match found otherwise 1.
1005  */
1006 int snat_static_mapping_match (snat_main_t * sm,
1007                                snat_session_key_t match,
1008                                snat_session_key_t * mapping,
1009                                u8 by_external,
1010                                u8 *is_addr_only)
1011 {
1012   clib_bihash_kv_8_8_t kv, value;
1013   snat_static_mapping_t *m;
1014   snat_session_key_t m_key;
1015   clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1016
1017   if (by_external)
1018     mapping_hash = &sm->static_mapping_by_external;
1019
1020   m_key.addr = match.addr;
1021   m_key.port = clib_net_to_host_u16 (match.port);
1022   m_key.protocol = match.protocol;
1023   m_key.fib_index = match.fib_index;
1024
1025   kv.key = m_key.as_u64;
1026
1027   if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1028     {
1029       /* Try address only mapping */
1030       m_key.port = 0;
1031       m_key.protocol = 0;
1032       kv.key = m_key.as_u64;
1033       if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1034         return 1;
1035     }
1036
1037   m = pool_elt_at_index (sm->static_mappings, value.value);
1038
1039   if (by_external)
1040     {
1041       mapping->addr = m->local_addr;
1042       /* Address only mapping doesn't change port */
1043       mapping->port = m->addr_only ? match.port
1044         : clib_host_to_net_u16 (m->local_port);
1045       mapping->fib_index = m->fib_index;
1046     }
1047   else
1048     {
1049       mapping->addr = m->external_addr;
1050       /* Address only mapping doesn't change port */
1051       mapping->port = m->addr_only ? match.port
1052         : clib_host_to_net_u16 (m->external_port);
1053       mapping->fib_index = sm->outside_fib_index;
1054     }
1055
1056   if (PREDICT_FALSE(is_addr_only != 0))
1057     *is_addr_only = m->addr_only;
1058
1059   return 0;
1060 }
1061
1062 int snat_alloc_outside_address_and_port (snat_main_t * sm, 
1063                                          u32 fib_index,
1064                                          snat_session_key_t * k,
1065                                          u32 * address_indexp)
1066 {
1067   int i;
1068   snat_address_t *a;
1069   u32 portnum;
1070
1071   for (i = 0; i < vec_len (sm->addresses); i++)
1072     {
1073       a = sm->addresses + i;
1074       if (sm->vrf_mode && a->fib_index != ~0 && a->fib_index != fib_index)
1075         continue;
1076       switch (k->protocol)
1077         {
1078 #define _(N, j, n, s) \
1079         case SNAT_PROTOCOL_##N: \
1080           if (a->busy_##n##_ports < (65535-1024)) \
1081             { \
1082               while (1) \
1083                 { \
1084                   portnum = random_u32 (&sm->random_seed); \
1085                   portnum &= 0xFFFF; \
1086                   if (portnum < 1024) \
1087                     continue; \
1088                   if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1089                     continue; \
1090                   clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1091                   a->busy_##n##_ports++; \
1092                   k->addr = a->addr; \
1093                   k->port = clib_host_to_net_u16(portnum); \
1094                   *address_indexp = i; \
1095                   return 0; \
1096                 } \
1097             } \
1098           break;
1099           foreach_snat_protocol
1100 #undef _
1101         default:
1102           clib_warning("unknown protocol");
1103           return 1;
1104         }
1105
1106     }
1107   /* Totally out of translations to use... */
1108   snat_ipfix_logging_addresses_exhausted(0);
1109   return 1;
1110 }
1111
1112
1113 static clib_error_t *
1114 add_address_command_fn (vlib_main_t * vm,
1115                         unformat_input_t * input,
1116                         vlib_cli_command_t * cmd)
1117 {
1118   unformat_input_t _line_input, *line_input = &_line_input;
1119   snat_main_t * sm = &snat_main;
1120   ip4_address_t start_addr, end_addr, this_addr;
1121   u32 start_host_order, end_host_order;
1122   u32 vrf_id = ~0;
1123   int i, count;
1124   int is_add = 1;
1125   int rv = 0;
1126   clib_error_t *error = 0;
1127
1128   /* Get a line of input. */
1129   if (!unformat_user (input, unformat_line_input, line_input))
1130     return 0;
1131
1132   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1133     {
1134       if (unformat (line_input, "%U - %U",
1135                     unformat_ip4_address, &start_addr,
1136                     unformat_ip4_address, &end_addr))
1137         ;
1138       else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
1139         ;
1140       else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1141         end_addr = start_addr;
1142       else if (unformat (line_input, "del"))
1143         is_add = 0;
1144       else
1145         {
1146           error = clib_error_return (0, "unknown input '%U'",
1147             format_unformat_error, line_input);
1148           goto done;
1149         }
1150      }
1151
1152   if (sm->static_mapping_only)
1153     {
1154       error = clib_error_return (0, "static mapping only mode");
1155       goto done;
1156     }
1157
1158   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1159   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1160   
1161   if (end_host_order < start_host_order)
1162     {
1163       error = clib_error_return (0, "end address less than start address");
1164       goto done;
1165     }
1166
1167   count = (end_host_order - start_host_order) + 1;
1168
1169   if (count > 1024)
1170     clib_warning ("%U - %U, %d addresses...",
1171                   format_ip4_address, &start_addr,
1172                   format_ip4_address, &end_addr,
1173                   count);
1174   
1175   this_addr = start_addr;
1176
1177   for (i = 0; i < count; i++)
1178     {
1179       if (is_add)
1180         snat_add_address (sm, &this_addr, vrf_id);
1181       else
1182         rv = snat_del_address (sm, this_addr, 0);
1183
1184       switch (rv)
1185         {
1186         case VNET_API_ERROR_NO_SUCH_ENTRY:
1187           error = clib_error_return (0, "S-NAT address not exist.");
1188           goto done;
1189         case VNET_API_ERROR_UNSPECIFIED:
1190           error = clib_error_return (0, "S-NAT address used in static mapping.");
1191           goto done;
1192         default:
1193           break;
1194         }
1195
1196       increment_v4_address (&this_addr);
1197     }
1198
1199 done:
1200   unformat_free (line_input);
1201
1202   return error;
1203 }
1204
1205 VLIB_CLI_COMMAND (add_address_command, static) = {
1206   .path = "snat add address",
1207   .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] "
1208                 "[tenant-vrf <vrf-id>] [del]",
1209   .function = add_address_command_fn,
1210 };
1211
1212 static clib_error_t *
1213 snat_feature_command_fn (vlib_main_t * vm,
1214                           unformat_input_t * input,
1215                           vlib_cli_command_t * cmd)
1216 {
1217   unformat_input_t _line_input, *line_input = &_line_input;
1218   vnet_main_t * vnm = vnet_get_main();
1219   clib_error_t * error = 0;
1220   u32 sw_if_index;
1221   u32 * inside_sw_if_indices = 0;
1222   u32 * outside_sw_if_indices = 0;
1223   u8 is_output_feature = 0;
1224   int is_del = 0;
1225   int i;
1226
1227   sw_if_index = ~0;
1228
1229   /* Get a line of input. */
1230   if (!unformat_user (input, unformat_line_input, line_input))
1231     return 0;
1232
1233   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1234     {
1235       if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1236                     vnm, &sw_if_index))
1237         vec_add1 (inside_sw_if_indices, sw_if_index);
1238       else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1239                          vnm, &sw_if_index))
1240         vec_add1 (outside_sw_if_indices, sw_if_index);
1241       else if (unformat (line_input, "output-feature"))
1242         is_output_feature = 1;
1243       else if (unformat (line_input, "del"))
1244         is_del = 1;
1245       else
1246         {
1247           error = clib_error_return (0, "unknown input '%U'",
1248             format_unformat_error, line_input);
1249           goto done;
1250         }
1251     }
1252
1253   if (vec_len (inside_sw_if_indices))
1254     {
1255       for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1256         {
1257           sw_if_index = inside_sw_if_indices[i];
1258           if (is_output_feature)
1259             {
1260               if (snat_interface_add_del_output_feature (sw_if_index, 1, is_del))
1261                 {
1262                   error = clib_error_return (0, "%s %U failed",
1263                                              is_del ? "del" : "add",
1264                                              format_vnet_sw_interface_name, vnm,
1265                                              vnet_get_sw_interface (vnm,
1266                                                                     sw_if_index));
1267                   goto done;
1268                 }
1269             }
1270           else
1271             {
1272               if (snat_interface_add_del (sw_if_index, 1, is_del))
1273                 {
1274                   error = clib_error_return (0, "%s %U failed",
1275                                              is_del ? "del" : "add",
1276                                              format_vnet_sw_interface_name, vnm,
1277                                              vnet_get_sw_interface (vnm,
1278                                                                     sw_if_index));
1279                   goto done;
1280                 }
1281             }
1282         }
1283     }
1284
1285   if (vec_len (outside_sw_if_indices))
1286     {
1287       for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1288         {
1289           sw_if_index = outside_sw_if_indices[i];
1290           if (is_output_feature)
1291             {
1292               if (snat_interface_add_del_output_feature (sw_if_index, 0, is_del))
1293                 {
1294                   error = clib_error_return (0, "%s %U failed",
1295                                              is_del ? "del" : "add",
1296                                              format_vnet_sw_interface_name, vnm,
1297                                              vnet_get_sw_interface (vnm,
1298                                                                     sw_if_index));
1299                   goto done;
1300                 }
1301             }
1302           else
1303             {
1304               if (snat_interface_add_del (sw_if_index, 0, is_del))
1305                 {
1306                   error = clib_error_return (0, "%s %U failed",
1307                                              is_del ? "del" : "add",
1308                                              format_vnet_sw_interface_name, vnm,
1309                                              vnet_get_sw_interface (vnm,
1310                                                                     sw_if_index));
1311                   goto done;
1312                 }
1313             }
1314         }
1315     }
1316
1317 done:
1318   unformat_free (line_input);
1319   vec_free (inside_sw_if_indices);
1320   vec_free (outside_sw_if_indices);
1321
1322   return error;
1323 }
1324
1325 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1326   .path = "set interface snat",
1327   .function = snat_feature_command_fn,
1328   .short_help = "set interface snat in <intfc> out <intfc> [output-feature] "
1329                 "[del]",
1330 };
1331
1332 uword
1333 unformat_snat_protocol (unformat_input_t * input, va_list * args)
1334 {
1335   u32 *r = va_arg (*args, u32 *);
1336
1337   if (0);
1338 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
1339   foreach_snat_protocol
1340 #undef _
1341   else
1342     return 0;
1343   return 1;
1344 }
1345
1346 u8 *
1347 format_snat_protocol (u8 * s, va_list * args)
1348 {
1349   u32 i = va_arg (*args, u32);
1350   u8 *t = 0;
1351
1352   switch (i)
1353     {
1354 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
1355       foreach_snat_protocol
1356 #undef _
1357     default:
1358       s = format (s, "unknown");
1359     }
1360   s = format (s, "%s", t);
1361   return s;
1362 }
1363
1364 static clib_error_t *
1365 add_static_mapping_command_fn (vlib_main_t * vm,
1366                                unformat_input_t * input,
1367                                vlib_cli_command_t * cmd)
1368 {
1369   unformat_input_t _line_input, *line_input = &_line_input;
1370   clib_error_t * error = 0;
1371   ip4_address_t l_addr, e_addr;
1372   u32 l_port = 0, e_port = 0, vrf_id = ~0;
1373   int is_add = 1;
1374   int addr_only = 1;
1375   u32 sw_if_index = ~0;
1376   vnet_main_t * vnm = vnet_get_main();
1377   int rv;
1378   snat_protocol_t proto;
1379   u8 proto_set = 0;
1380
1381   /* Get a line of input. */
1382   if (!unformat_user (input, unformat_line_input, line_input))
1383     return 0;
1384
1385   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1386     {
1387       if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1388                     &l_port))
1389         addr_only = 0;
1390       else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1391         ;
1392       else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1393                          &e_addr, &e_port))
1394         addr_only = 0;
1395       else if (unformat (line_input, "external %U", unformat_ip4_address,
1396                          &e_addr))
1397         ;
1398       else if (unformat (line_input, "external %U %u",
1399                          unformat_vnet_sw_interface, vnm, &sw_if_index,
1400                          &e_port))
1401         addr_only = 0;
1402
1403       else if (unformat (line_input, "external %U",
1404                          unformat_vnet_sw_interface, vnm, &sw_if_index))
1405         ;
1406       else if (unformat (line_input, "vrf %u", &vrf_id))
1407         ;
1408       else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
1409         proto_set = 1;
1410       else if (unformat (line_input, "del"))
1411         is_add = 0;
1412       else
1413         {
1414           error = clib_error_return (0, "unknown input: '%U'",
1415             format_unformat_error, line_input);
1416           goto done;
1417         }
1418     }
1419
1420   if (!addr_only && !proto_set)
1421     {
1422       error = clib_error_return (0, "missing protocol");
1423       goto done;
1424     }
1425
1426   rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1427                                vrf_id, addr_only, sw_if_index, proto, is_add);
1428
1429   switch (rv)
1430     {
1431     case VNET_API_ERROR_INVALID_VALUE:
1432       error = clib_error_return (0, "External port already in use.");
1433       goto done;
1434     case VNET_API_ERROR_NO_SUCH_ENTRY:
1435       if (is_add)
1436         error = clib_error_return (0, "External addres must be allocated.");
1437       else
1438         error = clib_error_return (0, "Mapping not exist.");
1439       goto done;
1440     case VNET_API_ERROR_NO_SUCH_FIB:
1441       error = clib_error_return (0, "No such VRF id.");
1442       goto done;
1443     case VNET_API_ERROR_VALUE_EXIST:
1444       error = clib_error_return (0, "Mapping already exist.");
1445       goto done;
1446     default:
1447       break;
1448     }
1449
1450 done:
1451   unformat_free (line_input);
1452
1453   return error;
1454 }
1455
1456 /*?
1457  * @cliexpar
1458  * @cliexstart{snat add static mapping}
1459  * Static mapping allows hosts on the external network to initiate connection
1460  * to to the local network host.
1461  * To create static mapping between local host address 10.0.0.3 port 6303 and
1462  * external address 4.4.4.4 port 3606 for TCP protocol use:
1463  *  vpp# snat add static mapping local tcp 10.0.0.3 6303 external 4.4.4.4 3606
1464  * If not runnig "static mapping only" S-NAT plugin mode use before:
1465  *  vpp# snat add address 4.4.4.4
1466  * To create static mapping between local and external address use:
1467  *  vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1468  * @cliexend
1469 ?*/
1470 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1471   .path = "snat add static mapping",
1472   .function = add_static_mapping_command_fn,
1473   .short_help =
1474     "snat add static mapping local tcp|udp|icmp <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1475 };
1476
1477 static clib_error_t *
1478 set_workers_command_fn (vlib_main_t * vm,
1479                         unformat_input_t * input,
1480                         vlib_cli_command_t * cmd)
1481 {
1482   unformat_input_t _line_input, *line_input = &_line_input;
1483   uword *bitmap = 0;
1484   int rv = 0;
1485   clib_error_t *error = 0;
1486
1487   /* Get a line of input. */
1488   if (!unformat_user (input, unformat_line_input, line_input))
1489     return 0;
1490
1491   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1492     {
1493       if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
1494         ;
1495       else
1496         {
1497           error = clib_error_return (0, "unknown input '%U'",
1498             format_unformat_error, line_input);
1499           goto done;
1500         }
1501      }
1502
1503   if (bitmap == 0)
1504     {
1505       error = clib_error_return (0, "List of workers must be specified.");
1506       goto done;
1507     }
1508
1509   rv = snat_set_workers(bitmap);
1510
1511   clib_bitmap_free (bitmap);
1512
1513   switch (rv)
1514     {
1515     case VNET_API_ERROR_INVALID_WORKER:
1516       error = clib_error_return (0, "Invalid worker(s).");
1517       goto done;
1518     case VNET_API_ERROR_FEATURE_DISABLED:
1519       error = clib_error_return (0,
1520         "Supported only if 2 or more workes available.");
1521       goto done;
1522     default:
1523       break;
1524     }
1525
1526 done:
1527   unformat_free (line_input);
1528
1529   return error;
1530 }
1531
1532 /*?
1533  * @cliexpar
1534  * @cliexstart{set snat workers}
1535  * Set SNAT workers if 2 or more workers available, use:
1536  *  vpp# set snat workers 0-2,5
1537  * @cliexend
1538 ?*/
1539 VLIB_CLI_COMMAND (set_workers_command, static) = {
1540   .path = "set snat workers",
1541   .function = set_workers_command_fn,
1542   .short_help =
1543     "set snat workers <workers-list>",
1544 };
1545
1546 static clib_error_t *
1547 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
1548                                               unformat_input_t * input,
1549                                               vlib_cli_command_t * cmd)
1550 {
1551   unformat_input_t _line_input, *line_input = &_line_input;
1552   u32 domain_id = 0;
1553   u32 src_port = 0;
1554   u8 enable = 1;
1555   int rv = 0;
1556   clib_error_t *error = 0;
1557
1558   /* Get a line of input. */
1559   if (!unformat_user (input, unformat_line_input, line_input))
1560     return 0;
1561
1562   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1563     {
1564       if (unformat (line_input, "domain %d", &domain_id))
1565         ;
1566       else if (unformat (line_input, "src-port %d", &src_port))
1567         ;
1568       else if (unformat (line_input, "disable"))
1569         enable = 0;
1570       else
1571         {
1572           error = clib_error_return (0, "unknown input '%U'",
1573             format_unformat_error, line_input);
1574           goto done;
1575         }
1576      }
1577
1578   rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
1579
1580   if (rv)
1581     {
1582       error = clib_error_return (0, "ipfix logging enable failed");
1583       goto done;
1584     }
1585
1586 done:
1587   unformat_free (line_input);
1588
1589   return error;
1590 }
1591
1592 /*?
1593  * @cliexpar
1594  * @cliexstart{snat ipfix logging}
1595  * To enable SNAT IPFIX logging use:
1596  *  vpp# snat ipfix logging
1597  * To set IPFIX exporter use:
1598  *  vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1599  * @cliexend
1600 ?*/
1601 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1602   .path = "snat ipfix logging",
1603   .function = snat_ipfix_logging_enable_disable_command_fn,
1604   .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1605 };
1606
1607 static u32
1608 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1609 {
1610   snat_main_t *sm = &snat_main;
1611   snat_user_key_t key0;
1612   clib_bihash_kv_8_8_t kv0, value0;
1613   u32 next_worker_index = 0;
1614
1615   key0.addr = ip0->src_address;
1616   key0.fib_index = rx_fib_index0;
1617
1618   kv0.key = key0.as_u64;
1619
1620   /* Ever heard of of the "user" before? */
1621   if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv0, &value0))
1622     {
1623       /* No, assign next available worker (RR) */
1624       next_worker_index = sm->first_worker_index;
1625       if (vec_len (sm->workers))
1626         {
1627           next_worker_index +=
1628             sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1629         }
1630
1631       /* add non-traslated packets worker lookup */
1632       kv0.value = next_worker_index;
1633       clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
1634     }
1635   else
1636     next_worker_index = value0.value;
1637
1638   return next_worker_index;
1639 }
1640
1641 static u32
1642 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1643 {
1644   snat_main_t *sm = &snat_main;
1645   snat_worker_key_t key0;
1646   clib_bihash_kv_8_8_t kv0, value0;
1647   udp_header_t * udp0;
1648   u32 next_worker_index = 0;
1649
1650   udp0 = ip4_next_header (ip0);
1651
1652   key0.addr = ip0->dst_address;
1653   key0.port = udp0->dst_port;
1654   key0.fib_index = rx_fib_index0;
1655
1656   if (PREDICT_FALSE(ip0->protocol == IP_PROTOCOL_ICMP))
1657     {
1658       icmp46_header_t * icmp0 = (icmp46_header_t *) udp0;
1659       icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
1660       key0.port = echo0->identifier;
1661     }
1662
1663   kv0.key = key0.as_u64;
1664
1665   /* Ever heard of of the "user" before? */
1666   if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1667     {
1668       key0.port = 0;
1669       kv0.key = key0.as_u64;
1670
1671       if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1672         {
1673           /* No, assign next available worker (RR) */
1674           next_worker_index = sm->first_worker_index;
1675           if (vec_len (sm->workers))
1676             {
1677               next_worker_index +=
1678                 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1679             }
1680         }
1681       else
1682         {
1683           /* Static mapping without port */
1684           next_worker_index = value0.value;
1685         }
1686
1687       /* Add to translated packets worker lookup */
1688       kv0.value = next_worker_index;
1689       clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
1690     }
1691   else
1692     next_worker_index = value0.value;
1693
1694   return next_worker_index;
1695 }
1696
1697 static clib_error_t *
1698 snat_config (vlib_main_t * vm, unformat_input_t * input)
1699 {
1700   snat_main_t * sm = &snat_main;
1701   u32 translation_buckets = 1024;
1702   u32 translation_memory_size = 128<<20;
1703   u32 user_buckets = 128;
1704   u32 user_memory_size = 64<<20;
1705   u32 max_translations_per_user = 100;
1706   u32 outside_vrf_id = 0;
1707   u32 inside_vrf_id = 0;
1708   u32 static_mapping_buckets = 1024;
1709   u32 static_mapping_memory_size = 64<<20;
1710   u8 static_mapping_only = 0;
1711   u8 static_mapping_connection_tracking = 0;
1712   vlib_thread_main_t *tm = vlib_get_thread_main ();
1713
1714   sm->deterministic = 0;
1715
1716   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1717     {
1718       if (unformat (input, "translation hash buckets %d", &translation_buckets))
1719         ;
1720       else if (unformat (input, "translation hash memory %d",
1721                          &translation_memory_size));
1722       else if (unformat (input, "user hash buckets %d", &user_buckets))
1723         ;
1724       else if (unformat (input, "user hash memory %d",
1725                          &user_memory_size))
1726         ;
1727       else if (unformat (input, "max translations per user %d",
1728                          &max_translations_per_user))
1729         ;
1730       else if (unformat (input, "outside VRF id %d",
1731                          &outside_vrf_id))
1732         ;
1733       else if (unformat (input, "inside VRF id %d",
1734                          &inside_vrf_id))
1735         ;
1736       else if (unformat (input, "static mapping only"))
1737         {
1738           static_mapping_only = 1;
1739           if (unformat (input, "connection tracking"))
1740             static_mapping_connection_tracking = 1;
1741         }
1742       else if (unformat (input, "deterministic"))
1743         sm->deterministic = 1;
1744       else
1745         return clib_error_return (0, "unknown input '%U'",
1746                                   format_unformat_error, input);
1747     }
1748
1749   /* for show commands, etc. */
1750   sm->translation_buckets = translation_buckets;
1751   sm->translation_memory_size = translation_memory_size;
1752   sm->user_buckets = user_buckets;
1753   sm->user_memory_size = user_memory_size;
1754   sm->max_translations_per_user = max_translations_per_user;
1755   sm->outside_vrf_id = outside_vrf_id;
1756   sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1757                                                              outside_vrf_id);
1758   sm->inside_vrf_id = inside_vrf_id;
1759   sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1760                                                             inside_vrf_id);
1761   sm->static_mapping_only = static_mapping_only;
1762   sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
1763
1764   if (sm->deterministic)
1765     {
1766       sm->in2out_node_index = snat_det_in2out_node.index;
1767       sm->in2out_output_node_index = ~0;
1768       sm->out2in_node_index = snat_det_out2in_node.index;
1769       sm->icmp_match_in2out_cb = icmp_match_in2out_det;
1770       sm->icmp_match_out2in_cb = icmp_match_out2in_det;
1771     }
1772   else
1773     {
1774       sm->worker_in2out_cb = snat_get_worker_in2out_cb;
1775       sm->worker_out2in_cb = snat_get_worker_out2in_cb;
1776       sm->in2out_node_index = snat_in2out_node.index;
1777       sm->in2out_output_node_index = snat_in2out_output_node.index;
1778       sm->out2in_node_index = snat_out2in_node.index;
1779       if (!static_mapping_only ||
1780           (static_mapping_only && static_mapping_connection_tracking))
1781         {
1782           sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
1783           sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
1784
1785           clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
1786                                 user_memory_size);
1787
1788           clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
1789                                 user_memory_size);
1790
1791           vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1792
1793           clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1794                                 translation_memory_size);
1795
1796           clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1797                                 translation_memory_size);
1798
1799           clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1800                                 user_memory_size);
1801
1802           clib_bihash_init_16_8 (&sm->in2out_unk_proto, "in2out-unk-proto",
1803                                  translation_buckets, translation_memory_size);
1804
1805           clib_bihash_init_16_8 (&sm->out2in_unk_proto, "out2in-unk-proto",
1806                                  translation_buckets, translation_memory_size);
1807         }
1808       else
1809         {
1810           sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
1811           sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
1812         }
1813       clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1814                             "static_mapping_by_local", static_mapping_buckets,
1815                             static_mapping_memory_size);
1816
1817       clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1818                             "static_mapping_by_external", static_mapping_buckets,
1819                             static_mapping_memory_size);
1820     }
1821
1822   return 0;
1823 }
1824
1825 VLIB_CONFIG_FUNCTION (snat_config, "snat");
1826
1827 u8 * format_snat_session_state (u8 * s, va_list * args)
1828 {
1829   u32 i = va_arg (*args, u32);
1830   u8 *t = 0;
1831
1832   switch (i)
1833     {
1834 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
1835     foreach_snat_session_state
1836 #undef _
1837     default:
1838       t = format (t, "unknown");
1839     }
1840   s = format (s, "%s", t);
1841   return s;
1842 }
1843
1844 u8 * format_snat_key (u8 * s, va_list * args)
1845 {
1846   snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1847   char * protocol_string = "unknown";
1848   static char *protocol_strings[] = {
1849       "UDP",
1850       "TCP",
1851       "ICMP",
1852   };
1853
1854   if (key->protocol < ARRAY_LEN(protocol_strings))
1855       protocol_string = protocol_strings[key->protocol];
1856
1857   s = format (s, "%U proto %s port %d fib %d",
1858               format_ip4_address, &key->addr, protocol_string,
1859               clib_net_to_host_u16 (key->port), key->fib_index);
1860   return s;
1861 }
1862
1863 u8 * format_snat_session (u8 * s, va_list * args)
1864 {
1865   snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
1866   snat_session_t * sess = va_arg (*args, snat_session_t *);
1867
1868   if (snat_is_unk_proto_session (sess))
1869     {
1870       s = format (s, "  i2o %U proto %u fib %u\n",
1871                   format_ip4_address, &sess->in2out.addr, sess->in2out.port,
1872                   sess->in2out.fib_index);
1873       s = format (s, "    o2i %U proto %u fib %u\n",
1874                   format_ip4_address, &sess->out2in.addr, sess->out2in.port,
1875                   sess->out2in.fib_index);
1876     }
1877   else
1878     {
1879       s = format (s, "  i2o %U\n", format_snat_key, &sess->in2out);
1880       s = format (s, "    o2i %U\n", format_snat_key, &sess->out2in);
1881     }
1882   s = format (s, "       last heard %.2f\n", sess->last_heard);
1883   s = format (s, "       total pkts %d, total bytes %lld\n",
1884               sess->total_pkts, sess->total_bytes);
1885   if (snat_is_session_static (sess))
1886     s = format (s, "       static translation\n");
1887   else
1888     s = format (s, "       dynamic translation\n");
1889
1890   return s;
1891 }
1892
1893 u8 * format_snat_user (u8 * s, va_list * args)
1894 {
1895   snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
1896   snat_user_t * u = va_arg (*args, snat_user_t *);
1897   int verbose = va_arg (*args, int);
1898   dlist_elt_t * head, * elt;
1899   u32 elt_index, head_index;
1900   u32 session_index;
1901   snat_session_t * sess;
1902
1903   s = format (s, "%U: %d dynamic translations, %d static translations\n",
1904               format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
1905
1906   if (verbose == 0)
1907     return s;
1908
1909   if (u->nsessions || u->nstaticsessions)
1910     {
1911       head_index = u->sessions_per_user_list_head_index;
1912       head = pool_elt_at_index (sm->list_pool, head_index);
1913
1914       elt_index = head->next;
1915       elt = pool_elt_at_index (sm->list_pool, elt_index);
1916       session_index = elt->value;
1917
1918       while (session_index != ~0)
1919         {
1920           sess = pool_elt_at_index (sm->sessions, session_index);
1921
1922           s = format (s, "  %U\n", format_snat_session, sm, sess);
1923
1924           elt_index = elt->next;
1925           elt = pool_elt_at_index (sm->list_pool, elt_index);
1926           session_index = elt->value;
1927         }
1928     }
1929
1930   return s;
1931 }
1932
1933 u8 * format_snat_static_mapping (u8 * s, va_list * args)
1934 {
1935   snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
1936
1937   if (m->addr_only)
1938       s = format (s, "local %U external %U vrf %d",
1939                   format_ip4_address, &m->local_addr,
1940                   format_ip4_address, &m->external_addr,
1941                   m->vrf_id);
1942   else
1943       s = format (s, "%U local %U:%d external %U:%d vrf %d",
1944                   format_snat_protocol, m->proto,
1945                   format_ip4_address, &m->local_addr, m->local_port,
1946                   format_ip4_address, &m->external_addr, m->external_port,
1947                   m->vrf_id);
1948
1949   return s;
1950 }
1951
1952 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
1953 {
1954   snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
1955   vnet_main_t *vnm = vnet_get_main();
1956
1957   if (m->addr_only)
1958       s = format (s, "local %U external %U vrf %d",
1959                   format_ip4_address, &m->l_addr,
1960                   format_vnet_sw_interface_name, vnm,
1961                   vnet_get_sw_interface (vnm, m->sw_if_index),
1962                   m->vrf_id);
1963   else
1964       s = format (s, "%U local %U:%d external %U:%d vrf %d",
1965                   format_snat_protocol, m->proto,
1966                   format_ip4_address, &m->l_addr, m->l_port,
1967                   format_vnet_sw_interface_name, vnm,
1968                   vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
1969                   m->vrf_id);
1970
1971   return s;
1972 }
1973
1974 u8 * format_det_map_ses (u8 * s, va_list * args)
1975 {
1976   snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
1977   ip4_address_t in_addr, out_addr;
1978   u32 in_offset, out_offset;
1979   snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
1980   u32 * i = va_arg (*args, u32 *);
1981
1982   u32 user_index = *i / SNAT_DET_SES_PER_USER;
1983   in_addr.as_u32 = clib_host_to_net_u32 (
1984     clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
1985   in_offset = clib_net_to_host_u32(in_addr.as_u32) -
1986     clib_net_to_host_u32(det_map->in_addr.as_u32);
1987   out_offset = in_offset / det_map->sharing_ratio;
1988   out_addr.as_u32 = clib_host_to_net_u32(
1989     clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
1990   s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
1991               format_ip4_address, &in_addr,
1992               clib_net_to_host_u16 (ses->in_port),
1993               format_ip4_address, &out_addr,
1994               clib_net_to_host_u16 (ses->out.out_port),
1995               format_ip4_address, &ses->out.ext_host_addr,
1996               clib_net_to_host_u16 (ses->out.ext_host_port),
1997               format_snat_session_state, ses->state,
1998               ses->expire);
1999
2000   return s;
2001 }
2002
2003 static clib_error_t *
2004 show_snat_command_fn (vlib_main_t * vm,
2005                  unformat_input_t * input,
2006                  vlib_cli_command_t * cmd)
2007 {
2008   int verbose = 0;
2009   snat_main_t * sm = &snat_main;
2010   snat_user_t * u;
2011   snat_static_mapping_t *m;
2012   snat_interface_t *i;
2013   snat_address_t * ap;
2014   vnet_main_t *vnm = vnet_get_main();
2015   snat_main_per_thread_data_t *tsm;
2016   u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2017   uword j = 0;
2018   snat_static_map_resolve_t *rp;
2019   snat_det_map_t * dm;
2020   snat_det_session_t * ses;
2021
2022   if (unformat (input, "detail"))
2023     verbose = 1;
2024   else if (unformat (input, "verbose"))
2025     verbose = 2;
2026
2027   if (sm->static_mapping_only)
2028     {
2029       if (sm->static_mapping_connection_tracking)
2030         vlib_cli_output (vm, "SNAT mode: static mapping only connection "
2031                          "tracking");
2032       else
2033         vlib_cli_output (vm, "SNAT mode: static mapping only");
2034     }
2035   else if (sm->deterministic)
2036     {
2037       vlib_cli_output (vm, "SNAT mode: deterministic mapping");
2038     }
2039   else
2040     {
2041       vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
2042     }
2043
2044   if (verbose > 0)
2045     {
2046       pool_foreach (i, sm->interfaces,
2047       ({
2048         vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2049                          vnet_get_sw_interface (vnm, i->sw_if_index),
2050                          i->is_inside ? "in" : "out");
2051       }));
2052
2053       pool_foreach (i, sm->output_feature_interfaces,
2054       ({
2055         vlib_cli_output (vm, "%U output-feature %s",
2056                          format_vnet_sw_interface_name, vnm,
2057                          vnet_get_sw_interface (vnm, i->sw_if_index),
2058                          i->is_inside ? "in" : "out");
2059       }));
2060
2061       if (vec_len (sm->auto_add_sw_if_indices))
2062         {
2063           vlib_cli_output (vm, "SNAT pool addresses interfaces:");
2064           vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2065             {
2066               vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2067                                vnet_get_sw_interface (vnm, *sw_if_index));
2068             }
2069         }
2070
2071       vec_foreach (ap, sm->addresses)
2072         {
2073           vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2074           if (ap->fib_index != ~0)
2075               vlib_cli_output (vm, "  tenant VRF: %u",
2076                                ip4_fib_get(ap->fib_index)->table_id);
2077           else
2078             vlib_cli_output (vm, "  tenant VRF independent");
2079 #define _(N, i, n, s) \
2080           vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
2081           foreach_snat_protocol
2082 #undef _
2083         }
2084     }
2085
2086   if (sm->num_workers > 1)
2087     {
2088       vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2089       if (verbose > 0)
2090         {
2091           vec_foreach (worker, sm->workers)
2092             {
2093               vlib_worker_thread_t *w =
2094                 vlib_worker_threads + *worker + sm->first_worker_index;
2095               vlib_cli_output (vm, "  %s", w->name);
2096             }
2097         }
2098     }
2099
2100   if (sm->deterministic)
2101     {
2102       vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
2103       vlib_cli_output (vm, "tcp-established timeout: %dsec",
2104                        sm->tcp_established_timeout);
2105       vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
2106                        sm->tcp_transitory_timeout);
2107       vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
2108       vlib_cli_output (vm, "%d deterministic mappings",
2109                        pool_elts (sm->det_maps));
2110       if (verbose > 0)
2111         {
2112           pool_foreach (dm, sm->det_maps,
2113           ({
2114             vlib_cli_output (vm, "in %U/%d out %U/%d\n",
2115                              format_ip4_address, &dm->in_addr, dm->in_plen,
2116                              format_ip4_address, &dm->out_addr, dm->out_plen);
2117             vlib_cli_output (vm, " outside address sharing ratio: %d\n",
2118                              dm->sharing_ratio);
2119             vlib_cli_output (vm, " number of ports per inside host: %d\n",
2120                              dm->ports_per_host);
2121             vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
2122             if (verbose > 1)
2123               {
2124                 vec_foreach_index (j, dm->sessions)
2125                   {
2126                     ses = vec_elt_at_index (dm->sessions, j);
2127                     if (ses->in_port)
2128                       vlib_cli_output (vm, "  %U", format_det_map_ses, dm, ses,
2129                                        &j);
2130                   }
2131               }
2132           }));
2133         }
2134     }
2135   else
2136     {
2137       if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2138         {
2139           vlib_cli_output (vm, "%d static mappings",
2140                            pool_elts (sm->static_mappings));
2141
2142           if (verbose > 0)
2143             {
2144               pool_foreach (m, sm->static_mappings,
2145               ({
2146                 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2147               }));
2148             }
2149         }
2150       else
2151         {
2152           vec_foreach (tsm, sm->per_thread_data)
2153             {
2154               users_num += pool_elts (tsm->users);
2155               sessions_num += pool_elts (tsm->sessions);
2156             }
2157
2158           vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2159                            " %d static mappings",
2160                            users_num,
2161                            vec_len (sm->addresses),
2162                            sessions_num,
2163                            pool_elts (sm->static_mappings));
2164
2165           if (verbose > 0)
2166             {
2167               vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
2168                                verbose - 1);
2169               vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
2170                                verbose - 1);
2171               vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2172                                verbose - 1);
2173               vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2174                                verbose - 1);
2175               vec_foreach_index (j, sm->per_thread_data)
2176                 {
2177                   tsm = vec_elt_at_index (sm->per_thread_data, j);
2178
2179                   if (pool_elts (tsm->users) == 0)
2180                     continue;
2181
2182                   vlib_worker_thread_t *w = vlib_worker_threads + j;
2183                   vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2184                                    w->lcore_id);
2185                   vlib_cli_output (vm, "  %d list pool elements",
2186                                    pool_elts (tsm->list_pool));
2187
2188                   pool_foreach (u, tsm->users,
2189                   ({
2190                     vlib_cli_output (vm, "  %U", format_snat_user, tsm, u,
2191                                      verbose - 1);
2192                   }));
2193                 }
2194
2195               if (pool_elts (sm->static_mappings))
2196                 {
2197                   vlib_cli_output (vm, "static mappings:");
2198                   pool_foreach (m, sm->static_mappings,
2199                   ({
2200                     vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2201                   }));
2202                   for (j = 0; j < vec_len (sm->to_resolve); j++)
2203                     {
2204                       rp = sm->to_resolve + j;
2205                       vlib_cli_output (vm, "%U",
2206                                        format_snat_static_map_to_resolve, rp);
2207                     }
2208                 }
2209             }
2210         }
2211     }
2212   return 0;
2213 }
2214
2215 VLIB_CLI_COMMAND (show_snat_command, static) = {
2216     .path = "show snat",
2217     .short_help = "show snat",
2218     .function = show_snat_command_fn,
2219 };
2220
2221
2222 static void
2223 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2224                                        uword opaque,
2225                                        u32 sw_if_index,
2226                                        ip4_address_t * address,
2227                                        u32 address_length,
2228                                        u32 if_address_index,
2229                                        u32 is_delete)
2230 {
2231   snat_main_t *sm = &snat_main;
2232   snat_static_map_resolve_t *rp;
2233   u32 *indices_to_delete = 0;
2234   int i, j;
2235   int rv;
2236
2237   for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2238     {
2239       if (sw_if_index == sm->auto_add_sw_if_indices[i])
2240         {
2241           if (!is_delete)
2242             {
2243               /* Don't trip over lease renewal, static config */
2244               for (j = 0; j < vec_len(sm->addresses); j++)
2245                 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2246                   return;
2247
2248               snat_add_address (sm, address, ~0);
2249               /* Scan static map resolution vector */
2250               for (j = 0; j < vec_len (sm->to_resolve); j++)
2251                 {
2252                   rp = sm->to_resolve + j;
2253                   /* On this interface? */
2254                   if (rp->sw_if_index == sw_if_index)
2255                     {
2256                       /* Add the static mapping */
2257                       rv = snat_add_static_mapping (rp->l_addr,
2258                                                     address[0],
2259                                                     rp->l_port,
2260                                                     rp->e_port,
2261                                                     rp->vrf_id,
2262                                                     rp->addr_only,
2263                                                     ~0 /* sw_if_index */,
2264                                                     rp->proto,
2265                                                     rp->is_add);
2266                       if (rv)
2267                         clib_warning ("snat_add_static_mapping returned %d", 
2268                                       rv);
2269                       vec_add1 (indices_to_delete, j);
2270                     }
2271                 }
2272               /* If we resolved any of the outstanding static mappings */
2273               if (vec_len(indices_to_delete))
2274                 {
2275                   /* Delete them */
2276                   for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2277                     vec_delete(sm->to_resolve, 1, j);
2278                   vec_free(indices_to_delete);
2279                 }
2280               return;
2281             }
2282           else
2283             {
2284               (void) snat_del_address(sm, address[0], 1);
2285               return;
2286             }
2287         }
2288     }
2289 }
2290
2291
2292 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del)
2293 {
2294   ip4_main_t * ip4_main = sm->ip4_main;
2295   ip4_address_t * first_int_addr;
2296   snat_static_map_resolve_t *rp;
2297   u32 *indices_to_delete = 0;
2298   int i, j;
2299
2300   first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2301                                                 0 /* just want the address*/);
2302
2303   for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2304     {
2305       if (sm->auto_add_sw_if_indices[i] == sw_if_index)
2306         {
2307           if (is_del)
2308             {
2309               /* if have address remove it */
2310               if (first_int_addr)
2311                   (void) snat_del_address (sm, first_int_addr[0], 1);
2312               else
2313                 {
2314                   for (j = 0; j < vec_len (sm->to_resolve); j++)
2315                     {
2316                       rp = sm->to_resolve + j;
2317                       if (rp->sw_if_index == sw_if_index)
2318                         vec_add1 (indices_to_delete, j);
2319                     }
2320                   if (vec_len(indices_to_delete))
2321                     {
2322                       for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2323                         vec_del1(sm->to_resolve, j);
2324                       vec_free(indices_to_delete);
2325                     }
2326                 }
2327               vec_del1(sm->auto_add_sw_if_indices, i);
2328             }
2329           else
2330             return VNET_API_ERROR_VALUE_EXIST;
2331
2332           return 0;
2333         }
2334     }
2335   
2336   if (is_del)
2337     return VNET_API_ERROR_NO_SUCH_ENTRY;
2338
2339   /* add to the auto-address list */
2340   vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2341
2342   /* If the address is already bound - or static - add it now */
2343   if (first_int_addr)
2344       snat_add_address (sm, first_int_addr, ~0);
2345
2346   return 0;
2347 }
2348
2349 static clib_error_t *
2350 snat_add_interface_address_command_fn (vlib_main_t * vm,
2351                                        unformat_input_t * input,
2352                                        vlib_cli_command_t * cmd)
2353 {
2354   snat_main_t *sm = &snat_main;
2355   unformat_input_t _line_input, *line_input = &_line_input;
2356   u32 sw_if_index;
2357   int rv;
2358   int is_del = 0;
2359   clib_error_t *error = 0;
2360
2361   /* Get a line of input. */
2362   if (!unformat_user (input, unformat_line_input, line_input))
2363     return 0;
2364
2365   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2366     {
2367       if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2368                     sm->vnet_main, &sw_if_index))
2369         ;
2370       else if (unformat (line_input, "del"))
2371         is_del = 1;
2372       else
2373         {
2374           error = clib_error_return (0, "unknown input '%U'",
2375                                      format_unformat_error, line_input);
2376           goto done;
2377         }
2378     }
2379
2380   rv = snat_add_interface_address (sm, sw_if_index, is_del);
2381
2382   switch (rv)
2383     {
2384     case 0:
2385       break;
2386
2387     default:
2388       error = clib_error_return (0, "snat_add_interface_address returned %d",
2389                                  rv);
2390       goto done;
2391     }
2392
2393 done:
2394   unformat_free (line_input);
2395
2396   return error;
2397 }
2398
2399 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2400     .path = "snat add interface address",
2401     .short_help = "snat add interface address <interface> [del]",
2402     .function = snat_add_interface_address_command_fn,
2403 };
2404
2405 static clib_error_t *
2406 snat_det_map_command_fn (vlib_main_t * vm,
2407                          unformat_input_t * input,
2408                          vlib_cli_command_t * cmd)
2409 {
2410   snat_main_t *sm = &snat_main;
2411   unformat_input_t _line_input, *line_input = &_line_input;
2412   ip4_address_t in_addr, out_addr;
2413   u32 in_plen, out_plen;
2414   int is_add = 1, rv;
2415   clib_error_t *error = 0;
2416
2417   /* Get a line of input. */
2418   if (!unformat_user (input, unformat_line_input, line_input))
2419     return 0;
2420
2421   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2422     {
2423       if (unformat (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
2424         ;
2425       else if (unformat (line_input, "out %U/%u", unformat_ip4_address, &out_addr, &out_plen))
2426         ;
2427       else if (unformat (line_input, "del"))
2428         is_add = 0;
2429       else
2430         {
2431           error = clib_error_return (0, "unknown input '%U'",
2432                                      format_unformat_error, line_input);
2433           goto done;
2434         }
2435     }
2436
2437   unformat_free (line_input);
2438
2439   rv = snat_det_add_map(sm, &in_addr, (u8) in_plen, &out_addr, (u8)out_plen,
2440                         is_add);
2441
2442   if (rv)
2443     {
2444       error = clib_error_return (0, "snat_det_add_map return %d", rv);
2445       goto done;
2446     }
2447
2448 done:
2449   unformat_free (line_input);
2450
2451   return error;
2452 }
2453
2454 /*?
2455  * @cliexpar
2456  * @cliexstart{snat deterministic add}
2457  * Create bijective mapping of inside address to outside address and port range
2458  * pairs, with the purpose of enabling deterministic NAT to reduce logging in
2459  * CGN deployments.
2460  * To create deterministic mapping between inside network 10.0.0.0/18 and
2461  * outside network 1.1.1.0/30 use:
2462  * # vpp# snat deterministic add in 10.0.0.0/18 out 1.1.1.0/30
2463  * @cliexend
2464 ?*/
2465 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
2466     .path = "snat deterministic add",
2467     .short_help = "snat deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
2468     .function = snat_det_map_command_fn,
2469 };
2470
2471 static clib_error_t *
2472 snat_det_forward_command_fn (vlib_main_t * vm,
2473                              unformat_input_t * input,
2474                              vlib_cli_command_t * cmd)
2475 {
2476   snat_main_t *sm = &snat_main;
2477   unformat_input_t _line_input, *line_input = &_line_input;
2478   ip4_address_t in_addr, out_addr;
2479   u16 lo_port;
2480   snat_det_map_t * dm;
2481   clib_error_t *error = 0;
2482
2483   /* Get a line of input. */
2484   if (!unformat_user (input, unformat_line_input, line_input))
2485     return 0;
2486
2487   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2488     {
2489       if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
2490         ;
2491       else
2492         {
2493           error = clib_error_return (0, "unknown input '%U'",
2494                                      format_unformat_error, line_input);
2495           goto done;
2496         }
2497     }
2498
2499   unformat_free (line_input);
2500
2501   dm = snat_det_map_by_user(sm, &in_addr);
2502   if (!dm)
2503     vlib_cli_output (vm, "no match");
2504   else
2505     {
2506       snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2507       vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
2508                        lo_port, lo_port + dm->ports_per_host - 1);
2509     }
2510
2511 done:
2512   unformat_free (line_input);
2513
2514   return error;
2515 }
2516
2517 /*?
2518  * @cliexpar
2519  * @cliexstart{snat deterministic forward}
2520  * Return outside address and port range from inside address for deterministic
2521  * NAT.
2522  * To obtain outside address and port of inside host use:
2523  *  vpp# snat deterministic forward 10.0.0.2
2524  *  1.1.1.0:<1054-1068>
2525  * @cliexend
2526 ?*/
2527 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
2528     .path = "snat deterministic forward",
2529     .short_help = "snat deterministic forward <addr>",
2530     .function = snat_det_forward_command_fn,
2531 };
2532
2533 static clib_error_t *
2534 snat_det_reverse_command_fn (vlib_main_t * vm,
2535                              unformat_input_t * input,
2536                              vlib_cli_command_t * cmd)
2537 {
2538   snat_main_t *sm = &snat_main;
2539   unformat_input_t _line_input, *line_input = &_line_input;
2540   ip4_address_t in_addr, out_addr;
2541   u32 out_port;
2542   snat_det_map_t * dm;
2543   clib_error_t *error = 0;
2544
2545   /* Get a line of input. */
2546   if (!unformat_user (input, unformat_line_input, line_input))
2547     return 0;
2548
2549   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2550     {
2551       if (unformat (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
2552         ;
2553       else
2554         {
2555           error =  clib_error_return (0, "unknown input '%U'",
2556                                       format_unformat_error, line_input);
2557         }
2558     }
2559
2560   unformat_free (line_input);
2561
2562   if (out_port < 1024 || out_port > 65535)
2563     {
2564       error = clib_error_return (0, "wrong port, must be <1024-65535>");
2565       goto done;
2566     }
2567
2568   dm = snat_det_map_by_out(sm, &out_addr);
2569   if (!dm)
2570     vlib_cli_output (vm, "no match");
2571   else
2572     {
2573       snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
2574       vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
2575     }
2576
2577 done:
2578   unformat_free (line_input);
2579
2580   return error;
2581 }
2582
2583 /*?
2584  * @cliexpar
2585  * @cliexstart{snat deterministic reverse}
2586  * Return inside address from outside address and port for deterministic NAT.
2587  * To obtain inside host address from outside address and port use:
2588  *  #vpp snat deterministic reverse 1.1.1.1:1276
2589  *  10.0.16.16
2590  * @cliexend
2591 ?*/
2592 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
2593     .path = "snat deterministic reverse",
2594     .short_help = "snat deterministic reverse <addr>:<port>",
2595     .function = snat_det_reverse_command_fn,
2596 };
2597
2598 static clib_error_t *
2599 set_timeout_command_fn (vlib_main_t * vm,
2600                         unformat_input_t * input,
2601                         vlib_cli_command_t * cmd)
2602 {
2603   snat_main_t *sm = &snat_main;
2604   unformat_input_t _line_input, *line_input = &_line_input;
2605   clib_error_t *error = 0;
2606
2607   /* Get a line of input. */
2608   if (!unformat_user (input, unformat_line_input, line_input))
2609     return 0;
2610
2611   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2612     {
2613       if (unformat (line_input, "udp %u", &sm->udp_timeout))
2614         ;
2615       else if (unformat (line_input, "tcp-established %u",
2616                &sm->tcp_established_timeout))
2617         ;
2618       else if (unformat (line_input, "tcp-transitory %u",
2619                &sm->tcp_transitory_timeout))
2620         ;
2621       else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
2622         ;
2623       else if (unformat (line_input, "reset"))
2624         {
2625           sm->udp_timeout = SNAT_UDP_TIMEOUT;
2626           sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
2627           sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
2628           sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
2629         }
2630       else
2631         {
2632           error = clib_error_return (0, "unknown input '%U'",
2633                                      format_unformat_error, line_input);
2634           goto done;
2635         }
2636     }
2637
2638   unformat_free (line_input);
2639
2640 done:
2641   unformat_free (line_input);
2642
2643   return error;
2644 }
2645
2646 /*?
2647  * @cliexpar
2648  * @cliexstart{set snat deterministic timeout}
2649  * Set values of timeouts for deterministic NAT (in seconds), use:
2650  *  vpp# set snat deterministic timeout udp 120 tcp-established 7500
2651  *  tcp-transitory 250 icmp 90
2652  * To reset default values use:
2653  *  vpp# set snat deterministic timeout reset
2654  * @cliexend
2655 ?*/
2656 VLIB_CLI_COMMAND (set_timeout_command, static) = {
2657   .path = "set snat deterministic timeout",
2658   .function = set_timeout_command_fn,
2659   .short_help =
2660     "set snat deterministic timeout [udp <sec> | tcp-established <sec> "
2661     "tcp-transitory <sec> | icmp <sec> | reset]",
2662 };
2663
2664 static clib_error_t *
2665 snat_det_close_session_out_fn (vlib_main_t *vm,
2666                                unformat_input_t * input,
2667                                vlib_cli_command_t * cmd)
2668 {
2669   snat_main_t *sm = &snat_main;
2670   unformat_input_t _line_input, *line_input = &_line_input;
2671   ip4_address_t out_addr, ext_addr, in_addr;
2672   u16 out_port, ext_port;
2673   snat_det_map_t * dm;
2674   snat_det_session_t * ses;
2675   snat_det_out_key_t key;
2676   clib_error_t *error = 0;
2677
2678   /* Get a line of input. */
2679   if (!unformat_user (input, unformat_line_input, line_input))
2680     return 0;
2681
2682   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2683     {
2684       if (unformat (line_input, "%U:%d %U:%d",
2685                     unformat_ip4_address, &out_addr, &out_port,
2686                     unformat_ip4_address, &ext_addr, &ext_port))
2687         ;
2688       else
2689         {
2690           error = clib_error_return (0, "unknown input '%U'",
2691                                      format_unformat_error, line_input);
2692           goto done;
2693         }
2694     }
2695
2696   unformat_free (line_input);
2697
2698   dm = snat_det_map_by_out(sm, &out_addr);
2699   if (!dm)
2700     vlib_cli_output (vm, "no match");
2701   else
2702     {
2703       snat_det_reverse(dm, &ext_addr, out_port, &in_addr);
2704       key.ext_host_addr = out_addr;
2705       key.ext_host_port = ntohs(ext_port);
2706       key.out_port = ntohs(out_port);
2707       ses = snat_det_get_ses_by_out(dm, &out_addr, key.as_u64);
2708       if (!ses)
2709         vlib_cli_output (vm, "no match");
2710       else
2711        snat_det_ses_close(dm, ses);
2712     }
2713
2714 done:
2715   unformat_free (line_input);
2716
2717   return error;
2718 }
2719
2720 /*?
2721  * @cliexpar
2722  * @cliexstart{snat deterministic close session out}
2723  * Close session using outside ip address and port
2724  * and external ip address and port, use:
2725  *  vpp# snat deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
2726  * @cliexend
2727 ?*/
2728 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
2729   .path = "snat deterministic close session out",
2730   .short_help = "snat deterministic close session out "
2731                 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
2732   .function = snat_det_close_session_out_fn,
2733 };
2734
2735 static clib_error_t *
2736 snat_det_close_session_in_fn (vlib_main_t *vm,
2737                               unformat_input_t * input,
2738                               vlib_cli_command_t * cmd)
2739 {
2740   snat_main_t *sm = &snat_main;
2741   unformat_input_t _line_input, *line_input = &_line_input;
2742   ip4_address_t in_addr, ext_addr;
2743   u16 in_port, ext_port;
2744   snat_det_map_t * dm;
2745   snat_det_session_t * ses;
2746   snat_det_out_key_t key;
2747   clib_error_t *error = 0;
2748
2749   /* Get a line of input. */
2750   if (!unformat_user (input, unformat_line_input, line_input))
2751     return 0;
2752
2753   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2754     {
2755       if (unformat (line_input, "%U:%d %U:%d",
2756                     unformat_ip4_address, &in_addr, &in_port,
2757                     unformat_ip4_address, &ext_addr, &ext_port))
2758         ;
2759       else
2760         {
2761           error = clib_error_return (0, "unknown input '%U'",
2762                                      format_unformat_error, line_input);
2763           goto done;
2764         }
2765     }
2766
2767   unformat_free (line_input);
2768
2769   dm = snat_det_map_by_user (sm, &in_addr);
2770   if (!dm)
2771     vlib_cli_output (vm, "no match");
2772   else
2773     {
2774       key.ext_host_addr = ext_addr;
2775       key.ext_host_port = ntohs (ext_port);
2776       ses = snat_det_find_ses_by_in (dm, &in_addr, ntohs(in_port), key);
2777       if (!ses)
2778         vlib_cli_output (vm, "no match");
2779       else
2780         snat_det_ses_close(dm, ses);
2781     }
2782
2783 done:
2784   unformat_free(line_input);
2785
2786   return error;
2787 }
2788
2789 /*?
2790  * @cliexpar
2791  * @cliexstart{snat deterministic close_session_in}
2792  * Close session using inside ip address and port
2793  * and external ip address and port, use:
2794  *  vpp# snat deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
2795  * @cliexend
2796 ?*/
2797 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
2798   .path = "snat deterministic close session in",
2799   .short_help = "snat deterministic close session in "
2800                 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
2801   .function = snat_det_close_session_in_fn,
2802 };