42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) 45 #define vl_api_version(n,v) static u32 api_version=(v); 52 vl_print (handle, (char *)s); \ 64 #define REPLY_MACRO(t) \ 66 unix_shared_memory_queue_t * q = \ 67 vl_api_client_index_to_input_queue (mp->client_index); \ 71 rmp = vl_msg_api_alloc (sizeof (*rmp)); \ 72 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \ 73 rmp->context = mp->context; \ 74 rmp->retval = ntohl(rv); \ 76 vl_msg_api_send_shmem (q, (u8 *)&rmp); \ 82 .node_name =
"snat-in2out",
83 .runs_before = {
"snat-out2in", 0},
87 .node_name =
"snat-out2in",
88 .runs_before = {
"ip4-lookup", 0},
114 #if (1 || CLIB_DEBUG > 0) 116 #define VALIDATE_SW_IF_INDEX(mp) \ 117 do { u32 __sw_if_index = ntohl(mp->sw_if_index); \ 118 vnet_main_t *__vnm = vnet_get_main(); \ 119 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \ 121 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \ 122 goto bad_sw_if_index; \ 126 #define BAD_SW_IF_INDEX_LABEL \ 132 #define VALIDATE_RX_SW_IF_INDEX(mp) \ 133 do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index); \ 134 vnet_main_t *__vnm = vnet_get_main(); \ 135 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \ 136 __rx_sw_if_index)) { \ 137 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \ 138 goto bad_rx_sw_if_index; \ 142 #define BAD_RX_SW_IF_INDEX_LABEL \ 144 bad_rx_sw_if_index: \ 148 #define VALIDATE_TX_SW_IF_INDEX(mp) \ 149 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \ 150 vnet_main_t *__vnm = vnet_get_main(); \ 151 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \ 152 __tx_sw_if_index)) { \ 153 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \ 154 goto bad_tx_sw_if_index; \ 158 #define BAD_TX_SW_IF_INDEX_LABEL \ 160 bad_tx_sw_if_index: \ 166 #define VALIDATE_SW_IF_INDEX(mp) 167 #define BAD_SW_IF_INDEX_LABEL 168 #define VALIDATE_RX_SW_IF_INDEX(mp) 169 #define BAD_RX_SW_IF_INDEX_LABEL 170 #define VALIDATE_TX_SW_IF_INDEX(mp) 171 #define BAD_TX_SW_IF_INDEX_LABEL 188 v = clib_net_to_host_u32(a->
as_u32) + 1;
189 a->
as_u32 = clib_host_to_net_u32(v);
199 u32 start_host_order, end_host_order;
206 rv = VNET_API_ERROR_UNIMPLEMENTED;
211 start_host_order = clib_host_to_net_u32 (tmp[0]);
213 end_host_order = clib_host_to_net_u32 (tmp[0]);
215 count = (end_host_order - start_host_order) + 1;
225 for (i = 0; i < count; i++)
240 s =
format (0,
"SCRIPT: snat_add_address_range ");
282 REPLY_MACRO(VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
290 s =
format (0,
"SCRIPT: snat_interface_add_del_feature ");
291 s =
format (s,
"sw_if_index %d %s %s",
300 #define foreach_snat_plugin_api_msg \ 301 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range) \ 302 _(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature) 310 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \ 312 vl_api_##n##_t_handler, \ 314 vl_api_##n##_t_endian, \ 315 vl_api_##n##_t_print, \ 316 sizeof(vl_api_##n##_t), 1); 325 #define _(n,f) sm->api_main->msg_print_handlers \ 326 [VL_API_##n + sm->msg_id_base] \ 327 = (void *) vl_api_##f##_t_print; 340 name =
format (0,
"snat_%08x%c", api_version, 0);
366 u16 port_host_byte_order = clib_net_to_host_u16 (k->
port);
375 port_host_byte_order, 0);
381 u32 * address_indexp)
406 k->
port = clib_host_to_net_u16(portnum);
424 u32 start_host_order, end_host_order;
432 end_addr = start_addr;
434 start_host_order = clib_host_to_net_u32 (start_addr.
as_u32);
435 end_host_order = clib_host_to_net_u32 (end_addr.
as_u32);
437 if (end_host_order < start_host_order)
440 count = (end_host_order - start_host_order) + 1;
448 this_addr = start_addr;
450 for (i = 0; i < count; i++)
460 .path =
"snat add address",
461 .short_help =
"snat add addresses <ip4-range-start> [- <ip4-range-end>]",
478 u32 * inside_sw_if_indices = 0;
479 u32 * outside_sw_if_indices = 0;
489 vec_add1 (inside_sw_if_indices, sw_if_index);
492 vec_add1 (outside_sw_if_indices, sw_if_index);
499 if (
vec_len (inside_sw_if_indices))
503 for (i = 0; i <
vec_len(inside_sw_if_indices); i++)
505 sw_if_index = inside_sw_if_indices[
i];
519 if (
vec_len (outside_sw_if_indices))
523 for (i = 0; i <
vec_len(outside_sw_if_indices); i++)
525 sw_if_index = outside_sw_if_indices[
i];
546 .path =
"set interface snat",
548 .short_help =
"set interface snat in <intfc> out <intfc> [del]",
555 u32 translation_buckets = 1024;
556 u32 translation_memory_size = 128<<20;
557 u32 user_buckets = 128;
558 u32 user_memory_size = 64<<20;
559 u32 max_translations_per_user = 100;
560 u32 outside_vrf_id = 0;
564 if (
unformat (input,
"translation hash buckets %d", &translation_buckets))
566 else if (
unformat (input,
"translation hash memory %d",
567 &translation_memory_size));
568 else if (
unformat (input,
"user hash buckets %d", &user_buckets))
570 else if (
unformat (input,
"user hash memory %d",
573 else if (
unformat (input,
"max translations per user %d",
574 &max_translations_per_user))
576 else if (
unformat (input,
"outside VRF id %d",
592 clib_bihash_init_8_8 (&sm->
in2out,
"in2out", translation_buckets,
593 translation_memory_size);
595 clib_bihash_init_8_8 (&sm->
out2in,
"out2in", translation_buckets,
596 translation_memory_size);
598 clib_bihash_init_8_8 (&sm->
user_hash,
"users", user_buckets,
608 char * protocol_string =
"unknown";
609 static char *protocol_strings[] = {
616 protocol_string = protocol_strings[key->
protocol];
618 s =
format (s,
"%U proto %s port %d fib %d",
627 snat_session_t * sess = va_arg (*args, snat_session_t *);
631 s =
format (s,
" last heard %.2f\n", sess->last_heard);
632 s =
format (s,
" total pkts %d, total bytes %lld\n",
633 sess->total_pkts, sess->total_bytes);
642 int verbose = va_arg (*args,
int);
644 u32 elt_index, head_index;
646 snat_session_t * sess;
648 s =
format (s,
"%U: %d translations\n",
657 elt_index = head->
next;
659 session_index = elt->
value;
661 while (session_index != ~0)
667 elt_index = elt->
next;
669 session_index = elt->
value;
686 else if (
unformat (input,
"verbose"))
689 vlib_cli_output (vm,
"%d users, %d outside addresses, %d active sessions",
705 vlib_cli_output (vm,
"%U", format_snat_user, sm, u, verbose - 1);
714 .short_help =
"show snat",
u32 translation_memory_size
u32 sessions_per_user_list_head_index
u32 max_translations_per_user
static void vl_api_snat_interface_add_del_feature_t_handler(vl_api_snat_interface_add_del_feature_t *mp)
sll srl srl sll sra u16x4 i
u32 * config_index_by_sw_if_index
static void * vl_api_snat_interface_add_del_feature_t_print(vl_api_snat_interface_add_del_feature_t *mp, void *handle)
static void vl_api_snat_add_address_range_t_handler(vl_api_snat_add_address_range_t *mp)
u32 vnet_config_del_feature(vlib_main_t *vm, vnet_config_main_t *cm, u32 config_string_heap_index, u32 feature_index, void *feature_config, u32 n_feature_config_bytes)
ip_lookup_main_t * ip4_lookup_main
ip_config_main_t rx_config_mains[VNET_N_CAST]
rx/tx interface/feature configuration.
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
ip_lookup_main_t lookup_main
void snat_add_address(snat_main_t *sm, ip4_address_t *addr)
unformat_function_t unformat_vnet_sw_interface
static clib_error_t * snat_init(vlib_main_t *vm)
static clib_error_t * add_address_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
ethernet_main_t * ethernet_main
u8 * format_snat_key(u8 *s, va_list *args)
vnet_main_t * vnet_get_main(void)
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
#define VLIB_INIT_FUNCTION(x)
u8 * format_snat_user(u8 *s, va_list *args)
#define foreach_snat_plugin_api_msg
#define VALIDATE_SW_IF_INDEX(mp)
#define clib_warning(format, args...)
static clib_error_t * snat_plugin_api_hookup(vlib_main_t *vm)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
VNET_IP4_UNICAST_FEATURE_INIT(ip4_snat_in2out, static)
#define VLIB_CONFIG_FUNCTION(x, n,...)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
static void increment_v4_address(ip4_address_t *a)
u8 * format_snat_session(u8 *s, va_list *args)
static clib_error_t * show_snat_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
clib_bihash_8_8_t user_hash
#define vec_free(V)
Free vector's memory (no header).
snat_address_t * addresses
int snat_alloc_outside_address_and_port(snat_main_t *sm, snat_session_key_t *k, u32 *address_indexp)
clib_error_t * vlib_plugin_register(vlib_main_t *vm, vnet_plugin_handoff_t *h, int from_early_init)
static clib_error_t * snat_config(vlib_main_t *vm, unformat_input_t *input)
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
u32 vnet_config_add_feature(vlib_main_t *vm, vnet_config_main_t *cm, u32 config_string_heap_index, u32 feature_index, void *feature_config, u32 n_feature_config_bytes)
#define BAD_SW_IF_INDEX_LABEL
snat_session_t * sessions
VLIB_CLI_COMMAND(set_interface_ip_source_and_port_range_check_command, static)
static void * vl_api_snat_add_address_range_t_print(vl_api_snat_add_address_range_t *mp, void *handle)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
void snat_free_outside_address_and_port(snat_main_t *sm, snat_session_key_t *k, u32 address_index)
static u32 random_u32(u32 *seed)
32-bit random number generator
ip4_main_t ip4_main
Global ip4 main structure.
static void plugin_custom_dump_configure(snat_main_t *sm)
u16 vl_msg_api_get_msg_ids(char *name, int n)
#define clib_error_return(e, args...)
ethernet_main_t * ethernet_main
static clib_error_t * snat_feature_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
vnet_config_main_t config_main
static uword pool_elts(void *v)
Number of active elements in a pool.