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