FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
snat.c File Reference
+ Include dependency graph for snat.c:

Go to the source code of this file.

Macros

#define vl_typedefs
 
#define vl_endianfun
 
#define vl_print(handle, ...)   vlib_cli_output (handle, __VA_ARGS__)
 
#define vl_api_version(n, v)   static u32 api_version=(v);
 
#define FINISH
 
#define REPLY_MACRO(t)
 
#define REPLY_MACRO2(t, body)
 
#define VALIDATE_SW_IF_INDEX(mp)
 
#define BAD_SW_IF_INDEX_LABEL
 
#define VALIDATE_RX_SW_IF_INDEX(mp)
 
#define BAD_RX_SW_IF_INDEX_LABEL
 
#define VALIDATE_TX_SW_IF_INDEX(mp)
 
#define BAD_TX_SW_IF_INDEX_LABEL
 
#define foreach_snat_plugin_api_msg
 

Functions

 VNET_IP4_UNICAST_FEATURE_INIT (ip4_snat_in2out, static)
 
 VNET_IP4_UNICAST_FEATURE_INIT (ip4_snat_out2in, static)
 
 VNET_IP4_UNICAST_FEATURE_INIT (ip4_snat_in2out_fast, static)
 
 VNET_IP4_UNICAST_FEATURE_INIT (ip4_snat_out2in_fast, static)
 
clib_error_tvlib_plugin_register (vlib_main_t *vm, vnet_plugin_handoff_t *h, int from_early_init)
 
void snat_add_address (snat_main_t *sm, ip4_address_t *addr)
 
static int is_snat_address_used_in_static_mapping (snat_main_t *sm, ip4_address_t addr)
 
int snat_del_address (snat_main_t *sm, ip4_address_t addr)
 
static void increment_v4_address (ip4_address_t *a)
 
int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, u16 l_port, u16 e_port, u32 vrf_id, int addr_only, int is_add)
 Add static mapping. More...
 
static int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
 
static void vl_api_snat_add_address_range_t_handler (vl_api_snat_add_address_range_t *mp)
 
static void * vl_api_snat_add_address_range_t_print (vl_api_snat_add_address_range_t *mp, void *handle)
 
static void send_snat_address_details (snat_address_t *a, unix_shared_memory_queue_t *q, u32 context)
 
static void vl_api_snat_address_dump_t_handler (vl_api_snat_address_dump_t *mp)
 
static void * vl_api_snat_address_dump_t_print (vl_api_snat_address_dump_t *mp, void *handle)
 
static void vl_api_snat_interface_add_del_feature_t_handler (vl_api_snat_interface_add_del_feature_t *mp)
 
static void * vl_api_snat_interface_add_del_feature_t_print (vl_api_snat_interface_add_del_feature_t *mp, void *handle)
 
static void send_snat_interface_details (snat_interface_t *i, unix_shared_memory_queue_t *q, u32 context)
 
static void vl_api_snat_interface_dump_t_handler (vl_api_snat_interface_dump_t *mp)
 
static void * vl_api_snat_interface_dump_t_print (vl_api_snat_interface_dump_t *mp, void *handle)
 
static void vl_api_snat_add_static_mapping_t_handler (vl_api_snat_add_static_mapping_t *mp)
 
static void * vl_api_snat_add_static_mapping_t_print (vl_api_snat_add_static_mapping_t *mp, void *handle)
 
static void send_snat_static_mapping_details (snat_static_mapping_t *m, unix_shared_memory_queue_t *q, u32 context)
 
static void vl_api_snat_static_mapping_dump_t_handler (vl_api_snat_static_mapping_dump_t *mp)
 
static void * vl_api_snat_static_mapping_dump_t_print (vl_api_snat_static_mapping_dump_t *mp, void *handle)
 
static void vl_api_snat_control_ping_t_handler (vl_api_snat_control_ping_t *mp)
 
static void * vl_api_snat_control_ping_t_print (vl_api_snat_control_ping_t *mp, void *handle)
 
static void vl_api_snat_show_config_t_handler (vl_api_snat_show_config_t *mp)
 
static void * vl_api_snat_show_config_t_print (vl_api_snat_show_config_t *mp, void *handle)
 
static clib_error_tsnat_plugin_api_hookup (vlib_main_t *vm)
 
static void plugin_custom_dump_configure (snat_main_t *sm)
 
static clib_error_tsnat_init (vlib_main_t *vm)
 
void snat_free_outside_address_and_port (snat_main_t *sm, snat_session_key_t *k, u32 address_index)
 
int snat_static_mapping_match (snat_main_t *sm, snat_session_key_t match, snat_session_key_t *mapping, u8 by_external)
 Match SNAT static mapping. More...
 
