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