#include <snat/snat.h>
#include <snat/snat_ipfix_logging.h>
#include <snat/snat_det.h>
+#include <snat/nat64.h>
#include <vnet/fib/fib_table.h>
#include <vnet/fib/ip4_fib.h>
-#include <vlibapi/api.h>
-#include <vlibmemory/api.h>
-#include <vlibsocket/api.h>
#include <vpp/app/version.h>
snat_main_t snat_main;
-/* define message IDs */
-#include <snat/snat_msg_enum.h>
-
-/* define message structures */
-#define vl_typedefs
-#include <snat/snat_all_api_h.h>
-#undef vl_typedefs
-
-/* define generated endian-swappers */
-#define vl_endianfun
-#include <snat/snat_all_api_h.h>
-#undef vl_endianfun
-
-#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
-
-#define REPLY_MSG_ID_BASE sm->msg_id_base
-#include <vlibapi/api_helper_macros.h>
-
-/* Get the API version number */
-#define vl_api_version(n,v) static u32 api_version=(v);
-#include <snat/snat_all_api_h.h>
-#undef vl_api_version
-
-/* Macro to finish up custom dump fns */
-#define FINISH \
- vec_add1 (s, 0); \
- vl_print (handle, (char *)s); \
- vec_free (s); \
- return handle;
/* Hook up input features */
VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
.node_name = "snat-out2in-fast",
.runs_before = VNET_FEATURES ("ip4-lookup"),
};
+VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
+ .arc_name = "ip4-unicast",
+ .node_name = "snat-hairpin-dst",
+ .runs_before = VNET_FEATURES ("ip4-lookup"),
+};
+
+/* Hook up output features */
+VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
+ .arc_name = "ip4-output",
+ .node_name = "snat-in2out-output",
+ .runs_before = VNET_FEATURES ("interface-output"),
+};
+VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
+ .arc_name = "ip4-output",
+ .node_name = "snat-in2out-output-worker-handoff",
+ .runs_before = VNET_FEATURES ("interface-output"),
+};
+VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
+ .arc_name = "ip4-output",
+ .node_name = "snat-hairpin-src",
+ .runs_before = VNET_FEATURES ("interface-output"),
+};
+
/* *INDENT-OFF* */
VLIB_PLUGIN_REGISTER () = {
(FIB_ENTRY_FLAG_CONNECTED |
FIB_ENTRY_FLAG_LOCAL |
FIB_ENTRY_FLAG_EXCLUSIVE),
- FIB_PROTOCOL_IP4,
+ DPO_PROTO_IP4,
NULL,
sw_if_index,
~0,
vec_add2 (sm->addresses, ap, 1);
ap->addr = *addr;
- ap->fib_index = ip4_fib_index_from_table_id(vrf_id);
+ if (vrf_id != ~0)
+ ap->fib_index =
+ fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id);
+ else
+ ap->fib_index = ~0;
#define _(N, i, n, s) \
clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535);
foreach_snat_protocol
snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
break;
}));
+ pool_foreach (i, sm->output_feature_interfaces,
+ ({
+ if (i->is_inside)
+ continue;
+
+ snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
+ break;
+ }));
}
static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
return 0;
}
-static void increment_v4_address (ip4_address_t * a)
+void increment_v4_address (ip4_address_t * a)
{
u32 v;
continue;
}
+ if (snat_is_unk_proto_session (s))
+ {
+ clib_bihash_kv_16_8_t up_kv;
+ snat_unk_proto_ses_key_t up_key;
+ up_key.l_addr = s->in2out.addr;
+ up_key.r_addr = s->ext_host_addr;
+ up_key.fib_index = s->in2out.fib_index;
+ up_key.proto = s->in2out.port;
+ up_key.rsvd[0] = up_key.rsvd[1] = up_key.rsvd[2] = 0;
+ up_kv.key[0] = up_key.as_u64[0];
+ up_kv.key[1] = up_key.as_u64[1];
+ if (clib_bihash_add_del_16_8 (&sm->in2out_unk_proto,
+ &up_kv, 0))
+ clib_warning ("in2out key del failed");
+
+ up_key.l_addr = s->out2in.addr;
+ up_key.fib_index = s->out2in.fib_index;
+ up_kv.key[0] = up_key.as_u64[0];
+ up_kv.key[1] = up_key.as_u64[1];
+ if (clib_bihash_add_del_16_8 (&sm->out2in_unk_proto,
+ &up_kv, 0))
+ clib_warning ("out2in key del failed");
+
+ goto delete;
+ }
/* log NAT event */
snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
s->out2in.addr.as_u32,
s->in2out.fib_index);
value.key = s->in2out.as_u64;
- clib_bihash_add_del_8_8 (&sm->in2out, &value, 0);
+ if (clib_bihash_add_del_8_8 (&sm->in2out, &value, 0))
+ clib_warning ("in2out key del failed");
value.key = s->out2in.as_u64;
- clib_bihash_add_del_8_8 (&sm->out2in, &value, 0);
+ if (clib_bihash_add_del_8_8 (&sm->out2in, &value, 0))
+ clib_warning ("out2in key del failed");
+delete:
pool_put (tsm->sessions, s);
clib_dlist_remove (tsm->list_pool, del_elt_index);
snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
break;
}));
+ pool_foreach (interface, sm->output_feature_interfaces,
+ ({
+ if (interface->is_inside)
+ continue;
+
+ snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
+ break;
+ }));
return 0;
}
}
}
+ if (a->fib_index != ~0)
+ fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4);
+
/* Delete sessions using address */
if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
{
pool_foreach (ses, tsm->sessions, ({
if (ses->out2in.addr.as_u32 == addr.as_u32)
{
- /* log NAT event */
- snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
- ses->out2in.addr.as_u32,
- ses->in2out.protocol,
- ses->in2out.port,
- ses->out2in.port,
- ses->in2out.fib_index);
+ if (snat_is_unk_proto_session (ses))
+ {
+ clib_bihash_kv_16_8_t up_kv;
+ snat_unk_proto_ses_key_t up_key;
+ up_key.l_addr = ses->in2out.addr;
+ up_key.r_addr = ses->ext_host_addr;
+ up_key.fib_index = ses->in2out.fib_index;
+ up_key.proto = ses->in2out.port;
+ up_key.rsvd[0] = up_key.rsvd[1] = up_key.rsvd[2] = 0;
+ up_kv.key[0] = up_key.as_u64[0];
+ up_kv.key[1] = up_key.as_u64[1];
+ if (clib_bihash_add_del_16_8 (&sm->in2out_unk_proto,
+ &up_kv, 0))
+ clib_warning ("in2out key del failed");
+
+ up_key.l_addr = ses->out2in.addr;
+ up_key.fib_index = ses->out2in.fib_index;
+ up_kv.key[0] = up_key.as_u64[0];
+ up_kv.key[1] = up_key.as_u64[1];
+ if (clib_bihash_add_del_16_8 (&sm->out2in_unk_proto,
+ &up_kv, 0))
+ clib_warning ("out2in key del failed");
+ }
+ else
+ {
+ /* log NAT event */
+ snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
+ ses->out2in.addr.as_u32,
+ ses->in2out.protocol,
+ ses->in2out.port,
+ ses->out2in.port,
+ ses->in2out.fib_index);
+ kv.key = ses->in2out.as_u64;
+ clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
+ kv.key = ses->out2in.as_u64;
+ clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
+ }
vec_add1 (ses_to_be_removed, ses - tsm->sessions);
- kv.key = ses->in2out.as_u64;
- clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
- kv.key = ses->out2in.as_u64;
- clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
clib_dlist_remove (tsm->list_pool, ses->per_user_index);
user_key.addr = ses->in2out.addr;
user_key.fib_index = ses->in2out.fib_index;
u->nsessions--;
}
}
- }));
-
- vec_foreach (ses_index, ses_to_be_removed)
- pool_put_index (tsm->sessions, ses_index[0]);
-
- vec_free (ses_to_be_removed);
- }
- }
-
- vec_del1 (sm->addresses, i);
-
- /* Delete external address from FIB */
- pool_foreach (interface, sm->interfaces,
- ({
- if (interface->is_inside)
- continue;
-
- snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
- break;
- }));
-
- return 0;
-}
-
-static int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
-{
- snat_main_t *sm = &snat_main;
- snat_interface_t *i;
- const char * feature_name;
- snat_address_t * ap;
- snat_static_mapping_t * m;
- snat_det_map_t * dm;
-
- if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
- feature_name = is_inside ? "snat-in2out-fast" : "snat-out2in-fast";
- else
- {
- if (sm->num_workers > 1 && !sm->deterministic)
- feature_name = is_inside ? "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
- else if (sm->deterministic)
- feature_name = is_inside ? "snat-det-in2out" : "snat-det-out2in";
- else
- feature_name = is_inside ? "snat-in2out" : "snat-out2in";
- }
-
- vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
- !is_del, 0, 0);
-
- if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
- sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
-
- if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
- sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
-
- pool_foreach (i, sm->interfaces,
- ({
- if (i->sw_if_index == sw_if_index)
- {
- if (is_del)
- pool_put (sm->interfaces, i);
- else
- return VNET_API_ERROR_VALUE_EXIST;
-
- goto fib;
- }
- }));
-
- if (is_del)
- return VNET_API_ERROR_NO_SUCH_ENTRY;
-
- pool_get (sm->interfaces, i);
- i->sw_if_index = sw_if_index;
- i->is_inside = is_inside;
-
- /* Add/delete external addresses to FIB */
-fib:
- if (is_inside)
- return 0;
-
- vec_foreach (ap, sm->addresses)
- snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
-
- pool_foreach (m, sm->static_mappings,
- ({
- if (!(m->addr_only))
- continue;
-
- snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
- }));
-
- pool_foreach (dm, sm->det_maps,
- ({
- snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
- }));
-
- return 0;
-}
-
-static int snat_set_workers (uword * bitmap)
-{
- snat_main_t *sm = &snat_main;
- int i;
-
- if (sm->num_workers < 2)
- return VNET_API_ERROR_FEATURE_DISABLED;
-
- if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
- return VNET_API_ERROR_INVALID_WORKER;
-
- vec_free (sm->workers);
- clib_bitmap_foreach (i, bitmap,
- ({
- vec_add1(sm->workers, i);
- }));
-
- return 0;
-}
-
-static void
-vl_api_snat_add_address_range_t_handler
-(vl_api_snat_add_address_range_t * mp)
-{
- snat_main_t * sm = &snat_main;
- vl_api_snat_add_address_range_reply_t * rmp;
- ip4_address_t this_addr;
- u32 start_host_order, end_host_order;
- u32 vrf_id;
- int i, count;
- int rv = 0;
- u32 * tmp;
-
- if (mp->is_ip4 != 1)
- {
- rv = VNET_API_ERROR_UNIMPLEMENTED;
- goto send_reply;
- }
-
- if (sm->static_mapping_only)
- {
- rv = VNET_API_ERROR_FEATURE_DISABLED;
- goto send_reply;
- }
-
- tmp = (u32 *) mp->first_ip_address;
- start_host_order = clib_host_to_net_u32 (tmp[0]);
- tmp = (u32 *) mp->last_ip_address;
- end_host_order = clib_host_to_net_u32 (tmp[0]);
-
- count = (end_host_order - start_host_order) + 1;
-
- vrf_id = clib_host_to_net_u32 (mp->vrf_id);
-
- if (count > 1024)
- clib_warning ("%U - %U, %d addresses...",
- format_ip4_address, mp->first_ip_address,
- format_ip4_address, mp->last_ip_address,
- count);
-
- memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
-
- for (i = 0; i < count; i++)
- {
- if (mp->is_add)
- snat_add_address (sm, &this_addr, vrf_id);
- else
- rv = snat_del_address (sm, this_addr, 0);
-
- if (rv)
- goto send_reply;
-
- increment_v4_address (&this_addr);
- }
-
- send_reply:
- REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
-}
-
-static void *vl_api_snat_add_address_range_t_print
-(vl_api_snat_add_address_range_t *mp, void * handle)
-{
- u8 * s;
-
- s = format (0, "SCRIPT: snat_add_address_range ");
- s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
- if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
- {
- s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
- }
- FINISH;
-}
-
-static void
-send_snat_address_details
-(snat_address_t * a, unix_shared_memory_queue_t * q, u32 context)
-{
- vl_api_snat_address_details_t *rmp;
- snat_main_t * sm = &snat_main;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_SNAT_ADDRESS_DETAILS+sm->msg_id_base);
- rmp->is_ip4 = 1;
- clib_memcpy (rmp->ip_address, &(a->addr), 4);
- if (a->fib_index != ~0)
- rmp->vrf_id = ntohl(ip4_fib_get(a->fib_index)->table_id);
- else
- rmp->vrf_id = ~0;
- rmp->context = context;
-
- vl_msg_api_send_shmem (q, (u8 *) & rmp);
-}
-
-static void
-vl_api_snat_address_dump_t_handler
-(vl_api_snat_address_dump_t * mp)
-{
- unix_shared_memory_queue_t *q;
- snat_main_t * sm = &snat_main;
- snat_address_t * a;
-
- q = vl_api_client_index_to_input_queue (mp->client_index);
- if (q == 0)
- return;
-
- vec_foreach (a, sm->addresses)
- send_snat_address_details (a, q, mp->context);
-}
-
-static void *vl_api_snat_address_dump_t_print
-(vl_api_snat_address_dump_t *mp, void * handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: snat_address_dump ");
-
- FINISH;
-}
-
-static void
-vl_api_snat_interface_add_del_feature_t_handler
-(vl_api_snat_interface_add_del_feature_t * mp)
-{
- snat_main_t * sm = &snat_main;
- vl_api_snat_interface_add_del_feature_reply_t * rmp;
- u8 is_del = mp->is_add == 0;
- u32 sw_if_index = ntohl(mp->sw_if_index);
- int rv = 0;
-
- VALIDATE_SW_IF_INDEX(mp);
-
- rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
-
- BAD_SW_IF_INDEX_LABEL;
-
- REPLY_MACRO(VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
-}
-
-static void *vl_api_snat_interface_add_del_feature_t_print
-(vl_api_snat_interface_add_del_feature_t * mp, void *handle)
-{
- u8 * s;
-
- s = format (0, "SCRIPT: snat_interface_add_del_feature ");
- s = format (s, "sw_if_index %d %s %s",
- clib_host_to_net_u32(mp->sw_if_index),
- mp->is_inside ? "in":"out",
- mp->is_add ? "" : "del");
-
- FINISH;
-}
-
-static void
-send_snat_interface_details
-(snat_interface_t * i, unix_shared_memory_queue_t * q, u32 context)
-{
- vl_api_snat_interface_details_t *rmp;
- snat_main_t * sm = &snat_main;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_DETAILS+sm->msg_id_base);
- rmp->sw_if_index = ntohl (i->sw_if_index);
- rmp->is_inside = i->is_inside;
- rmp->context = context;
-
- vl_msg_api_send_shmem (q, (u8 *) & rmp);
-}
-
-static void
-vl_api_snat_interface_dump_t_handler
-(vl_api_snat_interface_dump_t * mp)
-{
- unix_shared_memory_queue_t *q;
- snat_main_t * sm = &snat_main;
- snat_interface_t * i;
-
- q = vl_api_client_index_to_input_queue (mp->client_index);
- if (q == 0)
- return;
-
- pool_foreach (i, sm->interfaces,
- ({
- send_snat_interface_details(i, q, mp->context);
- }));
-}
-
-static void *vl_api_snat_interface_dump_t_print
-(vl_api_snat_interface_dump_t *mp, void * handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: snat_interface_dump ");
-
- FINISH;
-}static void
-
-vl_api_snat_add_static_mapping_t_handler
-(vl_api_snat_add_static_mapping_t * mp)
-{
- snat_main_t * sm = &snat_main;
- vl_api_snat_add_static_mapping_reply_t * rmp;
- ip4_address_t local_addr, external_addr;
- u16 local_port = 0, external_port = 0;
- u32 vrf_id, external_sw_if_index;
- int rv = 0;
- snat_protocol_t proto;
-
- if (mp->is_ip4 != 1)
- {
- rv = VNET_API_ERROR_UNIMPLEMENTED;
- goto send_reply;
- }
-
- memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
- memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
- if (mp->addr_only == 0)
- {
- local_port = clib_net_to_host_u16 (mp->local_port);
- external_port = clib_net_to_host_u16 (mp->external_port);
- }
- vrf_id = clib_net_to_host_u32 (mp->vrf_id);
- external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
- proto = ip_proto_to_snat_proto (mp->protocol);
-
- rv = snat_add_static_mapping(local_addr, external_addr, local_port,
- external_port, vrf_id, mp->addr_only,
- external_sw_if_index, proto, mp->is_add);
-
- send_reply:
- REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
-}
-
-static void *vl_api_snat_add_static_mapping_t_print
-(vl_api_snat_add_static_mapping_t *mp, void * handle)
-{
- u8 * s;
-
- s = format (0, "SCRIPT: snat_add_static_mapping ");
- s = format (s, "protocol %d local_addr %U external_addr %U ",
- mp->protocol,
- format_ip4_address, mp->local_ip_address,
- format_ip4_address, mp->external_ip_address);
-
- if (mp->addr_only == 0)
- s = format (s, "local_port %d external_port %d ",
- clib_net_to_host_u16 (mp->local_port),
- clib_net_to_host_u16 (mp->external_port));
-
- if (mp->vrf_id != ~0)
- s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
-
- if (mp->external_sw_if_index != ~0)
- s = format (s, "external_sw_if_index %d",
- clib_net_to_host_u32 (mp->external_sw_if_index));
- FINISH;
-}
-
-static void
-send_snat_static_mapping_details
-(snat_static_mapping_t * m, unix_shared_memory_queue_t * q, u32 context)
-{
- vl_api_snat_static_mapping_details_t *rmp;
- snat_main_t * sm = &snat_main;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
- rmp->is_ip4 = 1;
- rmp->addr_only = m->addr_only;
- clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
- clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
- rmp->local_port = htons (m->local_port);
- rmp->external_port = htons (m->external_port);
- rmp->external_sw_if_index = ~0;
- rmp->vrf_id = htonl (m->vrf_id);
- rmp->protocol = snat_proto_to_ip_proto (m->proto);
- rmp->context = context;
-
- vl_msg_api_send_shmem (q, (u8 *) & rmp);
-}
-
-static void
-send_snat_static_map_resolve_details
-(snat_static_map_resolve_t * m, unix_shared_memory_queue_t * q, u32 context)
-{
- vl_api_snat_static_mapping_details_t *rmp;
- snat_main_t * sm = &snat_main;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
- rmp->is_ip4 = 1;
- rmp->addr_only = m->addr_only;
- clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
- rmp->local_port = htons (m->l_port);
- rmp->external_port = htons (m->e_port);
- rmp->external_sw_if_index = htonl (m->sw_if_index);
- rmp->vrf_id = htonl (m->vrf_id);
- rmp->protocol = snat_proto_to_ip_proto (m->proto);
- rmp->context = context;
-
- vl_msg_api_send_shmem (q, (u8 *) & rmp);
-}
-
-static void
-vl_api_snat_static_mapping_dump_t_handler
-(vl_api_snat_static_mapping_dump_t * mp)
-{
- unix_shared_memory_queue_t *q;
- snat_main_t * sm = &snat_main;
- snat_static_mapping_t * m;
- snat_static_map_resolve_t *rp;
- int j;
-
- q = vl_api_client_index_to_input_queue (mp->client_index);
- if (q == 0)
- return;
-
- pool_foreach (m, sm->static_mappings,
- ({
- send_snat_static_mapping_details (m, q, mp->context);
- }));
-
- for (j = 0; j < vec_len (sm->to_resolve); j++)
- {
- rp = sm->to_resolve + j;
- send_snat_static_map_resolve_details (rp, q, mp->context);
- }
-}
-
-static void *vl_api_snat_static_mapping_dump_t_print
-(vl_api_snat_static_mapping_dump_t *mp, void * handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: snat_static_mapping_dump ");
-
- FINISH;
-}
-
-static void
-vl_api_snat_control_ping_t_handler
-(vl_api_snat_control_ping_t * mp)
-{
- vl_api_snat_control_ping_reply_t *rmp;
- snat_main_t * sm = &snat_main;
- int rv = 0;
-
- REPLY_MACRO2(VL_API_SNAT_CONTROL_PING_REPLY,
- ({
- rmp->vpe_pid = ntohl (getpid());
- }));
-}
-
-static void *vl_api_snat_control_ping_t_print
-(vl_api_snat_control_ping_t *mp, void * handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: snat_control_ping ");
-
- FINISH;
-}
-
-static void
-vl_api_snat_show_config_t_handler
-(vl_api_snat_show_config_t * mp)
-{
- vl_api_snat_show_config_reply_t *rmp;
- snat_main_t * sm = &snat_main;
- int rv = 0;
-
- REPLY_MACRO2(VL_API_SNAT_SHOW_CONFIG_REPLY,
- ({
- rmp->translation_buckets = htonl (sm->translation_buckets);
- rmp->translation_memory_size = htonl (sm->translation_memory_size);
- rmp->user_buckets = htonl (sm->user_buckets);
- rmp->user_memory_size = htonl (sm->user_memory_size);
- rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
- rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
- rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
- rmp->static_mapping_only = sm->static_mapping_only;
- rmp->static_mapping_connection_tracking =
- sm->static_mapping_connection_tracking;
- rmp->deterministic = sm->deterministic;
- }));
-}
-
-static void *vl_api_snat_show_config_t_print
-(vl_api_snat_show_config_t *mp, void * handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: snat_show_config ");
-
- FINISH;
-}
-
-static void
-vl_api_snat_set_workers_t_handler
-(vl_api_snat_set_workers_t * mp)
-{
- snat_main_t * sm = &snat_main;
- vl_api_snat_set_workers_reply_t * rmp;
- int rv = 0;
- uword *bitmap = 0;
- u64 mask = clib_net_to_host_u64 (mp->worker_mask);
-
- if (sm->num_workers < 2)
- {
- rv = VNET_API_ERROR_FEATURE_DISABLED;
- goto send_reply;
- }
-
- bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
- rv = snat_set_workers(bitmap);
- clib_bitmap_free (bitmap);
-
- send_reply:
- REPLY_MACRO (VL_API_SNAT_SET_WORKERS_REPLY);
-}
-
-static void *vl_api_snat_set_workers_t_print
-(vl_api_snat_set_workers_t *mp, void * handle)
-{
- u8 * s;
- uword *bitmap = 0;
- u8 first = 1;
- int i;
- u64 mask = clib_net_to_host_u64 (mp->worker_mask);
-
- s = format (0, "SCRIPT: snat_set_workers ");
- bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
- clib_bitmap_foreach (i, bitmap,
- ({
- if (first)
- s = format (s, "%d", i);
- else
- s = format (s, ",%d", i);
- first = 0;
- }));
- clib_bitmap_free (bitmap);
- FINISH;
-}
-
-static void
-send_snat_worker_details
-(u32 worker_index, unix_shared_memory_queue_t * q, u32 context)
-{
- vl_api_snat_worker_details_t *rmp;
- snat_main_t * sm = &snat_main;
- vlib_worker_thread_t *w =
- vlib_worker_threads + worker_index + sm->first_worker_index;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_SNAT_WORKER_DETAILS+sm->msg_id_base);
- rmp->context = context;
- rmp->worker_index = htonl (worker_index);
- rmp->lcore_id = htonl (w->lcore_id);
- strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
-
- vl_msg_api_send_shmem (q, (u8 *) & rmp);
-}
-
-static void
-vl_api_snat_worker_dump_t_handler
-(vl_api_snat_worker_dump_t * mp)
-{
- unix_shared_memory_queue_t *q;
- snat_main_t * sm = &snat_main;
- u32 * worker_index;
-
- q = vl_api_client_index_to_input_queue (mp->client_index);
- if (q == 0)
- return;
-
- vec_foreach (worker_index, sm->workers)
- {
- send_snat_worker_details(*worker_index, q, mp->context);
- }
-}
-
-static void *vl_api_snat_worker_dump_t_print
-(vl_api_snat_worker_dump_t *mp, void * handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: snat_worker_dump ");
-
- FINISH;
-}
-
-static int snat_add_interface_address(snat_main_t *sm,
- u32 sw_if_index,
- int is_del);
-
-static void
-vl_api_snat_add_del_interface_addr_t_handler
-(vl_api_snat_add_del_interface_addr_t * mp)
-{
- snat_main_t * sm = &snat_main;
- vl_api_snat_add_del_interface_addr_reply_t * rmp;
- u8 is_del = mp->is_add == 0;
- u32 sw_if_index = ntohl(mp->sw_if_index);
- int rv = 0;
-
- VALIDATE_SW_IF_INDEX(mp);
-
- rv = snat_add_interface_address (sm, sw_if_index, is_del);
-
- BAD_SW_IF_INDEX_LABEL;
-
- REPLY_MACRO(VL_API_SNAT_ADD_DEL_INTERFACE_ADDR_REPLY);
-}
-
-static void *vl_api_snat_add_del_interface_addr_t_print
-(vl_api_snat_add_del_interface_addr_t * mp, void *handle)
-{
- u8 * s;
-
- s = format (0, "SCRIPT: snat_add_del_interface_addr ");
- s = format (s, "sw_if_index %d %s",
- clib_host_to_net_u32(mp->sw_if_index),
- mp->is_add ? "" : "del");
-
- FINISH;
-}
-
-static void
-send_snat_interface_addr_details
-(u32 sw_if_index, unix_shared_memory_queue_t * q, u32 context)
-{
- vl_api_snat_interface_addr_details_t *rmp;
- snat_main_t * sm = &snat_main;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_ADDR_DETAILS+sm->msg_id_base);
- rmp->sw_if_index = ntohl (sw_if_index);
- rmp->context = context;
-
- vl_msg_api_send_shmem (q, (u8 *) & rmp);
-}
-
-static void
-vl_api_snat_interface_addr_dump_t_handler
-(vl_api_snat_interface_addr_dump_t * mp)
-{
- unix_shared_memory_queue_t *q;
- snat_main_t * sm = &snat_main;
- u32 * i;
-
- q = vl_api_client_index_to_input_queue (mp->client_index);
- if (q == 0)
- return;
-
- vec_foreach (i, sm->auto_add_sw_if_indices)
- send_snat_interface_addr_details(*i, q, mp->context);
-}
-
-static void *vl_api_snat_interface_addr_dump_t_print
-(vl_api_snat_interface_addr_dump_t *mp, void * handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: snat_interface_addr_dump ");
-
- FINISH;
-}
-
-static void
-vl_api_snat_ipfix_enable_disable_t_handler
-(vl_api_snat_ipfix_enable_disable_t * mp)
-{
- snat_main_t * sm = &snat_main;
- vl_api_snat_ipfix_enable_disable_reply_t * rmp;
- int rv = 0;
-
- rv = snat_ipfix_logging_enable_disable(mp->enable,
- clib_host_to_net_u32 (mp->domain_id),
- clib_host_to_net_u16 (mp->src_port));
-
- REPLY_MACRO (VL_API_SNAT_IPFIX_ENABLE_DISABLE_REPLY);
-}
-
-static void *vl_api_snat_ipfix_enable_disable_t_print
-(vl_api_snat_ipfix_enable_disable_t *mp, void * handle)
-{
- u8 * s;
-
- s = format (0, "SCRIPT: snat_ipfix_enable_disable ");
- if (mp->domain_id)
- s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
- if (mp->src_port)
- s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
- if (!mp->enable)
- s = format (s, "disable ");
-
- FINISH;
-}
-
-static void
-send_snat_user_details
-(snat_user_t * u, unix_shared_memory_queue_t * q, u32 context)
-{
- vl_api_snat_user_details_t * rmp;
- snat_main_t * sm = &snat_main;
- ip4_fib_t * fib_table;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_SNAT_USER_DETAILS+sm->msg_id_base);
-
- fib_table = ip4_fib_get(u->fib_index);
- rmp->vrf_id = ntohl (fib_table->table_id);
-
- rmp->is_ip4 = 1;
- clib_memcpy(rmp->ip_address, &(u->addr), 4);
- rmp->nsessions = ntohl (u->nsessions);
- rmp->nstaticsessions = ntohl (u->nstaticsessions);
- rmp->context = context;
-
- vl_msg_api_send_shmem (q, (u8 *) & rmp);
-}
-
-static void
-vl_api_snat_user_dump_t_handler
-(vl_api_snat_user_dump_t * mp)
-{
- unix_shared_memory_queue_t *q;
- snat_main_t * sm = &snat_main;
- snat_main_per_thread_data_t * tsm;
- snat_user_t * u;
-
- q = vl_api_client_index_to_input_queue (mp->client_index);
- if (q == 0)
- return;
-
- vec_foreach (tsm, sm->per_thread_data)
- vec_foreach (u, tsm->users)
- send_snat_user_details (u, q, mp->context);
-}
-
-static void *vl_api_snat_user_dump_t_print
-(vl_api_snat_user_dump_t *mp, void * handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: snat_user_dump ");
-
- FINISH;
-}
-
-static void
-send_snat_user_session_details
-(snat_session_t * s, unix_shared_memory_queue_t * q, u32 context)
-{
- vl_api_snat_user_session_details_t * rmp;
- snat_main_t * sm = &snat_main;
-
- rmp = vl_msg_api_alloc (sizeof(*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_SNAT_USER_SESSION_DETAILS+sm->msg_id_base);
- rmp->is_ip4 = 1;
- clib_memcpy(rmp->outside_ip_address, (&s->out2in.addr), 4);
- rmp->outside_port = s->out2in.port;
- clib_memcpy(rmp->inside_ip_address, (&s->in2out.addr), 4);
- rmp->inside_port = s->in2out.port;
- rmp->protocol = ntohs(snat_proto_to_ip_proto(s->in2out.protocol));
- rmp->is_static = s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING ? 1 : 0;
- rmp->last_heard = clib_host_to_net_u64((u64)s->last_heard);
- rmp->total_bytes = clib_host_to_net_u64(s->total_bytes);
- rmp->total_pkts = ntohl(s->total_pkts);
- rmp->context = context;
-
- vl_msg_api_send_shmem (q, (u8 *) & rmp);
-}
-
-static void
-vl_api_snat_user_session_dump_t_handler
-(vl_api_snat_user_session_dump_t * mp)
-{
- unix_shared_memory_queue_t *q;
- snat_main_t * sm = &snat_main;
- snat_main_per_thread_data_t *tsm;
- snat_session_t * s;
- clib_bihash_kv_8_8_t key, value;
- snat_user_key_t ukey;
- snat_user_t * u;
- u32 session_index, head_index, elt_index;
- dlist_elt_t * head, * elt;
-
- q = vl_api_client_index_to_input_queue (mp->client_index);
- if (q == 0)
- return;
- if (!mp->is_ip4)
- return;
-
- clib_memcpy (&ukey.addr, mp->ip_address, 4);
- ukey.fib_index = ip4_fib_index_from_table_id (ntohl(mp->vrf_id));
- key.key = ukey.as_u64;
- if (!clib_bihash_search_8_8 (&sm->worker_by_in, &key, &value))
- tsm = vec_elt_at_index (sm->per_thread_data, value.value);
- else
- tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
- if (clib_bihash_search_8_8 (&sm->user_hash, &key, &value))
- return;
- u = pool_elt_at_index (tsm->users, value.value);
- if (!u->nsessions && !u->nstaticsessions)
- return;
-
- head_index = u->sessions_per_user_list_head_index;
- head = pool_elt_at_index (tsm->list_pool, head_index);
- elt_index = head->next;
- elt = pool_elt_at_index (tsm->list_pool, elt_index);
- session_index = elt->value;
- while (session_index != ~0)
- {
- s = pool_elt_at_index (tsm->sessions, session_index);
-
- send_snat_user_session_details (s, q, mp->context);
-
- elt_index = elt->next;
- elt = pool_elt_at_index (tsm->list_pool, elt_index);
- session_index = elt->value;
- }
-}
-
-static void *vl_api_snat_user_session_dump_t_print
-(vl_api_snat_user_session_dump_t *mp, void * handle)
-{
- u8 *s;
-
- s = format (0, "SCRIPT: snat_user_session_dump ");
- s = format (s, "ip_address %U vrf_id %d\n",
- format_ip4_address, mp->ip_address,
- clib_net_to_host_u32 (mp->vrf_id));
-
- FINISH;
-}
-
-static void
-vl_api_snat_add_det_map_t_handler
-(vl_api_snat_add_det_map_t * mp)
-{
- snat_main_t * sm = &snat_main;
- vl_api_snat_add_det_map_reply_t * rmp;
- int rv = 0;
- ip4_address_t in_addr, out_addr;
-
- clib_memcpy(&in_addr, mp->in_addr, 4);
- clib_memcpy(&out_addr, mp->out_addr, 4);
- rv = snat_det_add_map(sm, &in_addr, mp->in_plen, &out_addr,
- mp->out_plen, mp->is_add);
-
- REPLY_MACRO (VL_API_SNAT_ADD_DET_MAP_REPLY);
-}
-
-static void *vl_api_snat_add_det_map_t_print
-(vl_api_snat_add_det_map_t *mp, void * handle)
-{
- u8 * s;
-
- s = format (0, "SCRIPT: snat_add_det_map ");
- s = format (s, "inside address %U/%d outside address %U/%d\n",
- format_ip4_address, mp->in_addr, mp->in_plen,
- format_ip4_address, mp->out_addr, mp->out_plen);
-
- FINISH;
-}
-
-static void
-vl_api_snat_det_forward_t_handler
-(vl_api_snat_det_forward_t * mp)
-{
- snat_main_t * sm = &snat_main;
- vl_api_snat_det_forward_reply_t * rmp;
- int rv = 0;
- u16 lo_port = 0, hi_port = 0;
- snat_det_map_t * dm;
- ip4_address_t in_addr, out_addr;
+ }));
- out_addr.as_u32 = 0;
- clib_memcpy(&in_addr, mp->in_addr, 4);
- dm = snat_det_map_by_user(sm, &in_addr);
- if (!dm)
- {
- rv = VNET_API_ERROR_NO_SUCH_ENTRY;
- goto send_reply;
+ vec_foreach (ses_index, ses_to_be_removed)
+ pool_put_index (tsm->sessions, ses_index[0]);
+
+ vec_free (ses_to_be_removed);
+ }
}
- snat_det_forward(dm, &in_addr, &out_addr, &lo_port);
- hi_port = lo_port + dm->ports_per_host - 1;
+ vec_del1 (sm->addresses, i);
-send_reply:
- REPLY_MACRO2(VL_API_SNAT_DET_FORWARD_REPLY,
+ /* Delete external address from FIB */
+ pool_foreach (interface, sm->interfaces,
({
- rmp->out_port_lo = ntohs(lo_port);
- rmp->out_port_hi = ntohs(hi_port);
- rmp->is_ip4 = 1;
- memset(rmp->out_addr, 0, 16);
- clib_memcpy(rmp->out_addr, &out_addr, 4);
- }))
-}
+ if (interface->is_inside)
+ continue;
-static void *vl_api_snat_det_forward_t_print
-(vl_api_snat_det_forward_t * mp, void * handle)
-{
- u8 * s;
+ snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
+ break;
+ }));
+ pool_foreach (interface, sm->output_feature_interfaces,
+ ({
+ if (interface->is_inside)
+ continue;
- s = format (0, "SCRIPT: smat_det_forward_t");
- s = format (s, "inside ip address %U\n",
- format_ip4_address, mp->in_addr);
+ snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
+ break;
+ }));
- FINISH;
+ return 0;
}
-static void
-vl_api_snat_det_reverse_t_handler
-(vl_api_snat_det_reverse_t * mp)
+int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
{
- snat_main_t * sm = &snat_main;
- vl_api_snat_det_reverse_reply_t * rmp;
- int rv = 0;
- ip4_address_t out_addr, in_addr;
+ snat_main_t *sm = &snat_main;
+ snat_interface_t *i;
+ const char * feature_name;
+ snat_address_t * ap;
+ snat_static_mapping_t * m;
snat_det_map_t * dm;
- in_addr.as_u32 = 0;
- clib_memcpy(&out_addr, mp->out_addr, 4);
- dm = snat_det_map_by_out(sm, &out_addr);
- if (!dm)
+ if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
+ feature_name = is_inside ? "snat-in2out-fast" : "snat-out2in-fast";
+ else
{
- rv = VNET_API_ERROR_NO_SUCH_ENTRY;
- goto send_reply;
+ if (sm->num_workers > 1 && !sm->deterministic)
+ feature_name = is_inside ? "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
+ else if (sm->deterministic)
+ feature_name = is_inside ? "snat-det-in2out" : "snat-det-out2in";
+ else
+ feature_name = is_inside ? "snat-in2out" : "snat-out2in";
}
- snat_det_reverse(dm, &out_addr, htons(mp->out_port), &in_addr);
-
- send_reply:
- REPLY_MACRO2(VL_API_SNAT_DET_REVERSE_REPLY,
- ({
- rmp->is_ip4 = 1;
- memset(rmp->in_addr, 0, 16);
- clib_memcpy(rmp->in_addr, &in_addr, 4);
- }))
-}
-
-static void *vl_api_snat_det_reverse_t_print
-(vl_api_snat_det_reverse_t * mp, void * handle)
-{
- u8 * s;
-
- s = format(0, "SCRIPT: smat_det_reverse_t");
- s = format(s, "outside ip address %U outside port %d",
- format_ip4_address, mp->out_addr, ntohs(mp->out_port));
-
- FINISH;
-}
-
-static void
-sent_snat_det_map_details
-(snat_det_map_t * m, unix_shared_memory_queue_t * q, u32 context)
-{
- vl_api_snat_det_map_details_t *rmp;
- snat_main_t * sm = &snat_main;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_SNAT_DET_MAP_DETAILS+sm->msg_id_base);
- rmp->is_ip4 = 1;
- clib_memcpy (rmp->in_addr, &m->in_addr, 4);
- rmp->in_plen = m->in_plen;
- clib_memcpy (rmp->out_addr, &m->out_addr, 4);
- rmp->out_plen = m->out_plen;
- rmp->sharing_ratio = htonl (m->sharing_ratio);
- rmp->ports_per_host = htons (m->ports_per_host);
- rmp->ses_num = htonl (m->ses_num);
- rmp->context = context;
-
- vl_msg_api_send_shmem (q, (u8 *) & rmp);
-}
-
-static void
-vl_api_snat_det_map_dump_t_handler
-(vl_api_snat_det_map_dump_t * mp)
-{
- unix_shared_memory_queue_t *q;
- snat_main_t * sm = &snat_main;
- snat_det_map_t * m;
-
- q = vl_api_client_index_to_input_queue (mp->client_index);
- if (q == 0)
- return;
-
- vec_foreach(m, sm->det_maps)
- sent_snat_det_map_details(m, q, mp->context);
-}
-
-static void * vl_api_snat_det_map_dump_t_print
-(vl_api_snat_det_map_dump_t *mp, void * handle)
-{
- u8 * s;
-
- s = format (0, "SCRIPT: snat_det_map_dump ");
+ vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
+ !is_del, 0, 0);
- FINISH;
-}
+ if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
+ sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
-static void
-vl_api_snat_det_set_timeouts_t_handler
-(vl_api_snat_det_set_timeouts_t * mp)
-{
- snat_main_t * sm = &snat_main;
- vl_api_snat_det_set_timeouts_reply_t * rmp;
- int rv = 0;
+ if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
+ sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
- sm->udp_timeout = ntohl(mp->udp);
- sm->tcp_established_timeout = ntohl(mp->tcp_established);
- sm->tcp_transitory_timeout = ntohl(mp->tcp_transitory);
- sm->icmp_timeout = ntohl(mp->icmp);
+ pool_foreach (i, sm->interfaces,
+ ({
+ if (i->sw_if_index == sw_if_index)
+ {
+ if (is_del)
+ pool_put (sm->interfaces, i);
+ else
+ return VNET_API_ERROR_VALUE_EXIST;
- REPLY_MACRO (VL_API_SNAT_DET_SET_TIMEOUTS_REPLY);
-}
+ goto fib;
+ }
+ }));
-static void *vl_api_snat_det_set_timeouts_t_print
-(vl_api_snat_det_set_timeouts_t *mp, void * handle)
-{
- u8 * s;
+ if (is_del)
+ return VNET_API_ERROR_NO_SUCH_ENTRY;
- s = format (0, "SCRIPT: snat_det_set_timeouts ");
- s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
- ntohl(mp->udp),
- ntohl(mp->tcp_established),
- ntohl(mp->tcp_transitory),
- ntohl(mp->icmp));
+ pool_get (sm->interfaces, i);
+ i->sw_if_index = sw_if_index;
+ i->is_inside = is_inside;
- FINISH;
-}
+ /* Add/delete external addresses to FIB */
+fib:
+ if (is_inside)
+ return 0;
-static void
-vl_api_snat_det_get_timeouts_t_handler
-(vl_api_snat_det_get_timeouts_t * mp)
-{
- snat_main_t * sm = &snat_main;
- vl_api_snat_det_get_timeouts_reply_t * rmp;
- int rv = 0;
+ vec_foreach (ap, sm->addresses)
+ snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
- REPLY_MACRO2(VL_API_SNAT_DET_GET_TIMEOUTS_REPLY,
+ pool_foreach (m, sm->static_mappings,
({
- rmp->udp = htonl(sm->udp_timeout);
- rmp->tcp_established = htonl(sm->tcp_established_timeout);
- rmp->tcp_transitory = htonl(sm->tcp_transitory_timeout);
- rmp->icmp = htonl(sm->icmp_timeout);
- }))
-}
+ if (!(m->addr_only))
+ continue;
-static void *vl_api_snat_det_get_timeouts_t_print
-(vl_api_snat_det_get_timeouts_t * mp, void * handle)
-{
- u8 * s;
+ snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
+ }));
- s = format(0, "SCRIPT: snat_det_get_timeouts");
+ pool_foreach (dm, sm->det_maps,
+ ({
+ snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
+ }));
- FINISH;
+ return 0;
}
-static void
-vl_api_snat_det_close_session_out_t_handler
-(vl_api_snat_det_close_session_out_t * mp)
+int snat_interface_add_del_output_feature (u32 sw_if_index,
+ u8 is_inside,
+ int is_del)
{
- snat_main_t * sm = &snat_main;
- vl_api_snat_det_close_session_out_reply_t * rmp;
- ip4_address_t out_addr, ext_addr, in_addr;
- snat_det_out_key_t key;
- snat_det_map_t * dm;
- snat_det_session_t * ses;
- int rv = 0;
+ snat_main_t *sm = &snat_main;
+ snat_interface_t *i;
+ snat_address_t * ap;
+ snat_static_mapping_t * m;
- clib_memcpy(&out_addr, mp->out_addr, 4);
- clib_memcpy(&ext_addr, mp->ext_addr, 4);
+ if (sm->deterministic ||
+ (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
+ return VNET_API_ERROR_UNSUPPORTED;
- dm = snat_det_map_by_out(sm, &out_addr);
- if (!dm)
+ if (is_inside)
+ {
+ vnet_feature_enable_disable ("ip4-unicast", "snat-hairpin-dst",
+ sw_if_index, !is_del, 0, 0);
+ vnet_feature_enable_disable ("ip4-output", "snat-hairpin-src",
+ sw_if_index, !is_del, 0, 0);
+ goto fq;
+ }
+
+ if (sm->num_workers > 1)
{
- rv = VNET_API_ERROR_NO_SUCH_ENTRY;
- goto send_reply;
+ vnet_feature_enable_disable ("ip4-unicast", "snat-out2in-worker-handoff",
+ sw_if_index, !is_del, 0, 0);
+ vnet_feature_enable_disable ("ip4-output",
+ "snat-in2out-output-worker-handoff",
+ sw_if_index, !is_del, 0, 0);
}
- snat_det_reverse(dm, &ext_addr, ntohs(mp->out_port), &in_addr);
- key.ext_host_addr = ext_addr;
- key.ext_host_port = mp->ext_port;
- key.out_port = mp->out_port;
- ses = snat_det_get_ses_by_out(dm, &in_addr, key.as_u64);
- if (!ses)
+ else
{
- rv = VNET_API_ERROR_NO_SUCH_ENTRY;
- goto send_reply;
+ vnet_feature_enable_disable ("ip4-unicast", "snat-out2in", sw_if_index,
+ !is_del, 0, 0);
+ vnet_feature_enable_disable ("ip4-output", "snat-in2out-output",
+ sw_if_index, !is_del, 0, 0);
}
- snat_det_ses_close(dm, ses);
-send_reply:
- REPLY_MACRO (VL_API_SNAT_DET_CLOSE_SESSION_OUT_REPLY);
-}
+fq:
+ if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
+ sm->fq_in2out_output_index =
+ vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
-static void *vl_api_snat_det_close_session_out_t_print
-(vl_api_snat_det_close_session_out_t *mp, void * handle)
-{
- u8 * s;
+ if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
+ sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
- s = format (0, "SCRIPT: snat_det_close_session_out ");
- s = format (s, "out_addr %U out_port %d "
- "ext_addr %U ext_port %d\n",
- format_ip4_address, mp->out_addr, ntohs(mp->out_port),
- format_ip4_address, mp->ext_addr, ntohs(mp->ext_port));
+ pool_foreach (i, sm->output_feature_interfaces,
+ ({
+ if (i->sw_if_index == sw_if_index)
+ {
+ if (is_del)
+ pool_put (sm->output_feature_interfaces, i);
+ else
+ return VNET_API_ERROR_VALUE_EXIST;
- FINISH;
-}
+ goto fib;
+ }
+ }));
-static void
-vl_api_snat_det_close_session_in_t_handler
-(vl_api_snat_det_close_session_in_t * mp)
-{
- snat_main_t * sm = &snat_main;
- vl_api_snat_det_close_session_in_reply_t * rmp;
- ip4_address_t in_addr, ext_addr;
- snat_det_out_key_t key;
- snat_det_map_t * dm;
- snat_det_session_t * ses;
- int rv = 0;
+ if (is_del)
+ return VNET_API_ERROR_NO_SUCH_ENTRY;
- clib_memcpy(&in_addr, mp->in_addr, 4);
- clib_memcpy(&ext_addr, mp->ext_addr, 4);
+ pool_get (sm->output_feature_interfaces, i);
+ i->sw_if_index = sw_if_index;
+ i->is_inside = is_inside;
- dm = snat_det_map_by_user(sm, &in_addr);
- if (!dm)
- {
- rv = VNET_API_ERROR_NO_SUCH_ENTRY;
- goto send_reply;
- }
- key.ext_host_addr = ext_addr;
- key.ext_host_port = mp->ext_port;
- ses = snat_det_find_ses_by_in(dm, &in_addr, mp->in_port, key);
- if (!ses)
- {
- rv = VNET_API_ERROR_NO_SUCH_ENTRY;
- goto send_reply;
- }
- snat_det_ses_close(dm, ses);
+ /* Add/delete external addresses to FIB */
+fib:
+ if (is_inside)
+ return 0;
-send_reply:
- REPLY_MACRO (VL_API_SNAT_DET_CLOSE_SESSION_OUT_REPLY);
-}
+ vec_foreach (ap, sm->addresses)
+ snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
-static void *vl_api_snat_det_close_session_in_t_print
-(vl_api_snat_det_close_session_in_t *mp, void * handle)
-{
- u8 * s;
- s = format (0, "SCRIPT: snat_det_close_session_in ");
- s = format (s, "in_addr %U in_port %d "
- "ext_addr %U ext_port %d\n",
- format_ip4_address, mp->in_addr, ntohs(mp->in_port),
- format_ip4_address, mp->ext_addr, ntohs(mp->ext_port));
-
- FINISH;
+ pool_foreach (m, sm->static_mappings,
+ ({
+ if (!(m->addr_only))
+ continue;
+
+ snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
+ }));
+
+ return 0;
}
-/* List of message types that this plugin understands */
-#define foreach_snat_plugin_api_msg \
-_(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range) \
-_(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature) \
-_(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping) \
-_(SNAT_CONTROL_PING, snat_control_ping) \
-_(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump) \
-_(SNAT_SHOW_CONFIG, snat_show_config) \
-_(SNAT_ADDRESS_DUMP, snat_address_dump) \
-_(SNAT_INTERFACE_DUMP, snat_interface_dump) \
-_(SNAT_SET_WORKERS, snat_set_workers) \
-_(SNAT_WORKER_DUMP, snat_worker_dump) \
-_(SNAT_ADD_DEL_INTERFACE_ADDR, snat_add_del_interface_addr) \
-_(SNAT_INTERFACE_ADDR_DUMP, snat_interface_addr_dump) \
-_(SNAT_IPFIX_ENABLE_DISABLE, snat_ipfix_enable_disable) \
-_(SNAT_USER_DUMP, snat_user_dump) \
-_(SNAT_USER_SESSION_DUMP, snat_user_session_dump) \
-_(SNAT_ADD_DET_MAP, snat_add_det_map) \
-_(SNAT_DET_FORWARD, snat_det_forward) \
-_(SNAT_DET_REVERSE, snat_det_reverse) \
-_(SNAT_DET_MAP_DUMP, snat_det_map_dump) \
-_(SNAT_DET_SET_TIMEOUTS, snat_det_set_timeouts) \
-_(SNAT_DET_GET_TIMEOUTS, snat_det_get_timeouts) \
-_(SNAT_DET_CLOSE_SESSION_OUT, snat_det_close_session_out) \
-_(SNAT_DET_CLOSE_SESSION_IN, snat_det_close_session_in)
-
-
-/* Set up the API message handling tables */
-static clib_error_t *
-snat_plugin_api_hookup (vlib_main_t *vm)
+int snat_set_workers (uword * bitmap)
{
- snat_main_t * sm __attribute__ ((unused)) = &snat_main;
-#define _(N,n) \
- vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
- #n, \
- vl_api_##n##_t_handler, \
- vl_noop_handler, \
- vl_api_##n##_t_endian, \
- vl_api_##n##_t_print, \
- sizeof(vl_api_##n##_t), 1);
- foreach_snat_plugin_api_msg;
-#undef _
+ snat_main_t *sm = &snat_main;
+ int i, j = 0;
- return 0;
-}
+ if (sm->num_workers < 2)
+ return VNET_API_ERROR_FEATURE_DISABLED;
-#define vl_msg_name_crc_list
-#include <snat/snat_all_api_h.h>
-#undef vl_msg_name_crc_list
+ if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
+ return VNET_API_ERROR_INVALID_WORKER;
-static void
-setup_message_id_table (snat_main_t * sm, api_main_t * am)
-{
-#define _(id,n,crc) \
- vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
- foreach_vl_msg_name_crc_snat;
-#undef _
-}
+ vec_free (sm->workers);
+ clib_bitmap_foreach (i, bitmap,
+ ({
+ vec_add1(sm->workers, i);
+ sm->per_thread_data[i].snat_thread_index = j;
+ j++;
+ }));
-static void plugin_custom_dump_configure (snat_main_t * sm)
-{
-#define _(n,f) sm->api_main->msg_print_handlers \
- [VL_API_##n + sm->msg_id_base] \
- = (void *) vl_api_##f##_t_print;
- foreach_snat_plugin_api_msg;
-#undef _
+ sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
+ sm->num_snat_thread = _vec_len (sm->workers);
+
+ return 0;
}
clib_error_t * error = 0;
ip4_main_t * im = &ip4_main;
ip_lookup_main_t * lm = &im->lookup_main;
- u8 * name;
uword *p;
vlib_thread_registration_t *tr;
vlib_thread_main_t *tm = vlib_get_thread_main ();
u32 i;
ip4_add_del_interface_address_callback_t cb4;
- name = format (0, "snat_%08x%c", api_version, 0);
-
- /* Ask for a correctly-sized block of API message decode slots */
- sm->msg_id_base = vl_msg_api_get_msg_ids
- ((char *) name, VL_MSG_FIRST_AVAILABLE);
-
sm->vlib_main = vm;
sm->vnet_main = vnet_get_main();
sm->ip4_main = im;
sm->first_worker_index = 0;
sm->next_worker = 0;
sm->num_workers = 0;
+ sm->num_snat_thread = 1;
sm->workers = 0;
+ sm->port_per_thread = 0xffff - 1024;
sm->fq_in2out_index = ~0;
sm->fq_out2in_index = ~0;
sm->udp_timeout = SNAT_UDP_TIMEOUT;
}
}
+ vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
+
/* Use all available workers by default */
if (sm->num_workers > 1)
{
snat_set_workers(bitmap);
clib_bitmap_free (bitmap);
}
+ else
+ {
+ sm->per_thread_data[0].snat_thread_index = 0;
+ }
- error = snat_plugin_api_hookup (vm);
-
- /* Add our API messages to the global name_crc hash table */
- setup_message_id_table (sm, &api_main);
-
- plugin_custom_dump_configure (sm);
- vec_free(name);
+ error = snat_api_init(vm, sm);
+ if (error)
+ return error;
/* Set up the interface address add/del callback */
cb4.function = snat_ip4_add_del_interface_address_cb;
/* Init IPFIX logging */
snat_ipfix_logging_init(vm);
+ error = nat64_init(vm);
+
return error;
}
return 0;
}
-int snat_alloc_outside_address_and_port (snat_main_t * sm,
+static_always_inline u16
+snat_random_port (snat_main_t * sm, u16 min, u16 max)
+{
+ return min + random_u32 (&sm->random_seed) /
+ (random_u32_max() / (max - min + 1) + 1);
+}
+
+int snat_alloc_outside_address_and_port (snat_main_t * sm,
u32 fib_index,
+ u32 thread_index,
snat_session_key_t * k,
u32 * address_indexp)
{
{
#define _(N, j, n, s) \
case SNAT_PROTOCOL_##N: \
- if (a->busy_##n##_ports < (65535-1024)) \
+ if (a->busy_##n##_ports < (sm->port_per_thread * sm->num_snat_thread)) \
{ \
while (1) \
{ \
- portnum = random_u32 (&sm->random_seed); \
- portnum &= 0xFFFF; \
- if (portnum < 1024) \
- continue; \
+ portnum = (sm->port_per_thread * \
+ sm->per_thread_data[thread_index].snat_thread_index) + \
+ snat_random_port(sm, 0, sm->port_per_thread) + 1024; \
if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
continue; \
clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
u32 sw_if_index;
u32 * inside_sw_if_indices = 0;
u32 * outside_sw_if_indices = 0;
+ u8 is_output_feature = 0;
int is_del = 0;
int i;
else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
vnm, &sw_if_index))
vec_add1 (outside_sw_if_indices, sw_if_index);
+ else if (unformat (line_input, "output-feature"))
+ is_output_feature = 1;
else if (unformat (line_input, "del"))
is_del = 1;
else
for (i = 0; i < vec_len(inside_sw_if_indices); i++)
{
sw_if_index = inside_sw_if_indices[i];
- snat_interface_add_del (sw_if_index, 1, is_del);
+ if (is_output_feature)
+ {
+ if (snat_interface_add_del_output_feature (sw_if_index, 1, is_del))
+ {
+ error = clib_error_return (0, "%s %U failed",
+ is_del ? "del" : "add",
+ format_vnet_sw_interface_name, vnm,
+ vnet_get_sw_interface (vnm,
+ sw_if_index));
+ goto done;
+ }
+ }
+ else
+ {
+ if (snat_interface_add_del (sw_if_index, 1, is_del))
+ {
+ error = clib_error_return (0, "%s %U failed",
+ is_del ? "del" : "add",
+ format_vnet_sw_interface_name, vnm,
+ vnet_get_sw_interface (vnm,
+ sw_if_index));
+ goto done;
+ }
+ }
}
}
for (i = 0; i < vec_len(outside_sw_if_indices); i++)
{
sw_if_index = outside_sw_if_indices[i];
- snat_interface_add_del (sw_if_index, 0, is_del);
+ if (is_output_feature)
+ {
+ if (snat_interface_add_del_output_feature (sw_if_index, 0, is_del))
+ {
+ error = clib_error_return (0, "%s %U failed",
+ is_del ? "del" : "add",
+ format_vnet_sw_interface_name, vnm,
+ vnet_get_sw_interface (vnm,
+ sw_if_index));
+ goto done;
+ }
+ }
+ else
+ {
+ if (snat_interface_add_del (sw_if_index, 0, is_del))
+ {
+ error = clib_error_return (0, "%s %U failed",
+ is_del ? "del" : "add",
+ format_vnet_sw_interface_name, vnm,
+ vnet_get_sw_interface (vnm,
+ sw_if_index));
+ goto done;
+ }
+ }
}
}
VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
.path = "set interface snat",
.function = snat_feature_command_fn,
- .short_help = "set interface snat in <intfc> out <intfc> [del]",
+ .short_help = "set interface snat in <intfc> out <intfc> [output-feature] "
+ "[del]",
};
uword
u32 static_mapping_memory_size = 64<<20;
u8 static_mapping_only = 0;
u8 static_mapping_connection_tracking = 0;
- vlib_thread_main_t *tm = vlib_get_thread_main ();
sm->deterministic = 0;
if (sm->deterministic)
{
sm->in2out_node_index = snat_det_in2out_node.index;
+ sm->in2out_output_node_index = ~0;
sm->out2in_node_index = snat_det_out2in_node.index;
sm->icmp_match_in2out_cb = icmp_match_in2out_det;
sm->icmp_match_out2in_cb = icmp_match_out2in_det;
sm->worker_in2out_cb = snat_get_worker_in2out_cb;
sm->worker_out2in_cb = snat_get_worker_out2in_cb;
sm->in2out_node_index = snat_in2out_node.index;
+ sm->in2out_output_node_index = snat_in2out_output_node.index;
sm->out2in_node_index = snat_out2in_node.index;
if (!static_mapping_only ||
(static_mapping_only && static_mapping_connection_tracking))
clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
user_memory_size);
- vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
-
clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
translation_memory_size);
clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
user_memory_size);
+
+ clib_bihash_init_16_8 (&sm->in2out_unk_proto, "in2out-unk-proto",
+ translation_buckets, translation_memory_size);
+
+ clib_bihash_init_16_8 (&sm->out2in_unk_proto, "out2in-unk-proto",
+ translation_buckets, translation_memory_size);
}
else
{
snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
snat_session_t * sess = va_arg (*args, snat_session_t *);
- s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
- s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
+ if (snat_is_unk_proto_session (sess))
+ {
+ s = format (s, " i2o %U proto %u fib %u\n",
+ format_ip4_address, &sess->in2out.addr, sess->in2out.port,
+ sess->in2out.fib_index);
+ s = format (s, " o2i %U proto %u fib %u\n",
+ format_ip4_address, &sess->out2in.addr, sess->out2in.port,
+ sess->out2in.fib_index);
+ }
+ else
+ {
+ s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
+ s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
+ }
s = format (s, " last heard %.2f\n", sess->last_heard);
s = format (s, " total pkts %d, total bytes %lld\n",
sess->total_pkts, sess->total_bytes);
i->is_inside ? "in" : "out");
}));
+ pool_foreach (i, sm->output_feature_interfaces,
+ ({
+ vlib_cli_output (vm, "%U output-feature %s",
+ format_vnet_sw_interface_name, vnm,
+ vnet_get_sw_interface (vnm, i->sw_if_index),
+ i->is_inside ? "in" : "out");
+ }));
+
if (vec_len (sm->auto_add_sw_if_indices))
{
vlib_cli_output (vm, "SNAT pool addresses interfaces:");
}
-static int snat_add_interface_address (snat_main_t *sm,
- u32 sw_if_index,
- int is_del)
+int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del)
{
ip4_main_t * ip4_main = sm->ip4_main;
ip4_address_t * first_int_addr;