int snat_alloc_outside_address_and_port (snat_main_t *sm, snat_session_key_t *k, u32 *address_indexp)
 
static clib_error_tadd_address_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
 
static clib_error_tsnat_feature_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
 
static clib_error_tadd_static_mapping_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
 
static clib_error_tsnat_config (vlib_main_t *vm, unformat_input_t *input)
 
u8format_snat_key (u8 *s, va_list *args)
 
u8format_snat_session (u8 *s, va_list *args)
 
u8format_snat_user (u8 *s, va_list *args)
 
u8format_snat_static_mapping (u8 *s, va_list *args)
 
static clib_error_tshow_snat_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
 

Variables

snat_main_t snat_main
 
static vlib_cli_command_t add_address_command
 (constructor) VLIB_CLI_COMMAND (add_address_command) More...
 
static vlib_cli_command_t set_interface_snat_command
 (constructor) VLIB_CLI_COMMAND (set_interface_snat_command) More...
 
static vlib_cli_command_t add_static_mapping_command
 (constructor) VLIB_CLI_COMMAND (add_static_mapping_command) More...
 
static vlib_cli_command_t show_snat_command
 (constructor) VLIB_CLI_COMMAND (show_snat_command) More...
 

Macro Definition Documentation

#define BAD_RX_SW_IF_INDEX_LABEL
Value:
do { \
bad_rx_sw_if_index: \
; \
} while (0);

Definition at line 168 of file snat.c.

#define BAD_SW_IF_INDEX_LABEL
Value:
do { \
bad_sw_if_index: \
; \
} while (0);

Definition at line 152 of file snat.c.

#define BAD_TX_SW_IF_INDEX_LABEL
Value:
do { \
bad_tx_sw_if_index: \
; \
} while (0);

Definition at line 184 of file snat.c.

#define FINISH
Value:
vec_add1 (s, 0); \
vl_print (handle, (char *)s); \
vec_free (s); \
return handle;
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:300
#define vl_print(handle,...)
Definition: snat.c:42

Definition at line 50 of file snat.c.

#define foreach_snat_plugin_api_msg
Value:
_(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)
int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, u16 l_port, u16 e_port, u32 vrf_id, int addr_only, int is_add)
Add static mapping.
Definition: snat.c:318

Definition at line 943 of file snat.c.

#define REPLY_MACRO (   t)
Value:
do { \
if (!q) \
return; \
\
rmp = vl_msg_api_alloc (sizeof (*rmp)); \
rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
rmp->context = mp->context; \
rmp->retval = ntohl(rv); \
vl_msg_api_send_shmem (q, (u8 *)&rmp); \
} while(0);
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
unix_shared_memory_queue_t * vl_api_client_index_to_input_queue(u32 index)
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
struct _unix_shared_memory_queue unix_shared_memory_queue_t

Definition at line 64 of file snat.c.

#define REPLY_MACRO2 (   t,
  body 
)
Value:
do { \
if (!q) \
return; \
\
rmp = vl_msg_api_alloc (sizeof (*rmp)); \
rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
rmp->context = mp->context; \
rmp->retval = ntohl(rv); \
do {body;} while (0); \
vl_msg_api_send_shmem (q, (u8 *)&rmp); \
} while(0);
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
unix_shared_memory_queue_t * vl_api_client_index_to_input_queue(u32 index)
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
struct _unix_shared_memory_queue unix_shared_memory_queue_t

Definition at line 79 of file snat.c.

#define VALIDATE_RX_SW_IF_INDEX (   mp)
Value:
do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index); \
if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
__rx_sw_if_index)) { \
rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
goto bad_rx_sw_if_index; \
} \
} while(0);
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:211
unsigned int u32
Definition: types.h:88
struct vnet_main_t vnet_main_t

Definition at line 158 of file snat.c.

#define VALIDATE_SW_IF_INDEX (   mp)
Value:
do { u32 __sw_if_index = ntohl(mp->sw_if_index); \
if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
__sw_if_index)) { \
rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
goto bad_sw_if_index; \
} \
} while(0);
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:211
unsigned int u32
Definition: types.h:88
struct vnet_main_t vnet_main_t

Definition at line 142 of file snat.c.

#define VALIDATE_TX_SW_IF_INDEX (   mp)
Value:
do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
__tx_sw_if_index)) { \
rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
goto bad_tx_sw_if_index; \
} \
} while(0);
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:211
unsigned int u32
Definition: types.h:88
struct vnet_main_t vnet_main_t

Definition at line 174 of file snat.c.

#define vl_api_version (   n,
 
)    static u32 api_version=(v);

