2 * snat.c - simple nat plugin
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/plugin/plugin.h>
22 #include <snat/snat.h>
23 #include <snat/snat_ipfix_logging.h>
24 #include <snat/snat_det.h>
25 #include <vnet/fib/fib_table.h>
26 #include <vnet/fib/ip4_fib.h>
28 #include <vlibapi/api.h>
29 #include <vlibmemory/api.h>
30 #include <vlibsocket/api.h>
31 #include <vpp/app/version.h>
33 snat_main_t snat_main;
35 /* define message IDs */
36 #include <snat/snat_msg_enum.h>
38 /* define message structures */
40 #include <snat/snat_all_api_h.h>
43 /* define generated endian-swappers */
45 #include <snat/snat_all_api_h.h>
48 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
50 #define REPLY_MSG_ID_BASE sm->msg_id_base
51 #include <vlibapi/api_helper_macros.h>
53 /* Get the API version number */
54 #define vl_api_version(n,v) static u32 api_version=(v);
55 #include <snat/snat_all_api_h.h>
58 /* Macro to finish up custom dump fns */
61 vl_print (handle, (char *)s); \
65 /* Hook up input features */
66 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
67 .arc_name = "ip4-unicast",
68 .node_name = "snat-in2out",
69 .runs_before = VNET_FEATURES ("snat-out2in"),
71 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
72 .arc_name = "ip4-unicast",
73 .node_name = "snat-out2in",
74 .runs_before = VNET_FEATURES ("ip4-lookup"),
76 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
77 .arc_name = "ip4-unicast",
78 .node_name = "snat-det-in2out",
79 .runs_before = VNET_FEATURES ("snat-det-out2in"),
81 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
82 .arc_name = "ip4-unicast",
83 .node_name = "snat-det-out2in",
84 .runs_before = VNET_FEATURES ("ip4-lookup"),
86 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
87 .arc_name = "ip4-unicast",
88 .node_name = "snat-in2out-worker-handoff",
89 .runs_before = VNET_FEATURES ("snat-out2in-worker-handoff"),
91 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
92 .arc_name = "ip4-unicast",
93 .node_name = "snat-out2in-worker-handoff",
94 .runs_before = VNET_FEATURES ("ip4-lookup"),
96 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
97 .arc_name = "ip4-unicast",
98 .node_name = "snat-in2out-fast",
99 .runs_before = VNET_FEATURES ("snat-out2in-fast"),
101 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
102 .arc_name = "ip4-unicast",
103 .node_name = "snat-out2in-fast",
104 .runs_before = VNET_FEATURES ("ip4-lookup"),
108 VLIB_PLUGIN_REGISTER () = {
109 .version = VPP_BUILD_VER,
110 .description = "Network Address Translation",
115 * @brief Add/del NAT address to FIB.
117 * Add the external NAT address to the FIB as receive entries. This ensures
118 * that VPP will reply to ARP for this address and we don't need to enable
119 * proxy ARP on the outside interface.
121 * @param addr IPv4 address.
122 * @param plen address prefix length
123 * @param sw_if_index Interface.
124 * @param is_add If 0 delete, otherwise add.
127 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
130 fib_prefix_t prefix = {
132 .fp_proto = FIB_PROTOCOL_IP4,
134 .ip4.as_u32 = addr->as_u32,
137 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
140 fib_table_entry_update_one_path(fib_index,
142 FIB_SOURCE_PLUGIN_HI,
143 (FIB_ENTRY_FLAG_CONNECTED |
144 FIB_ENTRY_FLAG_LOCAL |
145 FIB_ENTRY_FLAG_EXCLUSIVE),
152 FIB_ROUTE_PATH_FLAG_NONE);
154 fib_table_entry_delete(fib_index,
156 FIB_SOURCE_PLUGIN_HI);
159 void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id)
167 /* Check if address already exists */
168 vec_foreach (ap, sm->addresses)
170 if (ap->addr.as_u32 == addr->as_u32)
174 vec_add2 (sm->addresses, ap, 1);
176 ap->fib_index = ip4_fib_index_from_table_id(vrf_id);
177 #define _(N, i, n, s) \
178 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535);
179 foreach_snat_protocol
182 /* Add external address to FIB */
183 pool_foreach (i, sm->interfaces,
188 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
193 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
196 snat_static_mapping_t *m;
197 pool_foreach (m, sm->static_mappings,
199 if (m->external_addr.as_u32 == addr.as_u32)
206 static void increment_v4_address (ip4_address_t * a)
210 v = clib_net_to_host_u32(a->as_u32) + 1;
211 a->as_u32 = clib_host_to_net_u32(v);
215 snat_add_static_mapping_when_resolved (snat_main_t * sm,
216 ip4_address_t l_addr,
221 snat_protocol_t proto,
225 snat_static_map_resolve_t *rp;
227 vec_add2 (sm->to_resolve, rp, 1);
228 rp->l_addr.as_u32 = l_addr.as_u32;
230 rp->sw_if_index = sw_if_index;
234 rp->addr_only = addr_only;
239 * @brief Add static mapping.
241 * Create static mapping between local addr+port and external addr+port.
243 * @param l_addr Local IPv4 address.
244 * @param e_addr External IPv4 address.
245 * @param l_port Local port number.
246 * @param e_port External port number.
247 * @param vrf_id VRF ID.
248 * @param addr_only If 0 address port and pair mapping, otherwise address only.
249 * @param sw_if_index External port instead of specific IP address.
250 * @param is_add If 0 delete static mapping, otherwise add.
254 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
255 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
256 u32 sw_if_index, snat_protocol_t proto, int is_add)
258 snat_main_t * sm = &snat_main;
259 snat_static_mapping_t *m;
260 snat_session_key_t m_key;
261 clib_bihash_kv_8_8_t kv, value;
262 snat_address_t *a = 0;
265 snat_interface_t *interface;
268 /* If the external address is a specific interface address */
269 if (sw_if_index != ~0)
271 ip4_address_t * first_int_addr;
273 /* Might be already set... */
274 first_int_addr = ip4_interface_first_address
275 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
277 /* DHCP resolution required? */
278 if (first_int_addr == 0)
280 snat_add_static_mapping_when_resolved
281 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
286 e_addr.as_u32 = first_int_addr->as_u32;
290 m_key.port = addr_only ? 0 : e_port;
291 m_key.protocol = addr_only ? 0 : proto;
292 m_key.fib_index = sm->outside_fib_index;
293 kv.key = m_key.as_u64;
294 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
297 m = pool_elt_at_index (sm->static_mappings, value.value);
302 return VNET_API_ERROR_VALUE_EXIST;
304 /* Convert VRF id to FIB index */
307 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
309 return VNET_API_ERROR_NO_SUCH_FIB;
312 /* If not specified use inside VRF id from SNAT plugin startup config */
315 fib_index = sm->inside_fib_index;
316 vrf_id = sm->inside_vrf_id;
319 /* Find external address in allocated addresses and reserve port for
320 address and port pair mapping when dynamic translations enabled */
321 if (!addr_only && !(sm->static_mapping_only))
323 for (i = 0; i < vec_len (sm->addresses); i++)
325 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
327 a = sm->addresses + i;
328 /* External port must be unused */
331 #define _(N, j, n, s) \
332 case SNAT_PROTOCOL_##N: \
333 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
334 return VNET_API_ERROR_INVALID_VALUE; \
335 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
337 a->busy_##n##_ports++; \
339 foreach_snat_protocol
342 clib_warning("unknown_protocol");
343 return VNET_API_ERROR_INVALID_VALUE_2;
348 /* External address must be allocated */
350 return VNET_API_ERROR_NO_SUCH_ENTRY;
353 pool_get (sm->static_mappings, m);
354 memset (m, 0, sizeof (*m));
355 m->local_addr = l_addr;
356 m->external_addr = e_addr;
357 m->addr_only = addr_only;
359 m->fib_index = fib_index;
362 m->local_port = l_port;
363 m->external_port = e_port;
367 m_key.addr = m->local_addr;
368 m_key.port = m->local_port;
369 m_key.protocol = m->proto;
370 m_key.fib_index = m->fib_index;
371 kv.key = m_key.as_u64;
372 kv.value = m - sm->static_mappings;
373 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
375 m_key.addr = m->external_addr;
376 m_key.port = m->external_port;
377 m_key.fib_index = sm->outside_fib_index;
378 kv.key = m_key.as_u64;
379 kv.value = m - sm->static_mappings;
380 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
385 snat_user_key_t w_key0;
386 snat_worker_key_t w_key1;
388 w_key0.addr = m->local_addr;
389 w_key0.fib_index = m->fib_index;
390 kv.key = w_key0.as_u64;
392 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
394 kv.value = sm->first_worker_index +
395 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
397 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
401 kv.value = value.value;
404 w_key1.addr = m->external_addr;
405 w_key1.port = clib_host_to_net_u16 (m->external_port);
406 w_key1.fib_index = sm->outside_fib_index;
407 kv.key = w_key1.as_u64;
408 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
414 return VNET_API_ERROR_NO_SUCH_ENTRY;
416 /* Free external address port */
417 if (!addr_only && !(sm->static_mapping_only))
419 for (i = 0; i < vec_len (sm->addresses); i++)
421 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
423 a = sm->addresses + i;
426 #define _(N, j, n, s) \
427 case SNAT_PROTOCOL_##N: \
428 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
430 a->busy_##n##_ports--; \
432 foreach_snat_protocol
435 clib_warning("unknown_protocol");
436 return VNET_API_ERROR_INVALID_VALUE_2;
443 m_key.addr = m->local_addr;
444 m_key.port = m->local_port;
445 m_key.protocol = m->proto;
446 m_key.fib_index = m->fib_index;
447 kv.key = m_key.as_u64;
448 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
450 m_key.addr = m->external_addr;
451 m_key.port = m->external_port;
452 m_key.fib_index = sm->outside_fib_index;
453 kv.key = m_key.as_u64;
454 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
456 /* Delete session(s) for static mapping if exist */
457 if (!(sm->static_mapping_only) ||
458 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
460 snat_user_key_t u_key;
462 dlist_elt_t * head, * elt;
463 u32 elt_index, head_index, del_elt_index;
467 snat_main_per_thread_data_t *tsm;
469 u_key.addr = m->local_addr;
470 u_key.fib_index = m->fib_index;
471 kv.key = u_key.as_u64;
472 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
474 user_index = value.value;
475 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
476 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
478 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
479 u = pool_elt_at_index (tsm->users, user_index);
480 if (u->nstaticsessions)
482 head_index = u->sessions_per_user_list_head_index;
483 head = pool_elt_at_index (tsm->list_pool, head_index);
484 elt_index = head->next;
485 elt = pool_elt_at_index (tsm->list_pool, elt_index);
486 ses_index = elt->value;
487 while (ses_index != ~0)
489 s = pool_elt_at_index (tsm->sessions, ses_index);
490 del_elt_index = elt_index;
491 elt_index = elt->next;
492 elt = pool_elt_at_index (tsm->list_pool, elt_index);
493 ses_index = elt->value;
497 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
498 (clib_net_to_host_u16 (s->out2in.port) != e_port))
503 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
504 s->out2in.addr.as_u32,
508 s->in2out.fib_index);
510 value.key = s->in2out.as_u64;
511 clib_bihash_add_del_8_8 (&sm->in2out, &value, 0);
512 value.key = s->out2in.as_u64;
513 clib_bihash_add_del_8_8 (&sm->out2in, &value, 0);
514 pool_put (tsm->sessions, s);
516 clib_dlist_remove (tsm->list_pool, del_elt_index);
517 pool_put_index (tsm->list_pool, del_elt_index);
518 u->nstaticsessions--;
525 pool_put (tsm->users, u);
526 clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
532 /* Delete static mapping from pool */
533 pool_put (sm->static_mappings, m);
539 /* Add/delete external address to FIB */
540 pool_foreach (interface, sm->interfaces,
542 if (interface->is_inside)
545 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
552 int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
554 snat_address_t *a = 0;
556 u32 *ses_to_be_removed = 0, *ses_index;
557 clib_bihash_kv_8_8_t kv, value;
558 snat_user_key_t user_key;
560 snat_main_per_thread_data_t *tsm;
561 snat_static_mapping_t *m;
562 snat_interface_t *interface;
565 /* Find SNAT address */
566 for (i=0; i < vec_len (sm->addresses); i++)
568 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
570 a = sm->addresses + i;
575 return VNET_API_ERROR_NO_SUCH_ENTRY;
579 pool_foreach (m, sm->static_mappings,
581 if (m->external_addr.as_u32 == addr.as_u32)
582 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
583 m->local_port, m->external_port,
584 m->vrf_id, m->addr_only, ~0,
590 /* Check if address is used in some static mapping */
591 if (is_snat_address_used_in_static_mapping(sm, addr))
593 clib_warning ("address used in static mapping");
594 return VNET_API_ERROR_UNSPECIFIED;
598 /* Delete sessions using address */
599 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
601 vec_foreach (tsm, sm->per_thread_data)
603 pool_foreach (ses, tsm->sessions, ({
604 if (ses->out2in.addr.as_u32 == addr.as_u32)
607 snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
608 ses->out2in.addr.as_u32,
609 ses->in2out.protocol,
612 ses->in2out.fib_index);
613 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
614 kv.key = ses->in2out.as_u64;
615 clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
616 kv.key = ses->out2in.as_u64;
617 clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
618 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
619 user_key.addr = ses->in2out.addr;
620 user_key.fib_index = ses->in2out.fib_index;
621 kv.key = user_key.as_u64;
622 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
624 u = pool_elt_at_index (tsm->users, value.value);
630 vec_foreach (ses_index, ses_to_be_removed)
631 pool_put_index (tsm->sessions, ses_index[0]);
633 vec_free (ses_to_be_removed);
637 vec_del1 (sm->addresses, i);
639 /* Delete external address from FIB */
640 pool_foreach (interface, sm->interfaces,
642 if (interface->is_inside)
645 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
652 static int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
654 snat_main_t *sm = &snat_main;
656 const char * feature_name;
658 snat_static_mapping_t * m;
661 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
662 feature_name = is_inside ? "snat-in2out-fast" : "snat-out2in-fast";
665 if (sm->num_workers > 1 && !sm->deterministic)
666 feature_name = is_inside ? "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
667 else if (sm->deterministic)
668 feature_name = is_inside ? "snat-det-in2out" : "snat-det-out2in";
670 feature_name = is_inside ? "snat-in2out" : "snat-out2in";
673 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
676 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
677 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
679 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
680 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
682 pool_foreach (i, sm->interfaces,
684 if (i->sw_if_index == sw_if_index)
687 pool_put (sm->interfaces, i);
689 return VNET_API_ERROR_VALUE_EXIST;
696 return VNET_API_ERROR_NO_SUCH_ENTRY;
698 pool_get (sm->interfaces, i);
699 i->sw_if_index = sw_if_index;
700 i->is_inside = is_inside;
702 /* Add/delete external addresses to FIB */
707 vec_foreach (ap, sm->addresses)
708 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
710 pool_foreach (m, sm->static_mappings,
715 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
718 pool_foreach (dm, sm->det_maps,
720 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
726 static int snat_set_workers (uword * bitmap)
728 snat_main_t *sm = &snat_main;
731 if (sm->num_workers < 2)
732 return VNET_API_ERROR_FEATURE_DISABLED;
734 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
735 return VNET_API_ERROR_INVALID_WORKER;
737 vec_free (sm->workers);
738 clib_bitmap_foreach (i, bitmap,
740 vec_add1(sm->workers, i);
747 vl_api_snat_add_address_range_t_handler
748 (vl_api_snat_add_address_range_t * mp)
750 snat_main_t * sm = &snat_main;
751 vl_api_snat_add_address_range_reply_t * rmp;
752 ip4_address_t this_addr;
753 u32 start_host_order, end_host_order;
761 rv = VNET_API_ERROR_UNIMPLEMENTED;
765 if (sm->static_mapping_only)
767 rv = VNET_API_ERROR_FEATURE_DISABLED;
771 tmp = (u32 *) mp->first_ip_address;
772 start_host_order = clib_host_to_net_u32 (tmp[0]);
773 tmp = (u32 *) mp->last_ip_address;
774 end_host_order = clib_host_to_net_u32 (tmp[0]);
776 count = (end_host_order - start_host_order) + 1;
778 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
781 clib_warning ("%U - %U, %d addresses...",
782 format_ip4_address, mp->first_ip_address,
783 format_ip4_address, mp->last_ip_address,
786 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
788 for (i = 0; i < count; i++)
791 snat_add_address (sm, &this_addr, vrf_id);
793 rv = snat_del_address (sm, this_addr, 0);
798 increment_v4_address (&this_addr);
802 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
805 static void *vl_api_snat_add_address_range_t_print
806 (vl_api_snat_add_address_range_t *mp, void * handle)
810 s = format (0, "SCRIPT: snat_add_address_range ");
811 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
812 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
814 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
820 send_snat_address_details
821 (snat_address_t * a, unix_shared_memory_queue_t * q, u32 context)
823 vl_api_snat_address_details_t *rmp;
824 snat_main_t * sm = &snat_main;
826 rmp = vl_msg_api_alloc (sizeof (*rmp));
827 memset (rmp, 0, sizeof (*rmp));
828 rmp->_vl_msg_id = ntohs (VL_API_SNAT_ADDRESS_DETAILS+sm->msg_id_base);
830 clib_memcpy (rmp->ip_address, &(a->addr), 4);
831 if (a->fib_index != ~0)
832 rmp->vrf_id = ntohl(ip4_fib_get(a->fib_index)->table_id);
835 rmp->context = context;
837 vl_msg_api_send_shmem (q, (u8 *) & rmp);
841 vl_api_snat_address_dump_t_handler
842 (vl_api_snat_address_dump_t * mp)
844 unix_shared_memory_queue_t *q;
845 snat_main_t * sm = &snat_main;
848 q = vl_api_client_index_to_input_queue (mp->client_index);
852 vec_foreach (a, sm->addresses)
853 send_snat_address_details (a, q, mp->context);
856 static void *vl_api_snat_address_dump_t_print
857 (vl_api_snat_address_dump_t *mp, void * handle)
861 s = format (0, "SCRIPT: snat_address_dump ");
867 vl_api_snat_interface_add_del_feature_t_handler
868 (vl_api_snat_interface_add_del_feature_t * mp)
870 snat_main_t * sm = &snat_main;
871 vl_api_snat_interface_add_del_feature_reply_t * rmp;
872 u8 is_del = mp->is_add == 0;
873 u32 sw_if_index = ntohl(mp->sw_if_index);
876 VALIDATE_SW_IF_INDEX(mp);
878 rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
880 BAD_SW_IF_INDEX_LABEL;
882 REPLY_MACRO(VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
885 static void *vl_api_snat_interface_add_del_feature_t_print
886 (vl_api_snat_interface_add_del_feature_t * mp, void *handle)
890 s = format (0, "SCRIPT: snat_interface_add_del_feature ");
891 s = format (s, "sw_if_index %d %s %s",
892 clib_host_to_net_u32(mp->sw_if_index),
893 mp->is_inside ? "in":"out",
894 mp->is_add ? "" : "del");
900 send_snat_interface_details
901 (snat_interface_t * i, unix_shared_memory_queue_t * q, u32 context)
903 vl_api_snat_interface_details_t *rmp;
904 snat_main_t * sm = &snat_main;
906 rmp = vl_msg_api_alloc (sizeof (*rmp));
907 memset (rmp, 0, sizeof (*rmp));
908 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_DETAILS+sm->msg_id_base);
909 rmp->sw_if_index = ntohl (i->sw_if_index);
910 rmp->is_inside = i->is_inside;
911 rmp->context = context;
913 vl_msg_api_send_shmem (q, (u8 *) & rmp);
917 vl_api_snat_interface_dump_t_handler
918 (vl_api_snat_interface_dump_t * mp)
920 unix_shared_memory_queue_t *q;
921 snat_main_t * sm = &snat_main;
922 snat_interface_t * i;
924 q = vl_api_client_index_to_input_queue (mp->client_index);
928 pool_foreach (i, sm->interfaces,
930 send_snat_interface_details(i, q, mp->context);
934 static void *vl_api_snat_interface_dump_t_print
935 (vl_api_snat_interface_dump_t *mp, void * handle)
939 s = format (0, "SCRIPT: snat_interface_dump ");
944 vl_api_snat_add_static_mapping_t_handler
945 (vl_api_snat_add_static_mapping_t * mp)
947 snat_main_t * sm = &snat_main;
948 vl_api_snat_add_static_mapping_reply_t * rmp;
949 ip4_address_t local_addr, external_addr;
950 u16 local_port = 0, external_port = 0;
951 u32 vrf_id, external_sw_if_index;
953 snat_protocol_t proto;
957 rv = VNET_API_ERROR_UNIMPLEMENTED;
961 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
962 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
963 if (mp->addr_only == 0)
965 local_port = clib_net_to_host_u16 (mp->local_port);
966 external_port = clib_net_to_host_u16 (mp->external_port);
968 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
969 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
970 proto = ip_proto_to_snat_proto (mp->protocol);
972 rv = snat_add_static_mapping(local_addr, external_addr, local_port,
973 external_port, vrf_id, mp->addr_only,
974 external_sw_if_index, proto, mp->is_add);
977 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
980 static void *vl_api_snat_add_static_mapping_t_print
981 (vl_api_snat_add_static_mapping_t *mp, void * handle)
985 s = format (0, "SCRIPT: snat_add_static_mapping ");
986 s = format (s, "protocol %d local_addr %U external_addr %U ",
988 format_ip4_address, mp->local_ip_address,
989 format_ip4_address, mp->external_ip_address);
991 if (mp->addr_only == 0)
992 s = format (s, "local_port %d external_port %d ",
993 clib_net_to_host_u16 (mp->local_port),
994 clib_net_to_host_u16 (mp->external_port));
996 if (mp->vrf_id != ~0)
997 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
999 if (mp->external_sw_if_index != ~0)
1000 s = format (s, "external_sw_if_index %d",
1001 clib_net_to_host_u32 (mp->external_sw_if_index));
1006 send_snat_static_mapping_details
1007 (snat_static_mapping_t * m, unix_shared_memory_queue_t * q, u32 context)
1009 vl_api_snat_static_mapping_details_t *rmp;
1010 snat_main_t * sm = &snat_main;
1012 rmp = vl_msg_api_alloc (sizeof (*rmp));
1013 memset (rmp, 0, sizeof (*rmp));
1014 rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
1016 rmp->addr_only = m->addr_only;
1017 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
1018 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
1019 rmp->local_port = htons (m->local_port);
1020 rmp->external_port = htons (m->external_port);
1021 rmp->external_sw_if_index = ~0;
1022 rmp->vrf_id = htonl (m->vrf_id);
1023 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1024 rmp->context = context;
1026 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1030 send_snat_static_map_resolve_details
1031 (snat_static_map_resolve_t * m, unix_shared_memory_queue_t * q, u32 context)
1033 vl_api_snat_static_mapping_details_t *rmp;
1034 snat_main_t * sm = &snat_main;
1036 rmp = vl_msg_api_alloc (sizeof (*rmp));
1037 memset (rmp, 0, sizeof (*rmp));
1038 rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
1040 rmp->addr_only = m->addr_only;
1041 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
1042 rmp->local_port = htons (m->l_port);
1043 rmp->external_port = htons (m->e_port);
1044 rmp->external_sw_if_index = htonl (m->sw_if_index);
1045 rmp->vrf_id = htonl (m->vrf_id);
1046 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1047 rmp->context = context;
1049 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1053 vl_api_snat_static_mapping_dump_t_handler
1054 (vl_api_snat_static_mapping_dump_t * mp)
1056 unix_shared_memory_queue_t *q;
1057 snat_main_t * sm = &snat_main;
1058 snat_static_mapping_t * m;
1059 snat_static_map_resolve_t *rp;
1062 q = vl_api_client_index_to_input_queue (mp->client_index);
1066 pool_foreach (m, sm->static_mappings,
1068 send_snat_static_mapping_details (m, q, mp->context);
1071 for (j = 0; j < vec_len (sm->to_resolve); j++)
1073 rp = sm->to_resolve + j;
1074 send_snat_static_map_resolve_details (rp, q, mp->context);
1078 static void *vl_api_snat_static_mapping_dump_t_print
1079 (vl_api_snat_static_mapping_dump_t *mp, void * handle)
1083 s = format (0, "SCRIPT: snat_static_mapping_dump ");
1089 vl_api_snat_control_ping_t_handler
1090 (vl_api_snat_control_ping_t * mp)
1092 vl_api_snat_control_ping_reply_t *rmp;
1093 snat_main_t * sm = &snat_main;
1096 REPLY_MACRO2(VL_API_SNAT_CONTROL_PING_REPLY,
1098 rmp->vpe_pid = ntohl (getpid());
1102 static void *vl_api_snat_control_ping_t_print
1103 (vl_api_snat_control_ping_t *mp, void * handle)
1107 s = format (0, "SCRIPT: snat_control_ping ");
1113 vl_api_snat_show_config_t_handler
1114 (vl_api_snat_show_config_t * mp)
1116 vl_api_snat_show_config_reply_t *rmp;
1117 snat_main_t * sm = &snat_main;
1120 REPLY_MACRO2(VL_API_SNAT_SHOW_CONFIG_REPLY,
1122 rmp->translation_buckets = htonl (sm->translation_buckets);
1123 rmp->translation_memory_size = htonl (sm->translation_memory_size);
1124 rmp->user_buckets = htonl (sm->user_buckets);
1125 rmp->user_memory_size = htonl (sm->user_memory_size);
1126 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
1127 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
1128 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
1129 rmp->static_mapping_only = sm->static_mapping_only;
1130 rmp->static_mapping_connection_tracking =
1131 sm->static_mapping_connection_tracking;
1132 rmp->deterministic = sm->deterministic;
1136 static void *vl_api_snat_show_config_t_print
1137 (vl_api_snat_show_config_t *mp, void * handle)
1141 s = format (0, "SCRIPT: snat_show_config ");
1147 vl_api_snat_set_workers_t_handler
1148 (vl_api_snat_set_workers_t * mp)
1150 snat_main_t * sm = &snat_main;
1151 vl_api_snat_set_workers_reply_t * rmp;
1154 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1156 if (sm->num_workers < 2)
1158 rv = VNET_API_ERROR_FEATURE_DISABLED;
1162 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
1163 rv = snat_set_workers(bitmap);
1164 clib_bitmap_free (bitmap);
1167 REPLY_MACRO (VL_API_SNAT_SET_WORKERS_REPLY);
1170 static void *vl_api_snat_set_workers_t_print
1171 (vl_api_snat_set_workers_t *mp, void * handle)
1177 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
1179 s = format (0, "SCRIPT: snat_set_workers ");
1180 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
1181 clib_bitmap_foreach (i, bitmap,
1184 s = format (s, "%d", i);
1186 s = format (s, ",%d", i);
1189 clib_bitmap_free (bitmap);
1194 send_snat_worker_details
1195 (u32 worker_index, unix_shared_memory_queue_t * q, u32 context)
1197 vl_api_snat_worker_details_t *rmp;
1198 snat_main_t * sm = &snat_main;
1199 vlib_worker_thread_t *w =
1200 vlib_worker_threads + worker_index + sm->first_worker_index;
1202 rmp = vl_msg_api_alloc (sizeof (*rmp));
1203 memset (rmp, 0, sizeof (*rmp));
1204 rmp->_vl_msg_id = ntohs (VL_API_SNAT_WORKER_DETAILS+sm->msg_id_base);
1205 rmp->context = context;
1206 rmp->worker_index = htonl (worker_index);
1207 rmp->lcore_id = htonl (w->lcore_id);
1208 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
1210 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1214 vl_api_snat_worker_dump_t_handler
1215 (vl_api_snat_worker_dump_t * mp)
1217 unix_shared_memory_queue_t *q;
1218 snat_main_t * sm = &snat_main;
1221 q = vl_api_client_index_to_input_queue (mp->client_index);
1225 vec_foreach (worker_index, sm->workers)
1227 send_snat_worker_details(*worker_index, q, mp->context);
1231 static void *vl_api_snat_worker_dump_t_print
1232 (vl_api_snat_worker_dump_t *mp, void * handle)
1236 s = format (0, "SCRIPT: snat_worker_dump ");
1241 static int snat_add_interface_address(snat_main_t *sm,
1246 vl_api_snat_add_del_interface_addr_t_handler
1247 (vl_api_snat_add_del_interface_addr_t * mp)
1249 snat_main_t * sm = &snat_main;
1250 vl_api_snat_add_del_interface_addr_reply_t * rmp;
1251 u8 is_del = mp->is_add == 0;
1252 u32 sw_if_index = ntohl(mp->sw_if_index);
1255 VALIDATE_SW_IF_INDEX(mp);
1257 rv = snat_add_interface_address (sm, sw_if_index, is_del);
1259 BAD_SW_IF_INDEX_LABEL;
1261 REPLY_MACRO(VL_API_SNAT_ADD_DEL_INTERFACE_ADDR_REPLY);
1264 static void *vl_api_snat_add_del_interface_addr_t_print
1265 (vl_api_snat_add_del_interface_addr_t * mp, void *handle)
1269 s = format (0, "SCRIPT: snat_add_del_interface_addr ");
1270 s = format (s, "sw_if_index %d %s",
1271 clib_host_to_net_u32(mp->sw_if_index),
1272 mp->is_add ? "" : "del");
1278 send_snat_interface_addr_details
1279 (u32 sw_if_index, unix_shared_memory_queue_t * q, u32 context)
1281 vl_api_snat_interface_addr_details_t *rmp;
1282 snat_main_t * sm = &snat_main;
1284 rmp = vl_msg_api_alloc (sizeof (*rmp));
1285 memset (rmp, 0, sizeof (*rmp));
1286 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_ADDR_DETAILS+sm->msg_id_base);
1287 rmp->sw_if_index = ntohl (sw_if_index);
1288 rmp->context = context;
1290 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1294 vl_api_snat_interface_addr_dump_t_handler
1295 (vl_api_snat_interface_addr_dump_t * mp)
1297 unix_shared_memory_queue_t *q;
1298 snat_main_t * sm = &snat_main;
1301 q = vl_api_client_index_to_input_queue (mp->client_index);
1305 vec_foreach (i, sm->auto_add_sw_if_indices)
1306 send_snat_interface_addr_details(*i, q, mp->context);
1309 static void *vl_api_snat_interface_addr_dump_t_print
1310 (vl_api_snat_interface_addr_dump_t *mp, void * handle)
1314 s = format (0, "SCRIPT: snat_interface_addr_dump ");
1320 vl_api_snat_ipfix_enable_disable_t_handler
1321 (vl_api_snat_ipfix_enable_disable_t * mp)
1323 snat_main_t * sm = &snat_main;
1324 vl_api_snat_ipfix_enable_disable_reply_t * rmp;
1327 rv = snat_ipfix_logging_enable_disable(mp->enable,
1328 clib_host_to_net_u32 (mp->domain_id),
1329 clib_host_to_net_u16 (mp->src_port));
1331 REPLY_MACRO (VL_API_SNAT_IPFIX_ENABLE_DISABLE_REPLY);
1334 static void *vl_api_snat_ipfix_enable_disable_t_print
1335 (vl_api_snat_ipfix_enable_disable_t *mp, void * handle)
1339 s = format (0, "SCRIPT: snat_ipfix_enable_disable ");
1341 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
1343 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
1345 s = format (s, "disable ");
1351 send_snat_user_details
1352 (snat_user_t * u, unix_shared_memory_queue_t * q, u32 context)
1354 vl_api_snat_user_details_t * rmp;
1355 snat_main_t * sm = &snat_main;
1356 ip4_fib_t * fib_table;
1358 rmp = vl_msg_api_alloc (sizeof (*rmp));
1359 memset (rmp, 0, sizeof (*rmp));
1360 rmp->_vl_msg_id = ntohs (VL_API_SNAT_USER_DETAILS+sm->msg_id_base);
1362 fib_table = ip4_fib_get(u->fib_index);
1363 rmp->vrf_id = ntohl (fib_table->table_id);
1366 clib_memcpy(rmp->ip_address, &(u->addr), 4);
1367 rmp->nsessions = ntohl (u->nsessions);
1368 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1369 rmp->context = context;
1371 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1375 vl_api_snat_user_dump_t_handler
1376 (vl_api_snat_user_dump_t * mp)
1378 unix_shared_memory_queue_t *q;
1379 snat_main_t * sm = &snat_main;
1380 snat_main_per_thread_data_t * tsm;
1383 q = vl_api_client_index_to_input_queue (mp->client_index);
1387 vec_foreach (tsm, sm->per_thread_data)
1388 vec_foreach (u, tsm->users)
1389 send_snat_user_details (u, q, mp->context);
1392 static void *vl_api_snat_user_dump_t_print
1393 (vl_api_snat_user_dump_t *mp, void * handle)
1397 s = format (0, "SCRIPT: snat_user_dump ");
1403 send_snat_user_session_details
1404 (snat_session_t * s, unix_shared_memory_queue_t * q, u32 context)
1406 vl_api_snat_user_session_details_t * rmp;
1407 snat_main_t * sm = &snat_main;
1409 rmp = vl_msg_api_alloc (sizeof(*rmp));
1410 memset (rmp, 0, sizeof (*rmp));
1411 rmp->_vl_msg_id = ntohs (VL_API_SNAT_USER_SESSION_DETAILS+sm->msg_id_base);
1413 clib_memcpy(rmp->outside_ip_address, (&s->out2in.addr), 4);
1414 rmp->outside_port = s->out2in.port;
1415 clib_memcpy(rmp->inside_ip_address, (&s->in2out.addr), 4);
1416 rmp->inside_port = s->in2out.port;
1417 rmp->protocol = ntohs(snat_proto_to_ip_proto(s->in2out.protocol));
1418 rmp->is_static = s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING ? 1 : 0;
1419 rmp->last_heard = clib_host_to_net_u64((u64)s->last_heard);
1420 rmp->total_bytes = clib_host_to_net_u64(s->total_bytes);
1421 rmp->total_pkts = ntohl(s->total_pkts);
1422 rmp->context = context;
1424 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1428 vl_api_snat_user_session_dump_t_handler
1429 (vl_api_snat_user_session_dump_t * mp)
1431 unix_shared_memory_queue_t *q;
1432 snat_main_t * sm = &snat_main;
1433 snat_main_per_thread_data_t *tsm;
1435 clib_bihash_kv_8_8_t key, value;
1436 snat_user_key_t ukey;
1438 u32 session_index, head_index, elt_index;
1439 dlist_elt_t * head, * elt;
1441 q = vl_api_client_index_to_input_queue (mp->client_index);
1447 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1448 ukey.fib_index = ip4_fib_index_from_table_id (ntohl(mp->vrf_id));
1449 key.key = ukey.as_u64;
1450 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &key, &value))
1451 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
1453 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1454 if (clib_bihash_search_8_8 (&sm->user_hash, &key, &value))
1456 u = pool_elt_at_index (tsm->users, value.value);
1457 if (!u->nsessions && !u->nstaticsessions)
1460 head_index = u->sessions_per_user_list_head_index;
1461 head = pool_elt_at_index (tsm->list_pool, head_index);
1462 elt_index = head->next;
1463 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1464 session_index = elt->value;
1465 while (session_index != ~0)
1467 s = pool_elt_at_index (tsm->sessions, session_index);
1469 send_snat_user_session_details (s, q, mp->context);
1471 elt_index = elt->next;
1472 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1473 session_index = elt->value;
1477 static void *vl_api_snat_user_session_dump_t_print
1478 (vl_api_snat_user_session_dump_t *mp, void * handle)
1482 s = format (0, "SCRIPT: snat_user_session_dump ");
1483 s = format (s, "ip_address %U vrf_id %d\n",
1484 format_ip4_address, mp->ip_address,
1485 clib_net_to_host_u32 (mp->vrf_id));
1491 vl_api_snat_add_det_map_t_handler
1492 (vl_api_snat_add_det_map_t * mp)
1494 snat_main_t * sm = &snat_main;
1495 vl_api_snat_add_det_map_reply_t * rmp;
1497 ip4_address_t in_addr, out_addr;
1499 clib_memcpy(&in_addr, mp->in_addr, 4);
1500 clib_memcpy(&out_addr, mp->out_addr, 4);
1501 rv = snat_det_add_map(sm, &in_addr, mp->in_plen, &out_addr,
1502 mp->out_plen, mp->is_add);
1504 REPLY_MACRO (VL_API_SNAT_ADD_DET_MAP_REPLY);
1507 static void *vl_api_snat_add_det_map_t_print
1508 (vl_api_snat_add_det_map_t *mp, void * handle)
1512 s = format (0, "SCRIPT: snat_add_det_map ");
1513 s = format (s, "inside address %U/%d outside address %U/%d\n",
1514 format_ip4_address, mp->in_addr, mp->in_plen,
1515 format_ip4_address, mp->out_addr, mp->out_plen);
1521 vl_api_snat_det_forward_t_handler
1522 (vl_api_snat_det_forward_t * mp)
1524 snat_main_t * sm = &snat_main;
1525 vl_api_snat_det_forward_reply_t * rmp;
1527 u16 lo_port = 0, hi_port = 0;
1528 snat_det_map_t * dm;
1529 ip4_address_t in_addr, out_addr;
1531 out_addr.as_u32 = 0;
1532 clib_memcpy(&in_addr, mp->in_addr, 4);
1533 dm = snat_det_map_by_user(sm, &in_addr);
1536 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1540 snat_det_forward(dm, &in_addr, &out_addr, &lo_port);
1541 hi_port = lo_port + dm->ports_per_host - 1;
1544 REPLY_MACRO2(VL_API_SNAT_DET_FORWARD_REPLY,
1546 rmp->out_port_lo = ntohs(lo_port);
1547 rmp->out_port_hi = ntohs(hi_port);
1549 memset(rmp->out_addr, 0, 16);
1550 clib_memcpy(rmp->out_addr, &out_addr, 4);
1554 static void *vl_api_snat_det_forward_t_print
1555 (vl_api_snat_det_forward_t * mp, void * handle)
1559 s = format (0, "SCRIPT: smat_det_forward_t");
1560 s = format (s, "inside ip address %U\n",
1561 format_ip4_address, mp->in_addr);
1567 vl_api_snat_det_reverse_t_handler
1568 (vl_api_snat_det_reverse_t * mp)
1570 snat_main_t * sm = &snat_main;
1571 vl_api_snat_det_reverse_reply_t * rmp;
1573 ip4_address_t out_addr, in_addr;
1574 snat_det_map_t * dm;
1577 clib_memcpy(&out_addr, mp->out_addr, 4);
1578 dm = snat_det_map_by_out(sm, &out_addr);
1581 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1585 snat_det_reverse(dm, &out_addr, htons(mp->out_port), &in_addr);
1588 REPLY_MACRO2(VL_API_SNAT_DET_REVERSE_REPLY,
1591 memset(rmp->in_addr, 0, 16);
1592 clib_memcpy(rmp->in_addr, &in_addr, 4);
1596 static void *vl_api_snat_det_reverse_t_print
1597 (vl_api_snat_det_reverse_t * mp, void * handle)
1601 s = format(0, "SCRIPT: smat_det_reverse_t");
1602 s = format(s, "outside ip address %U outside port %d",
1603 format_ip4_address, mp->out_addr, ntohs(mp->out_port));
1609 sent_snat_det_map_details
1610 (snat_det_map_t * m, unix_shared_memory_queue_t * q, u32 context)
1612 vl_api_snat_det_map_details_t *rmp;
1613 snat_main_t * sm = &snat_main;
1615 rmp = vl_msg_api_alloc (sizeof (*rmp));
1616 memset (rmp, 0, sizeof (*rmp));
1617 rmp->_vl_msg_id = ntohs (VL_API_SNAT_DET_MAP_DETAILS+sm->msg_id_base);
1619 clib_memcpy (rmp->in_addr, &m->in_addr, 4);
1620 rmp->in_plen = m->in_plen;
1621 clib_memcpy (rmp->out_addr, &m->out_addr, 4);
1622 rmp->out_plen = m->out_plen;
1623 rmp->sharing_ratio = htonl (m->sharing_ratio);
1624 rmp->ports_per_host = htons (m->ports_per_host);
1625 rmp->ses_num = htonl (m->ses_num);
1626 rmp->context = context;
1628 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1632 vl_api_snat_det_map_dump_t_handler
1633 (vl_api_snat_det_map_dump_t * mp)
1635 unix_shared_memory_queue_t *q;
1636 snat_main_t * sm = &snat_main;
1639 q = vl_api_client_index_to_input_queue (mp->client_index);
1643 vec_foreach(m, sm->det_maps)
1644 sent_snat_det_map_details(m, q, mp->context);
1647 static void * vl_api_snat_det_map_dump_t_print
1648 (vl_api_snat_det_map_dump_t *mp, void * handle)
1652 s = format (0, "SCRIPT: snat_det_map_dump ");
1658 vl_api_snat_det_set_timeouts_t_handler
1659 (vl_api_snat_det_set_timeouts_t * mp)
1661 snat_main_t * sm = &snat_main;
1662 vl_api_snat_det_set_timeouts_reply_t * rmp;
1665 sm->udp_timeout = ntohl(mp->udp);
1666 sm->tcp_established_timeout = ntohl(mp->tcp_established);
1667 sm->tcp_transitory_timeout = ntohl(mp->tcp_transitory);
1668 sm->icmp_timeout = ntohl(mp->icmp);
1670 REPLY_MACRO (VL_API_SNAT_DET_SET_TIMEOUTS_REPLY);
1673 static void *vl_api_snat_det_set_timeouts_t_print
1674 (vl_api_snat_det_set_timeouts_t *mp, void * handle)
1678 s = format (0, "SCRIPT: snat_det_set_timeouts ");
1679 s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
1681 ntohl(mp->tcp_established),
1682 ntohl(mp->tcp_transitory),
1689 vl_api_snat_det_get_timeouts_t_handler
1690 (vl_api_snat_det_get_timeouts_t * mp)
1692 snat_main_t * sm = &snat_main;
1693 vl_api_snat_det_get_timeouts_reply_t * rmp;
1696 REPLY_MACRO2(VL_API_SNAT_DET_GET_TIMEOUTS_REPLY,
1698 rmp->udp = htonl(sm->udp_timeout);
1699 rmp->tcp_established = htonl(sm->tcp_established_timeout);
1700 rmp->tcp_transitory = htonl(sm->tcp_transitory_timeout);
1701 rmp->icmp = htonl(sm->icmp_timeout);
1705 static void *vl_api_snat_det_get_timeouts_t_print
1706 (vl_api_snat_det_get_timeouts_t * mp, void * handle)
1710 s = format(0, "SCRIPT: snat_det_get_timeouts");
1716 vl_api_snat_det_close_session_out_t_handler
1717 (vl_api_snat_det_close_session_out_t * mp)
1719 snat_main_t * sm = &snat_main;
1720 vl_api_snat_det_close_session_out_reply_t * rmp;
1721 ip4_address_t out_addr, ext_addr, in_addr;
1722 snat_det_out_key_t key;
1723 snat_det_map_t * dm;
1724 snat_det_session_t * ses;
1727 clib_memcpy(&out_addr, mp->out_addr, 4);
1728 clib_memcpy(&ext_addr, mp->ext_addr, 4);
1730 dm = snat_det_map_by_out(sm, &out_addr);
1733 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1736 snat_det_reverse(dm, &ext_addr, ntohs(mp->out_port), &in_addr);
1737 key.ext_host_addr = ext_addr;
1738 key.ext_host_port = mp->ext_port;
1739 key.out_port = mp->out_port;
1740 ses = snat_det_get_ses_by_out(dm, &in_addr, key.as_u64);
1743 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1746 snat_det_ses_close(dm, ses);
1749 REPLY_MACRO (VL_API_SNAT_DET_CLOSE_SESSION_OUT_REPLY);
1752 static void *vl_api_snat_det_close_session_out_t_print
1753 (vl_api_snat_det_close_session_out_t *mp, void * handle)
1757 s = format (0, "SCRIPT: snat_det_close_session_out ");
1758 s = format (s, "out_addr %U out_port %d "
1759 "ext_addr %U ext_port %d\n",
1760 format_ip4_address, mp->out_addr, ntohs(mp->out_port),
1761 format_ip4_address, mp->ext_addr, ntohs(mp->ext_port));
1767 vl_api_snat_det_close_session_in_t_handler
1768 (vl_api_snat_det_close_session_in_t * mp)
1770 snat_main_t * sm = &snat_main;
1771 vl_api_snat_det_close_session_in_reply_t * rmp;
1772 ip4_address_t in_addr, ext_addr;
1773 snat_det_out_key_t key;
1774 snat_det_map_t * dm;
1775 snat_det_session_t * ses;
1778 clib_memcpy(&in_addr, mp->in_addr, 4);
1779 clib_memcpy(&ext_addr, mp->ext_addr, 4);
1781 dm = snat_det_map_by_user(sm, &in_addr);
1784 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1787 key.ext_host_addr = ext_addr;
1788 key.ext_host_port = mp->ext_port;
1789 ses = snat_det_find_ses_by_in(dm, &in_addr, mp->in_port, key);
1792 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1795 snat_det_ses_close(dm, ses);
1798 REPLY_MACRO (VL_API_SNAT_DET_CLOSE_SESSION_OUT_REPLY);
1801 static void *vl_api_snat_det_close_session_in_t_print
1802 (vl_api_snat_det_close_session_in_t *mp, void * handle)
1805 s = format (0, "SCRIPT: snat_det_close_session_in ");
1806 s = format (s, "in_addr %U in_port %d "
1807 "ext_addr %U ext_port %d\n",
1808 format_ip4_address, mp->in_addr, ntohs(mp->in_port),
1809 format_ip4_address, mp->ext_addr, ntohs(mp->ext_port));
1814 /* List of message types that this plugin understands */
1815 #define foreach_snat_plugin_api_msg \
1816 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range) \
1817 _(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature) \
1818 _(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping) \
1819 _(SNAT_CONTROL_PING, snat_control_ping) \
1820 _(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump) \
1821 _(SNAT_SHOW_CONFIG, snat_show_config) \
1822 _(SNAT_ADDRESS_DUMP, snat_address_dump) \
1823 _(SNAT_INTERFACE_DUMP, snat_interface_dump) \
1824 _(SNAT_SET_WORKERS, snat_set_workers) \
1825 _(SNAT_WORKER_DUMP, snat_worker_dump) \
1826 _(SNAT_ADD_DEL_INTERFACE_ADDR, snat_add_del_interface_addr) \
1827 _(SNAT_INTERFACE_ADDR_DUMP, snat_interface_addr_dump) \
1828 _(SNAT_IPFIX_ENABLE_DISABLE, snat_ipfix_enable_disable) \
1829 _(SNAT_USER_DUMP, snat_user_dump) \
1830 _(SNAT_USER_SESSION_DUMP, snat_user_session_dump) \
1831 _(SNAT_ADD_DET_MAP, snat_add_det_map) \
1832 _(SNAT_DET_FORWARD, snat_det_forward) \
1833 _(SNAT_DET_REVERSE, snat_det_reverse) \
1834 _(SNAT_DET_MAP_DUMP, snat_det_map_dump) \
1835 _(SNAT_DET_SET_TIMEOUTS, snat_det_set_timeouts) \
1836 _(SNAT_DET_GET_TIMEOUTS, snat_det_get_timeouts) \
1837 _(SNAT_DET_CLOSE_SESSION_OUT, snat_det_close_session_out) \
1838 _(SNAT_DET_CLOSE_SESSION_IN, snat_det_close_session_in)
1841 /* Set up the API message handling tables */
1842 static clib_error_t *
1843 snat_plugin_api_hookup (vlib_main_t *vm)
1845 snat_main_t * sm __attribute__ ((unused)) = &snat_main;
1847 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1849 vl_api_##n##_t_handler, \
1851 vl_api_##n##_t_endian, \
1852 vl_api_##n##_t_print, \
1853 sizeof(vl_api_##n##_t), 1);
1854 foreach_snat_plugin_api_msg;
1860 #define vl_msg_name_crc_list
1861 #include <snat/snat_all_api_h.h>
1862 #undef vl_msg_name_crc_list
1865 setup_message_id_table (snat_main_t * sm, api_main_t * am)
1867 #define _(id,n,crc) \
1868 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
1869 foreach_vl_msg_name_crc_snat;
1873 static void plugin_custom_dump_configure (snat_main_t * sm)
1875 #define _(n,f) sm->api_main->msg_print_handlers \
1876 [VL_API_##n + sm->msg_id_base] \
1877 = (void *) vl_api_##f##_t_print;
1878 foreach_snat_plugin_api_msg;
1884 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1887 ip4_address_t * address,
1889 u32 if_address_index,
1892 static clib_error_t * snat_init (vlib_main_t * vm)
1894 snat_main_t * sm = &snat_main;
1895 clib_error_t * error = 0;
1896 ip4_main_t * im = &ip4_main;
1897 ip_lookup_main_t * lm = &im->lookup_main;
1900 vlib_thread_registration_t *tr;
1901 vlib_thread_main_t *tm = vlib_get_thread_main ();
1904 ip4_add_del_interface_address_callback_t cb4;
1906 name = format (0, "snat_%08x%c", api_version, 0);
1908 /* Ask for a correctly-sized block of API message decode slots */
1909 sm->msg_id_base = vl_msg_api_get_msg_ids
1910 ((char *) name, VL_MSG_FIRST_AVAILABLE);
1913 sm->vnet_main = vnet_get_main();
1915 sm->ip4_lookup_main = lm;
1916 sm->api_main = &api_main;
1917 sm->first_worker_index = 0;
1918 sm->next_worker = 0;
1919 sm->num_workers = 0;
1921 sm->fq_in2out_index = ~0;
1922 sm->fq_out2in_index = ~0;
1923 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1924 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1925 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1926 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1928 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1931 tr = (vlib_thread_registration_t *) p[0];
1934 sm->num_workers = tr->count;
1935 sm->first_worker_index = tr->first_index;
1939 /* Use all available workers by default */
1940 if (sm->num_workers > 1)
1942 for (i=0; i < sm->num_workers; i++)
1943 bitmap = clib_bitmap_set (bitmap, i, 1);
1944 snat_set_workers(bitmap);
1945 clib_bitmap_free (bitmap);
1948 error = snat_plugin_api_hookup (vm);
1950 /* Add our API messages to the global name_crc hash table */
1951 setup_message_id_table (sm, &api_main);
1953 plugin_custom_dump_configure (sm);
1956 /* Set up the interface address add/del callback */
1957 cb4.function = snat_ip4_add_del_interface_address_cb;
1958 cb4.function_opaque = 0;
1960 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1962 /* Init IPFIX logging */
1963 snat_ipfix_logging_init(vm);
1968 VLIB_INIT_FUNCTION (snat_init);
1970 void snat_free_outside_address_and_port (snat_main_t * sm,
1971 snat_session_key_t * k,
1975 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1977 ASSERT (address_index < vec_len (sm->addresses));
1979 a = sm->addresses + address_index;
1981 switch (k->protocol)
1983 #define _(N, i, n, s) \
1984 case SNAT_PROTOCOL_##N: \
1985 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1986 port_host_byte_order) == 1); \
1987 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1988 port_host_byte_order, 0); \
1989 a->busy_##n##_ports--; \
1991 foreach_snat_protocol
1994 clib_warning("unknown_protocol");
2000 * @brief Match SNAT static mapping.
2002 * @param sm SNAT main.
2003 * @param match Address and port to match.
2004 * @param mapping External or local address and port of the matched mapping.
2005 * @param by_external If 0 match by local address otherwise match by external
2007 * @param is_addr_only If matched mapping is address only
2009 * @returns 0 if match found otherwise 1.
2011 int snat_static_mapping_match (snat_main_t * sm,
2012 snat_session_key_t match,
2013 snat_session_key_t * mapping,
2017 clib_bihash_kv_8_8_t kv, value;
2018 snat_static_mapping_t *m;
2019 snat_session_key_t m_key;
2020 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
2023 mapping_hash = &sm->static_mapping_by_external;
2025 m_key.addr = match.addr;
2026 m_key.port = clib_net_to_host_u16 (match.port);
2027 m_key.protocol = match.protocol;
2028 m_key.fib_index = match.fib_index;
2030 kv.key = m_key.as_u64;
2032 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2034 /* Try address only mapping */
2037 kv.key = m_key.as_u64;
2038 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2042 m = pool_elt_at_index (sm->static_mappings, value.value);
2046 mapping->addr = m->local_addr;
2047 /* Address only mapping doesn't change port */
2048 mapping->port = m->addr_only ? match.port
2049 : clib_host_to_net_u16 (m->local_port);
2050 mapping->fib_index = m->fib_index;
2054 mapping->addr = m->external_addr;
2055 /* Address only mapping doesn't change port */
2056 mapping->port = m->addr_only ? match.port
2057 : clib_host_to_net_u16 (m->external_port);
2058 mapping->fib_index = sm->outside_fib_index;
2061 if (PREDICT_FALSE(is_addr_only != 0))
2062 *is_addr_only = m->addr_only;
2067 int snat_alloc_outside_address_and_port (snat_main_t * sm,
2069 snat_session_key_t * k,
2070 u32 * address_indexp)
2076 for (i = 0; i < vec_len (sm->addresses); i++)
2078 a = sm->addresses + i;
2079 if (sm->vrf_mode && a->fib_index != ~0 && a->fib_index != fib_index)
2081 switch (k->protocol)
2083 #define _(N, j, n, s) \
2084 case SNAT_PROTOCOL_##N: \
2085 if (a->busy_##n##_ports < (65535-1024)) \
2089 portnum = random_u32 (&sm->random_seed); \
2090 portnum &= 0xFFFF; \
2091 if (portnum < 1024) \
2093 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2095 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2096 a->busy_##n##_ports++; \
2097 k->addr = a->addr; \
2098 k->port = clib_host_to_net_u16(portnum); \
2099 *address_indexp = i; \
2104 foreach_snat_protocol
2107 clib_warning("unknown protocol");
2112 /* Totally out of translations to use... */
2113 snat_ipfix_logging_addresses_exhausted(0);
2118 static clib_error_t *
2119 add_address_command_fn (vlib_main_t * vm,
2120 unformat_input_t * input,
2121 vlib_cli_command_t * cmd)
2123 unformat_input_t _line_input, *line_input = &_line_input;
2124 snat_main_t * sm = &snat_main;
2125 ip4_address_t start_addr, end_addr, this_addr;
2126 u32 start_host_order, end_host_order;
2131 clib_error_t *error = 0;
2133 /* Get a line of input. */
2134 if (!unformat_user (input, unformat_line_input, line_input))
2137 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2139 if (unformat (line_input, "%U - %U",
2140 unformat_ip4_address, &start_addr,
2141 unformat_ip4_address, &end_addr))
2143 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
2145 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
2146 end_addr = start_addr;
2147 else if (unformat (line_input, "del"))
2151 error = clib_error_return (0, "unknown input '%U'",
2152 format_unformat_error, line_input);
2157 if (sm->static_mapping_only)
2159 error = clib_error_return (0, "static mapping only mode");
2163 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
2164 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
2166 if (end_host_order < start_host_order)
2168 error = clib_error_return (0, "end address less than start address");
2172 count = (end_host_order - start_host_order) + 1;
2175 clib_warning ("%U - %U, %d addresses...",
2176 format_ip4_address, &start_addr,
2177 format_ip4_address, &end_addr,
2180 this_addr = start_addr;
2182 for (i = 0; i < count; i++)
2185 snat_add_address (sm, &this_addr, vrf_id);
2187 rv = snat_del_address (sm, this_addr, 0);
2191 case VNET_API_ERROR_NO_SUCH_ENTRY:
2192 error = clib_error_return (0, "S-NAT address not exist.");
2194 case VNET_API_ERROR_UNSPECIFIED:
2195 error = clib_error_return (0, "S-NAT address used in static mapping.");
2201 increment_v4_address (&this_addr);
2205 unformat_free (line_input);
2210 VLIB_CLI_COMMAND (add_address_command, static) = {
2211 .path = "snat add address",
2212 .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] "
2213 "[tenant-vrf <vrf-id>] [del]",
2214 .function = add_address_command_fn,
2217 static clib_error_t *
2218 snat_feature_command_fn (vlib_main_t * vm,
2219 unformat_input_t * input,
2220 vlib_cli_command_t * cmd)
2222 unformat_input_t _line_input, *line_input = &_line_input;
2223 vnet_main_t * vnm = vnet_get_main();
2224 clib_error_t * error = 0;
2226 u32 * inside_sw_if_indices = 0;
2227 u32 * outside_sw_if_indices = 0;
2233 /* Get a line of input. */
2234 if (!unformat_user (input, unformat_line_input, line_input))
2237 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2239 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
2241 vec_add1 (inside_sw_if_indices, sw_if_index);
2242 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
2244 vec_add1 (outside_sw_if_indices, sw_if_index);
2245 else if (unformat (line_input, "del"))
2249 error = clib_error_return (0, "unknown input '%U'",
2250 format_unformat_error, line_input);
2255 if (vec_len (inside_sw_if_indices))
2257 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
2259 sw_if_index = inside_sw_if_indices[i];
2260 snat_interface_add_del (sw_if_index, 1, is_del);
2264 if (vec_len (outside_sw_if_indices))
2266 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
2268 sw_if_index = outside_sw_if_indices[i];
2269 snat_interface_add_del (sw_if_index, 0, is_del);
2274 unformat_free (line_input);
2275 vec_free (inside_sw_if_indices);
2276 vec_free (outside_sw_if_indices);
2281 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
2282 .path = "set interface snat",
2283 .function = snat_feature_command_fn,
2284 .short_help = "set interface snat in <intfc> out <intfc> [del]",
2288 unformat_snat_protocol (unformat_input_t * input, va_list * args)
2290 u32 *r = va_arg (*args, u32 *);
2293 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
2294 foreach_snat_protocol
2302 format_snat_protocol (u8 * s, va_list * args)
2304 u32 i = va_arg (*args, u32);
2309 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
2310 foreach_snat_protocol
2313 s = format (s, "unknown");
2315 s = format (s, "%s", t);
2319 static clib_error_t *
2320 add_static_mapping_command_fn (vlib_main_t * vm,
2321 unformat_input_t * input,
2322 vlib_cli_command_t * cmd)
2324 unformat_input_t _line_input, *line_input = &_line_input;
2325 clib_error_t * error = 0;
2326 ip4_address_t l_addr, e_addr;
2327 u32 l_port = 0, e_port = 0, vrf_id = ~0;
2330 u32 sw_if_index = ~0;
2331 vnet_main_t * vnm = vnet_get_main();
2333 snat_protocol_t proto;
2336 /* Get a line of input. */
2337 if (!unformat_user (input, unformat_line_input, line_input))
2340 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2342 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
2345 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
2347 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
2350 else if (unformat (line_input, "external %U", unformat_ip4_address,
2353 else if (unformat (line_input, "external %U %u",
2354 unformat_vnet_sw_interface, vnm, &sw_if_index,
2358 else if (unformat (line_input, "external %U",
2359 unformat_vnet_sw_interface, vnm, &sw_if_index))
2361 else if (unformat (line_input, "vrf %u", &vrf_id))
2363 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
2365 else if (unformat (line_input, "del"))
2369 error = clib_error_return (0, "unknown input: '%U'",
2370 format_unformat_error, line_input);
2375 if (!addr_only && !proto_set)
2377 error = clib_error_return (0, "missing protocol");
2381 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
2382 vrf_id, addr_only, sw_if_index, proto, is_add);
2386 case VNET_API_ERROR_INVALID_VALUE:
2387 error = clib_error_return (0, "External port already in use.");
2389 case VNET_API_ERROR_NO_SUCH_ENTRY:
2391 error = clib_error_return (0, "External addres must be allocated.");
2393 error = clib_error_return (0, "Mapping not exist.");
2395 case VNET_API_ERROR_NO_SUCH_FIB:
2396 error = clib_error_return (0, "No such VRF id.");
2398 case VNET_API_ERROR_VALUE_EXIST:
2399 error = clib_error_return (0, "Mapping already exist.");
2406 unformat_free (line_input);
2413 * @cliexstart{snat add static mapping}
2414 * Static mapping allows hosts on the external network to initiate connection
2415 * to to the local network host.
2416 * To create static mapping between local host address 10.0.0.3 port 6303 and
2417 * external address 4.4.4.4 port 3606 for TCP protocol use:
2418 * vpp# snat add static mapping local tcp 10.0.0.3 6303 external 4.4.4.4 3606
2419 * If not runnig "static mapping only" S-NAT plugin mode use before:
2420 * vpp# snat add address 4.4.4.4
2421 * To create static mapping between local and external address use:
2422 * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
2425 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
2426 .path = "snat add static mapping",
2427 .function = add_static_mapping_command_fn,
2429 "snat add static mapping local tcp|udp|icmp <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
2432 static clib_error_t *
2433 set_workers_command_fn (vlib_main_t * vm,
2434 unformat_input_t * input,
2435 vlib_cli_command_t * cmd)
2437 unformat_input_t _line_input, *line_input = &_line_input;
2440 clib_error_t *error = 0;
2442 /* Get a line of input. */
2443 if (!unformat_user (input, unformat_line_input, line_input))
2446 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2448 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
2452 error = clib_error_return (0, "unknown input '%U'",
2453 format_unformat_error, line_input);
2460 error = clib_error_return (0, "List of workers must be specified.");
2464 rv = snat_set_workers(bitmap);
2466 clib_bitmap_free (bitmap);
2470 case VNET_API_ERROR_INVALID_WORKER:
2471 error = clib_error_return (0, "Invalid worker(s).");
2473 case VNET_API_ERROR_FEATURE_DISABLED:
2474 error = clib_error_return (0,
2475 "Supported only if 2 or more workes available.");
2482 unformat_free (line_input);
2489 * @cliexstart{set snat workers}
2490 * Set SNAT workers if 2 or more workers available, use:
2491 * vpp# set snat workers 0-2,5
2494 VLIB_CLI_COMMAND (set_workers_command, static) = {
2495 .path = "set snat workers",
2496 .function = set_workers_command_fn,
2498 "set snat workers <workers-list>",
2501 static clib_error_t *
2502 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
2503 unformat_input_t * input,
2504 vlib_cli_command_t * cmd)
2506 unformat_input_t _line_input, *line_input = &_line_input;
2511 clib_error_t *error = 0;
2513 /* Get a line of input. */
2514 if (!unformat_user (input, unformat_line_input, line_input))
2517 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2519 if (unformat (line_input, "domain %d", &domain_id))
2521 else if (unformat (line_input, "src-port %d", &src_port))
2523 else if (unformat (line_input, "disable"))
2527 error = clib_error_return (0, "unknown input '%U'",
2528 format_unformat_error, line_input);
2533 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
2537 error = clib_error_return (0, "ipfix logging enable failed");
2542 unformat_free (line_input);
2549 * @cliexstart{snat ipfix logging}
2550 * To enable SNAT IPFIX logging use:
2551 * vpp# snat ipfix logging
2552 * To set IPFIX exporter use:
2553 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
2556 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
2557 .path = "snat ipfix logging",
2558 .function = snat_ipfix_logging_enable_disable_command_fn,
2559 .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
2563 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2565 snat_main_t *sm = &snat_main;
2566 snat_user_key_t key0;
2567 clib_bihash_kv_8_8_t kv0, value0;
2568 u32 next_worker_index = 0;
2570 key0.addr = ip0->src_address;
2571 key0.fib_index = rx_fib_index0;
2573 kv0.key = key0.as_u64;
2575 /* Ever heard of of the "user" before? */
2576 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv0, &value0))
2578 /* No, assign next available worker (RR) */
2579 next_worker_index = sm->first_worker_index;
2580 if (vec_len (sm->workers))
2582 next_worker_index +=
2583 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
2586 /* add non-traslated packets worker lookup */
2587 kv0.value = next_worker_index;
2588 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
2591 next_worker_index = value0.value;
2593 return next_worker_index;
2597 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2599 snat_main_t *sm = &snat_main;
2600 snat_worker_key_t key0;
2601 clib_bihash_kv_8_8_t kv0, value0;
2602 udp_header_t * udp0;
2603 u32 next_worker_index = 0;
2605 udp0 = ip4_next_header (ip0);
2607 key0.addr = ip0->dst_address;
2608 key0.port = udp0->dst_port;
2609 key0.fib_index = rx_fib_index0;
2611 if (PREDICT_FALSE(ip0->protocol == IP_PROTOCOL_ICMP))
2613 icmp46_header_t * icmp0 = (icmp46_header_t *) udp0;
2614 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
2615 key0.port = echo0->identifier;
2618 kv0.key = key0.as_u64;
2620 /* Ever heard of of the "user" before? */
2621 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
2624 kv0.key = key0.as_u64;
2626 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
2628 /* No, assign next available worker (RR) */
2629 next_worker_index = sm->first_worker_index;
2630 if (vec_len (sm->workers))
2632 next_worker_index +=
2633 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
2638 /* Static mapping without port */
2639 next_worker_index = value0.value;
2642 /* Add to translated packets worker lookup */
2643 kv0.value = next_worker_index;
2644 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
2647 next_worker_index = value0.value;
2649 return next_worker_index;
2652 static clib_error_t *
2653 snat_config (vlib_main_t * vm, unformat_input_t * input)
2655 snat_main_t * sm = &snat_main;
2656 u32 translation_buckets = 1024;
2657 u32 translation_memory_size = 128<<20;
2658 u32 user_buckets = 128;
2659 u32 user_memory_size = 64<<20;
2660 u32 max_translations_per_user = 100;
2661 u32 outside_vrf_id = 0;
2662 u32 inside_vrf_id = 0;
2663 u32 static_mapping_buckets = 1024;
2664 u32 static_mapping_memory_size = 64<<20;
2665 u8 static_mapping_only = 0;
2666 u8 static_mapping_connection_tracking = 0;
2667 vlib_thread_main_t *tm = vlib_get_thread_main ();
2669 sm->deterministic = 0;
2671 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2673 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2675 else if (unformat (input, "translation hash memory %d",
2676 &translation_memory_size));
2677 else if (unformat (input, "user hash buckets %d", &user_buckets))
2679 else if (unformat (input, "user hash memory %d",
2682 else if (unformat (input, "max translations per user %d",
2683 &max_translations_per_user))
2685 else if (unformat (input, "outside VRF id %d",
2688 else if (unformat (input, "inside VRF id %d",
2691 else if (unformat (input, "static mapping only"))
2693 static_mapping_only = 1;
2694 if (unformat (input, "connection tracking"))
2695 static_mapping_connection_tracking = 1;
2697 else if (unformat (input, "deterministic"))
2698 sm->deterministic = 1;
2700 return clib_error_return (0, "unknown input '%U'",
2701 format_unformat_error, input);
2704 /* for show commands, etc. */
2705 sm->translation_buckets = translation_buckets;
2706 sm->translation_memory_size = translation_memory_size;
2707 sm->user_buckets = user_buckets;
2708 sm->user_memory_size = user_memory_size;
2709 sm->max_translations_per_user = max_translations_per_user;
2710 sm->outside_vrf_id = outside_vrf_id;
2711 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2713 sm->inside_vrf_id = inside_vrf_id;
2714 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2716 sm->static_mapping_only = static_mapping_only;
2717 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2719 if (sm->deterministic)
2721 sm->in2out_node_index = snat_det_in2out_node.index;
2722 sm->out2in_node_index = snat_det_out2in_node.index;
2723 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
2724 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
2728 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2729 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2730 sm->in2out_node_index = snat_in2out_node.index;
2731 sm->out2in_node_index = snat_out2in_node.index;
2732 if (!static_mapping_only ||
2733 (static_mapping_only && static_mapping_connection_tracking))
2735 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2736 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2738 clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
2741 clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
2744 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
2746 clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
2747 translation_memory_size);
2749 clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
2750 translation_memory_size);
2752 clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
2757 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2758 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2760 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2761 "static_mapping_by_local", static_mapping_buckets,
2762 static_mapping_memory_size);
2764 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2765 "static_mapping_by_external", static_mapping_buckets,
2766 static_mapping_memory_size);
2772 VLIB_CONFIG_FUNCTION (snat_config, "snat");
2774 u8 * format_snat_session_state (u8 * s, va_list * args)
2776 u32 i = va_arg (*args, u32);
2781 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
2782 foreach_snat_session_state
2785 t = format (t, "unknown");
2787 s = format (s, "%s", t);
2791 u8 * format_snat_key (u8 * s, va_list * args)
2793 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
2794 char * protocol_string = "unknown";
2795 static char *protocol_strings[] = {
2801 if (key->protocol < ARRAY_LEN(protocol_strings))
2802 protocol_string = protocol_strings[key->protocol];
2804 s = format (s, "%U proto %s port %d fib %d",
2805 format_ip4_address, &key->addr, protocol_string,
2806 clib_net_to_host_u16 (key->port), key->fib_index);
2810 u8 * format_snat_session (u8 * s, va_list * args)
2812 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2813 snat_session_t * sess = va_arg (*args, snat_session_t *);
2815 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2816 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2817 s = format (s, " last heard %.2f\n", sess->last_heard);
2818 s = format (s, " total pkts %d, total bytes %lld\n",
2819 sess->total_pkts, sess->total_bytes);
2820 if (snat_is_session_static (sess))
2821 s = format (s, " static translation\n");
2823 s = format (s, " dynamic translation\n");
2828 u8 * format_snat_user (u8 * s, va_list * args)
2830 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2831 snat_user_t * u = va_arg (*args, snat_user_t *);
2832 int verbose = va_arg (*args, int);
2833 dlist_elt_t * head, * elt;
2834 u32 elt_index, head_index;
2836 snat_session_t * sess;
2838 s = format (s, "%U: %d dynamic translations, %d static translations\n",
2839 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
2844 if (u->nsessions || u->nstaticsessions)
2846 head_index = u->sessions_per_user_list_head_index;
2847 head = pool_elt_at_index (sm->list_pool, head_index);
2849 elt_index = head->next;
2850 elt = pool_elt_at_index (sm->list_pool, elt_index);
2851 session_index = elt->value;
2853 while (session_index != ~0)
2855 sess = pool_elt_at_index (sm->sessions, session_index);
2857 s = format (s, " %U\n", format_snat_session, sm, sess);
2859 elt_index = elt->next;
2860 elt = pool_elt_at_index (sm->list_pool, elt_index);
2861 session_index = elt->value;
2868 u8 * format_snat_static_mapping (u8 * s, va_list * args)
2870 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2873 s = format (s, "local %U external %U vrf %d",
2874 format_ip4_address, &m->local_addr,
2875 format_ip4_address, &m->external_addr,
2878 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2879 format_snat_protocol, m->proto,
2880 format_ip4_address, &m->local_addr, m->local_port,
2881 format_ip4_address, &m->external_addr, m->external_port,
2887 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
2889 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
2890 vnet_main_t *vnm = vnet_get_main();
2893 s = format (s, "local %U external %U vrf %d",
2894 format_ip4_address, &m->l_addr,
2895 format_vnet_sw_interface_name, vnm,
2896 vnet_get_sw_interface (vnm, m->sw_if_index),
2899 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2900 format_snat_protocol, m->proto,
2901 format_ip4_address, &m->l_addr, m->l_port,
2902 format_vnet_sw_interface_name, vnm,
2903 vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
2909 u8 * format_det_map_ses (u8 * s, va_list * args)
2911 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
2912 ip4_address_t in_addr, out_addr;
2913 u32 in_offset, out_offset;
2914 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2915 u32 * i = va_arg (*args, u32 *);
2917 u32 user_index = *i / SNAT_DET_SES_PER_USER;
2918 in_addr.as_u32 = clib_host_to_net_u32 (
2919 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2920 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2921 clib_net_to_host_u32(det_map->in_addr.as_u32);
2922 out_offset = in_offset / det_map->sharing_ratio;
2923 out_addr.as_u32 = clib_host_to_net_u32(
2924 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2925 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2926 format_ip4_address, &in_addr,
2927 clib_net_to_host_u16 (ses->in_port),
2928 format_ip4_address, &out_addr,
2929 clib_net_to_host_u16 (ses->out.out_port),
2930 format_ip4_address, &ses->out.ext_host_addr,
2931 clib_net_to_host_u16 (ses->out.ext_host_port),
2932 format_snat_session_state, ses->state,
2938 static clib_error_t *
2939 show_snat_command_fn (vlib_main_t * vm,
2940 unformat_input_t * input,
2941 vlib_cli_command_t * cmd)
2944 snat_main_t * sm = &snat_main;
2946 snat_static_mapping_t *m;
2947 snat_interface_t *i;
2948 snat_address_t * ap;
2949 vnet_main_t *vnm = vnet_get_main();
2950 snat_main_per_thread_data_t *tsm;
2951 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2953 snat_static_map_resolve_t *rp;
2954 snat_det_map_t * dm;
2955 snat_det_session_t * ses;
2957 if (unformat (input, "detail"))
2959 else if (unformat (input, "verbose"))
2962 if (sm->static_mapping_only)
2964 if (sm->static_mapping_connection_tracking)
2965 vlib_cli_output (vm, "SNAT mode: static mapping only connection "
2968 vlib_cli_output (vm, "SNAT mode: static mapping only");
2970 else if (sm->deterministic)
2972 vlib_cli_output (vm, "SNAT mode: deterministic mapping");
2976 vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
2981 pool_foreach (i, sm->interfaces,
2983 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2984 vnet_get_sw_interface (vnm, i->sw_if_index),
2985 i->is_inside ? "in" : "out");
2988 if (vec_len (sm->auto_add_sw_if_indices))
2990 vlib_cli_output (vm, "SNAT pool addresses interfaces:");
2991 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2993 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2994 vnet_get_sw_interface (vnm, *sw_if_index));
2998 vec_foreach (ap, sm->addresses)
3000 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
3001 if (ap->fib_index != ~0)
3002 vlib_cli_output (vm, " tenant VRF: %u",
3003 ip4_fib_get(ap->fib_index)->table_id);
3005 vlib_cli_output (vm, " tenant VRF independent");
3006 #define _(N, i, n, s) \
3007 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
3008 foreach_snat_protocol
3013 if (sm->num_workers > 1)
3015 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
3018 vec_foreach (worker, sm->workers)
3020 vlib_worker_thread_t *w =
3021 vlib_worker_threads + *worker + sm->first_worker_index;
3022 vlib_cli_output (vm, " %s", w->name);
3027 if (sm->deterministic)
3029 vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
3030 vlib_cli_output (vm, "tcp-established timeout: %dsec",
3031 sm->tcp_established_timeout);
3032 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
3033 sm->tcp_transitory_timeout);
3034 vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
3035 vlib_cli_output (vm, "%d deterministic mappings",
3036 pool_elts (sm->det_maps));
3039 pool_foreach (dm, sm->det_maps,
3041 vlib_cli_output (vm, "in %U/%d out %U/%d\n",
3042 format_ip4_address, &dm->in_addr, dm->in_plen,
3043 format_ip4_address, &dm->out_addr, dm->out_plen);
3044 vlib_cli_output (vm, " outside address sharing ratio: %d\n",
3046 vlib_cli_output (vm, " number of ports per inside host: %d\n",
3047 dm->ports_per_host);
3048 vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
3051 vec_foreach_index (j, dm->sessions)
3053 ses = vec_elt_at_index (dm->sessions, j);
3055 vlib_cli_output (vm, " %U", format_det_map_ses, dm, ses,
3064 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
3066 vlib_cli_output (vm, "%d static mappings",
3067 pool_elts (sm->static_mappings));
3071 pool_foreach (m, sm->static_mappings,
3073 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
3079 vec_foreach (tsm, sm->per_thread_data)
3081 users_num += pool_elts (tsm->users);
3082 sessions_num += pool_elts (tsm->sessions);
3085 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
3086 " %d static mappings",
3088 vec_len (sm->addresses),
3090 pool_elts (sm->static_mappings));
3094 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
3096 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
3098 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
3100 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
3102 vec_foreach_index (j, sm->per_thread_data)
3104 tsm = vec_elt_at_index (sm->per_thread_data, j);
3106 if (pool_elts (tsm->users) == 0)
3109 vlib_worker_thread_t *w = vlib_worker_threads + j;
3110 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
3112 vlib_cli_output (vm, " %d list pool elements",
3113 pool_elts (tsm->list_pool));
3115 pool_foreach (u, tsm->users,
3117 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
3122 if (pool_elts (sm->static_mappings))
3124 vlib_cli_output (vm, "static mappings:");
3125 pool_foreach (m, sm->static_mappings,
3127 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
3129 for (j = 0; j < vec_len (sm->to_resolve); j++)
3131 rp = sm->to_resolve + j;
3132 vlib_cli_output (vm, "%U",
3133 format_snat_static_map_to_resolve, rp);
3142 VLIB_CLI_COMMAND (show_snat_command, static) = {
3143 .path = "show snat",
3144 .short_help = "show snat",
3145 .function = show_snat_command_fn,
3150 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
3153 ip4_address_t * address,
3155 u32 if_address_index,
3158 snat_main_t *sm = &snat_main;
3159 snat_static_map_resolve_t *rp;
3160 u32 *indices_to_delete = 0;
3164 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
3166 if (sw_if_index == sm->auto_add_sw_if_indices[i])
3170 /* Don't trip over lease renewal, static config */
3171 for (j = 0; j < vec_len(sm->addresses); j++)
3172 if (sm->addresses[j].addr.as_u32 == address->as_u32)
3175 snat_add_address (sm, address, ~0);
3176 /* Scan static map resolution vector */
3177 for (j = 0; j < vec_len (sm->to_resolve); j++)
3179 rp = sm->to_resolve + j;
3180 /* On this interface? */
3181 if (rp->sw_if_index == sw_if_index)
3183 /* Add the static mapping */
3184 rv = snat_add_static_mapping (rp->l_addr,
3190 ~0 /* sw_if_index */,
3194 clib_warning ("snat_add_static_mapping returned %d",
3196 vec_add1 (indices_to_delete, j);
3199 /* If we resolved any of the outstanding static mappings */
3200 if (vec_len(indices_to_delete))
3203 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
3204 vec_delete(sm->to_resolve, 1, j);
3205 vec_free(indices_to_delete);
3211 (void) snat_del_address(sm, address[0], 1);
3219 static int snat_add_interface_address (snat_main_t *sm,
3223 ip4_main_t * ip4_main = sm->ip4_main;
3224 ip4_address_t * first_int_addr;
3225 snat_static_map_resolve_t *rp;
3226 u32 *indices_to_delete = 0;
3229 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
3230 0 /* just want the address*/);
3232 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
3234 if (sm->auto_add_sw_if_indices[i] == sw_if_index)
3238 /* if have address remove it */
3240 (void) snat_del_address (sm, first_int_addr[0], 1);
3243 for (j = 0; j < vec_len (sm->to_resolve); j++)
3245 rp = sm->to_resolve + j;
3246 if (rp->sw_if_index == sw_if_index)
3247 vec_add1 (indices_to_delete, j);
3249 if (vec_len(indices_to_delete))
3251 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
3252 vec_del1(sm->to_resolve, j);
3253 vec_free(indices_to_delete);
3256 vec_del1(sm->auto_add_sw_if_indices, i);
3259 return VNET_API_ERROR_VALUE_EXIST;
3266 return VNET_API_ERROR_NO_SUCH_ENTRY;
3268 /* add to the auto-address list */
3269 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
3271 /* If the address is already bound - or static - add it now */
3273 snat_add_address (sm, first_int_addr, ~0);
3278 static clib_error_t *
3279 snat_add_interface_address_command_fn (vlib_main_t * vm,
3280 unformat_input_t * input,
3281 vlib_cli_command_t * cmd)
3283 snat_main_t *sm = &snat_main;
3284 unformat_input_t _line_input, *line_input = &_line_input;
3288 clib_error_t *error = 0;
3290 /* Get a line of input. */
3291 if (!unformat_user (input, unformat_line_input, line_input))
3294 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3296 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
3297 sm->vnet_main, &sw_if_index))
3299 else if (unformat (line_input, "del"))
3303 error = clib_error_return (0, "unknown input '%U'",
3304 format_unformat_error, line_input);
3309 rv = snat_add_interface_address (sm, sw_if_index, is_del);
3317 error = clib_error_return (0, "snat_add_interface_address returned %d",
3323 unformat_free (line_input);
3328 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
3329 .path = "snat add interface address",
3330 .short_help = "snat add interface address <interface> [del]",
3331 .function = snat_add_interface_address_command_fn,
3334 static clib_error_t *
3335 snat_det_map_command_fn (vlib_main_t * vm,
3336 unformat_input_t * input,
3337 vlib_cli_command_t * cmd)
3339 snat_main_t *sm = &snat_main;
3340 unformat_input_t _line_input, *line_input = &_line_input;
3341 ip4_address_t in_addr, out_addr;
3342 u32 in_plen, out_plen;
3344 clib_error_t *error = 0;
3346 /* Get a line of input. */
3347 if (!unformat_user (input, unformat_line_input, line_input))
3350 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3352 if (unformat (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
3354 else if (unformat (line_input, "out %U/%u", unformat_ip4_address, &out_addr, &out_plen))
3356 else if (unformat (line_input, "del"))
3360 error = clib_error_return (0, "unknown input '%U'",
3361 format_unformat_error, line_input);
3366 unformat_free (line_input);
3368 rv = snat_det_add_map(sm, &in_addr, (u8) in_plen, &out_addr, (u8)out_plen,
3373 error = clib_error_return (0, "snat_det_add_map return %d", rv);
3378 unformat_free (line_input);
3385 * @cliexstart{snat deterministic add}
3386 * Create bijective mapping of inside address to outside address and port range
3387 * pairs, with the purpose of enabling deterministic NAT to reduce logging in
3389 * To create deterministic mapping between inside network 10.0.0.0/18 and
3390 * outside network 1.1.1.0/30 use:
3391 * # vpp# snat deterministic add in 10.0.0.0/18 out 1.1.1.0/30
3394 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
3395 .path = "snat deterministic add",
3396 .short_help = "snat deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
3397 .function = snat_det_map_command_fn,
3400 static clib_error_t *
3401 snat_det_forward_command_fn (vlib_main_t * vm,
3402 unformat_input_t * input,
3403 vlib_cli_command_t * cmd)
3405 snat_main_t *sm = &snat_main;
3406 unformat_input_t _line_input, *line_input = &_line_input;
3407 ip4_address_t in_addr, out_addr;
3409 snat_det_map_t * dm;
3410 clib_error_t *error = 0;
3412 /* Get a line of input. */
3413 if (!unformat_user (input, unformat_line_input, line_input))
3416 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3418 if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
3422 error = clib_error_return (0, "unknown input '%U'",
3423 format_unformat_error, line_input);
3428 unformat_free (line_input);
3430 dm = snat_det_map_by_user(sm, &in_addr);
3432 vlib_cli_output (vm, "no match");
3435 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
3436 vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
3437 lo_port, lo_port + dm->ports_per_host - 1);
3441 unformat_free (line_input);
3448 * @cliexstart{snat deterministic forward}
3449 * Return outside address and port range from inside address for deterministic
3451 * To obtain outside address and port of inside host use:
3452 * vpp# snat deterministic forward 10.0.0.2
3453 * 1.1.1.0:<1054-1068>
3456 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
3457 .path = "snat deterministic forward",
3458 .short_help = "snat deterministic forward <addr>",
3459 .function = snat_det_forward_command_fn,
3462 static clib_error_t *
3463 snat_det_reverse_command_fn (vlib_main_t * vm,
3464 unformat_input_t * input,
3465 vlib_cli_command_t * cmd)
3467 snat_main_t *sm = &snat_main;
3468 unformat_input_t _line_input, *line_input = &_line_input;
3469 ip4_address_t in_addr, out_addr;
3471 snat_det_map_t * dm;
3472 clib_error_t *error = 0;
3474 /* Get a line of input. */
3475 if (!unformat_user (input, unformat_line_input, line_input))
3478 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3480 if (unformat (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
3484 error = clib_error_return (0, "unknown input '%U'",
3485 format_unformat_error, line_input);
3489 unformat_free (line_input);
3491 if (out_port < 1024 || out_port > 65535)
3493 error = clib_error_return (0, "wrong port, must be <1024-65535>");
3497 dm = snat_det_map_by_out(sm, &out_addr);
3499 vlib_cli_output (vm, "no match");
3502 snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
3503 vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
3507 unformat_free (line_input);
3514 * @cliexstart{snat deterministic reverse}
3515 * Return inside address from outside address and port for deterministic NAT.
3516 * To obtain inside host address from outside address and port use:
3517 * #vpp snat deterministic reverse 1.1.1.1:1276
3521 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
3522 .path = "snat deterministic reverse",
3523 .short_help = "snat deterministic reverse <addr>:<port>",
3524 .function = snat_det_reverse_command_fn,
3527 static clib_error_t *
3528 set_timeout_command_fn (vlib_main_t * vm,
3529 unformat_input_t * input,
3530 vlib_cli_command_t * cmd)
3532 snat_main_t *sm = &snat_main;
3533 unformat_input_t _line_input, *line_input = &_line_input;
3534 clib_error_t *error = 0;
3536 /* Get a line of input. */
3537 if (!unformat_user (input, unformat_line_input, line_input))
3540 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3542 if (unformat (line_input, "udp %u", &sm->udp_timeout))
3544 else if (unformat (line_input, "tcp-established %u",
3545 &sm->tcp_established_timeout))
3547 else if (unformat (line_input, "tcp-transitory %u",
3548 &sm->tcp_transitory_timeout))
3550 else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
3552 else if (unformat (line_input, "reset"))
3554 sm->udp_timeout = SNAT_UDP_TIMEOUT;
3555 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
3556 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
3557 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
3561 error = clib_error_return (0, "unknown input '%U'",
3562 format_unformat_error, line_input);
3567 unformat_free (line_input);
3570 unformat_free (line_input);
3577 * @cliexstart{set snat deterministic timeout}
3578 * Set values of timeouts for deterministic NAT (in seconds), use:
3579 * vpp# set snat deterministic timeout udp 120 tcp-established 7500
3580 * tcp-transitory 250 icmp 90
3581 * To reset default values use:
3582 * vpp# set snat deterministic timeout reset
3585 VLIB_CLI_COMMAND (set_timeout_command, static) = {
3586 .path = "set snat deterministic timeout",
3587 .function = set_timeout_command_fn,
3589 "set snat deterministic timeout [udp <sec> | tcp-established <sec> "
3590 "tcp-transitory <sec> | icmp <sec> | reset]",
3593 static clib_error_t *
3594 snat_det_close_session_out_fn (vlib_main_t *vm,
3595 unformat_input_t * input,
3596 vlib_cli_command_t * cmd)
3598 snat_main_t *sm = &snat_main;
3599 unformat_input_t _line_input, *line_input = &_line_input;
3600 ip4_address_t out_addr, ext_addr, in_addr;
3601 u16 out_port, ext_port;
3602 snat_det_map_t * dm;
3603 snat_det_session_t * ses;
3604 snat_det_out_key_t key;
3605 clib_error_t *error = 0;
3607 /* Get a line of input. */
3608 if (!unformat_user (input, unformat_line_input, line_input))
3611 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3613 if (unformat (line_input, "%U:%d %U:%d",
3614 unformat_ip4_address, &out_addr, &out_port,
3615 unformat_ip4_address, &ext_addr, &ext_port))
3619 error = clib_error_return (0, "unknown input '%U'",
3620 format_unformat_error, line_input);
3625 unformat_free (line_input);
3627 dm = snat_det_map_by_out(sm, &out_addr);
3629 vlib_cli_output (vm, "no match");
3632 snat_det_reverse(dm, &ext_addr, out_port, &in_addr);
3633 key.ext_host_addr = out_addr;
3634 key.ext_host_port = ntohs(ext_port);
3635 key.out_port = ntohs(out_port);
3636 ses = snat_det_get_ses_by_out(dm, &out_addr, key.as_u64);
3638 vlib_cli_output (vm, "no match");
3640 snat_det_ses_close(dm, ses);
3644 unformat_free (line_input);
3651 * @cliexstart{snat deterministic close session out}
3652 * Close session using outside ip address and port
3653 * and external ip address and port, use:
3654 * vpp# snat deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
3657 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
3658 .path = "snat deterministic close session out",
3659 .short_help = "snat deterministic close session out "
3660 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
3661 .function = snat_det_close_session_out_fn,
3664 static clib_error_t *
3665 snat_det_close_session_in_fn (vlib_main_t *vm,
3666 unformat_input_t * input,
3667 vlib_cli_command_t * cmd)
3669 snat_main_t *sm = &snat_main;
3670 unformat_input_t _line_input, *line_input = &_line_input;
3671 ip4_address_t in_addr, ext_addr;
3672 u16 in_port, ext_port;
3673 snat_det_map_t * dm;
3674 snat_det_session_t * ses;
3675 snat_det_out_key_t key;
3676 clib_error_t *error = 0;
3678 /* Get a line of input. */
3679 if (!unformat_user (input, unformat_line_input, line_input))
3682 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3684 if (unformat (line_input, "%U:%d %U:%d",
3685 unformat_ip4_address, &in_addr, &in_port,
3686 unformat_ip4_address, &ext_addr, &ext_port))
3690 error = clib_error_return (0, "unknown input '%U'",
3691 format_unformat_error, line_input);
3696 unformat_free (line_input);
3698 dm = snat_det_map_by_user (sm, &in_addr);
3700 vlib_cli_output (vm, "no match");
3703 key.ext_host_addr = ext_addr;
3704 key.ext_host_port = ntohs (ext_port);
3705 ses = snat_det_find_ses_by_in (dm, &in_addr, ntohs(in_port), key);
3707 vlib_cli_output (vm, "no match");
3709 snat_det_ses_close(dm, ses);
3713 unformat_free(line_input);
3720 * @cliexstart{snat deterministic close_session_in}
3721 * Close session using inside ip address and port
3722 * and external ip address and port, use:
3723 * vpp# snat deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
3726 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
3727 .path = "snat deterministic close session in",
3728 .short_help = "snat deterministic close session in "
3729 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
3730 .function = snat_det_close_session_in_fn,