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