Definition at line 45 of file snat.c.

#define vl_endianfun

Definition at line 38 of file snat.c.

#define vl_print (   handle,
  ... 
)    vlib_cli_output (handle, __VA_ARGS__)

Definition at line 42 of file snat.c.

#define vl_typedefs

Definition at line 33 of file snat.c.

Function Documentation

static clib_error_t* add_address_command_fn ( vlib_main_t vm,
unformat_input_t input,
vlib_cli_command_t cmd 
)
static

Definition at line 1128 of file snat.c.

+ Here is the call graph for this function:

static clib_error_t* add_static_mapping_command_fn ( vlib_main_t vm,
unformat_input_t input,
vlib_cli_command_t cmd 
)
static

Definition at line 1277 of file snat.c.

+ Here is the call graph for this function:

u8* format_snat_key ( u8 s,
va_list *  args 
)

Definition at line 1446 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

u8* format_snat_session ( u8 s,
va_list *  args 
)

Definition at line 1465 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

u8* format_snat_static_mapping ( u8 s,
va_list *  args 
)

Definition at line 1523 of file snat.c.

+ Here is the call graph for this function:

u8* format_snat_user ( u8 s,
va_list *  args 
)

Definition at line 1483 of file snat.c.

+ Here is the call graph for this function:

static void increment_v4_address ( ip4_address_t a)
static

Definition at line 295 of file snat.c.

+ Here is the caller graph for this function:

static int is_snat_address_used_in_static_mapping ( snat_main_t sm,
ip4_address_t  addr 
)
static

Definition at line 217 of file snat.c.

+ Here is the caller graph for this function:

static void plugin_custom_dump_configure ( snat_main_t sm)
static

Definition at line 972 of file snat.c.

+ Here is the caller graph for this function:

static void send_snat_address_details ( snat_address_t a,
unix_shared_memory_queue_t q,
u32  context 
)
static

Definition at line 662 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void send_snat_interface_details ( snat_interface_t i,
unix_shared_memory_queue_t q,
u32  context 
)
static

Definition at line 738 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void send_snat_static_mapping_details ( snat_static_mapping_t m,
unix_shared_memory_queue_t q,
u32  context 
)
static

Definition at line 837 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static clib_error_t* show_snat_command_fn ( vlib_main_t vm,
unformat_input_t input,
vlib_cli_command_t cmd 
)
static

Definition at line 1542 of file snat.c.

+ Here is the call graph for this function:

void snat_add_address ( snat_main_t sm,
ip4_address_t addr 
)

Definition at line 201 of file snat.c.

+ Here is the caller graph for this function:

int snat_add_static_mapping ( ip4_address_t  l_addr,
ip4_address_t  e_addr,
u16  l_port,
u16  e_port,
u32  vrf_id,
int  addr_only,
int  is_add 
)

Add static mapping.

Create static mapping between local addr+port and external addr+port.

Parameters
l_addrLocal IPv4 address.
e_addrExternal IPv4 address.
l_portLocal port number.
e_portExternal port number.
vrf_idVRF ID.
addr_onlyIf 0 address port and pair mapping, otherwise address only.
is_addIf 0 delete static mapping, otherwise add.
Returns

Definition at line 318 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int snat_alloc_outside_address_and_port ( snat_main_t sm,
snat_session_key_t k,
u32 address_indexp 
)

Definition at line 1089 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static clib_error_t* snat_config ( vlib_main_t vm,
unformat_input_t input 
)
static

Definition at line 1363 of file snat.c.

+ Here is the call graph for this function:

int snat_del_address ( snat_main_t sm,
ip4_address_t  addr 
)

Definition at line 230 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static clib_error_t* snat_feature_command_fn ( vlib_main_t vm,
unformat_input_t input,
vlib_cli_command_t cmd 
)
static

Definition at line 1211 of file snat.c.

+ Here is the call graph for this function:

void snat_free_outside_address_and_port ( snat_main_t sm,
snat_session_key_t k,
u32  address_index 
)

Definition at line 1010 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static clib_error_t* snat_init ( vlib_main_t vm)
static

Definition at line 981 of file snat.c.

+ Here is the call graph for this function:

static int snat_interface_add_del ( u32  sw_if_index,
u8  is_inside,
int  is_del 
)
static

Definition at line 539 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static clib_error_t* snat_plugin_api_hookup ( vlib_main_t vm)
static

Definition at line 955 of file snat.c.

+ Here is the caller graph for this function:

int snat_static_mapping_match ( snat_main_t sm,
snat_session_key_t  match,
snat_session_key_t mapping,
u8  by_external 
)

Match SNAT static mapping.

Parameters
smSNAT main.
matchAddress and port to match.
mappingExternal or local address and port of the matched mapping.
by_externalIf 0 match by local address otherwise match by external address.
Returns
0 if match found otherwise 1.

Definition at line 1039 of file snat.c.

+ Here is the caller graph for this function:

static void vl_api_snat_add_address_range_t_handler ( vl_api_snat_add_address_range_t mp)
static

Definition at line 592 of file snat.c.

+ Here is the call graph for this function:

static void* vl_api_snat_add_address_range_t_print ( vl_api_snat_add_address_range_t mp,
void *  handle 
)
static

Definition at line 647 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void vl_api_snat_add_static_mapping_t_handler ( vl_api_snat_add_static_mapping_t mp)
static

Definition at line 782 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void* vl_api_snat_add_static_mapping_t_print ( vl_api_snat_add_static_mapping_t mp,
void *  handle 
)
static

Definition at line 815 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void vl_api_snat_address_dump_t_handler ( vl_api_snat_address_dump_t mp)
static

Definition at line 679 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void* vl_api_snat_address_dump_t_print ( vl_api_snat_address_dump_t mp,
void *  handle 
)
static

Definition at line 694 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void vl_api_snat_control_ping_t_handler ( vl_api_snat_control_ping_t mp)
static

Definition at line 887 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void* vl_api_snat_control_ping_t_print ( vl_api_snat_control_ping_t mp,
void *  handle 
)
static

Definition at line 900 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void vl_api_snat_interface_add_del_feature_t_handler ( vl_api_snat_interface_add_del_feature_t mp)
static

Definition at line 705 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void* vl_api_snat_interface_add_del_feature_t_print ( vl_api_snat_interface_add_del_feature_t mp,
void *  handle 
)
static

Definition at line 723 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void vl_api_snat_interface_dump_t_handler ( vl_api_snat_interface_dump_t mp)
static

Definition at line 755 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void* vl_api_snat_interface_dump_t_print ( vl_api_snat_interface_dump_t mp,
void *  handle 
)
static

Definition at line 772 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void vl_api_snat_show_config_t_handler ( vl_api_snat_show_config_t mp)
static

Definition at line 911 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void* vl_api_snat_show_config_t_print ( vl_api_snat_show_config_t mp,
void *  handle 
)
static

Definition at line 933 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void vl_api_snat_static_mapping_dump_t_handler ( vl_api_snat_static_mapping_dump_t mp)
static

Definition at line 859 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void* vl_api_snat_static_mapping_dump_t_print ( vl_api_snat_static_mapping_dump_t mp,
void *  handle 
)
static

Definition at line 876 of file snat.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

clib_error_t* vlib_plugin_register ( vlib_main_t vm,
vnet_plugin_handoff_t h,
int  from_early_init 
)

Definition at line 126 of file snat.c.

VNET_IP4_UNICAST_FEATURE_INIT ( ip4_snat_in2out  ,
static   
)
VNET_IP4_UNICAST_FEATURE_INIT ( ip4_snat_out2in  ,
static   
)
VNET_IP4_UNICAST_FEATURE_INIT ( ip4_snat_in2out_fast  ,
static   
)
VNET_IP4_UNICAST_FEATURE_INIT ( ip4_snat_out2in_fast  ,
static   
)

Variable Documentation

vlib_cli_command_t add_address_command
static
Initial value:
= {
.path = "snat add address",
.short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] [del]",
}
static clib_error_t * add_address_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: snat.c:1128

(constructor) VLIB_CLI_COMMAND (add_address_command)

Definition at line 1204 of file snat.c.

vlib_cli_command_t add_static_mapping_command
static
Initial value:
= {
.path = "snat add static mapping",
.short_help =
"snat add static mapping local <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
}
static clib_error_t * add_static_mapping_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: snat.c:1277

(constructor) VLIB_CLI_COMMAND (add_static_mapping_command)

Definition at line 1355 of file snat.c.

vlib_cli_command_t set_interface_snat_command
static
Initial value:
= {
.path = "set interface snat",
.short_help = "set interface snat in <intfc> out <intfc> [del]",
}
static clib_error_t * snat_feature_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: snat.c:1211

(constructor) VLIB_CLI_COMMAND (set_interface_snat_command)

Definition at line 1270 of file snat.c.

vlib_cli_command_t show_snat_command
static
Initial value:
= {
.path = "show snat",
.short_help = "show snat",
.function = show_snat_command_fn,
}
static clib_error_t * show_snat_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: snat.c:1542

(constructor) VLIB_CLI_COMMAND (show_snat_command)

Definition at line 1631 of file snat.c.

snat_main_t snat_main

Definition at line 27 of file snat.c.