-static clib_error_t *
-add_lb_static_mapping_command_fn (vlib_main_t * vm,
- unformat_input_t * input,
- vlib_cli_command_t * cmd)
-{
- unformat_input_t _line_input, *line_input = &_line_input;
- clib_error_t * error = 0;
- ip4_address_t l_addr, e_addr;
- u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
- int is_add = 1;
- int rv;
- snat_protocol_t proto;
- u8 proto_set = 0;
- nat44_lb_addr_port_t *locals = 0, local;
-
- /* Get a line of input. */
- if (!unformat_user (input, unformat_line_input, line_input))
- return 0;
-
- while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
- {
- if (unformat (line_input, "local %U:%u probability %u",
- unformat_ip4_address, &l_addr, &l_port, &probability))
- {
- memset (&local, 0, sizeof (local));
- local.addr = l_addr;
- local.port = (u16) l_port;
- local.probability = (u8) probability;
- vec_add1 (locals, local);
- }
- else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
- &e_addr, &e_port))
- ;
- else if (unformat (line_input, "vrf %u", &vrf_id))
- ;
- else if (unformat (line_input, "protocol %U", unformat_snat_protocol,
- &proto))
- proto_set = 1;
- else if (unformat (line_input, "del"))
- is_add = 0;
- else
- {
- error = clib_error_return (0, "unknown input: '%U'",
- format_unformat_error, line_input);
- goto done;
- }
- }
-
- if (vec_len (locals) < 2)
- {
- error = clib_error_return (0, "at least two local must be set");
- goto done;
- }
-
- if (!proto_set)
- {
- error = clib_error_return (0, "missing protocol");
- goto done;
- }
-
- rv = nat44_add_del_lb_static_mapping (e_addr, (u16) e_port, proto, vrf_id,
- locals, is_add);
-
- switch (rv)
- {
- case VNET_API_ERROR_INVALID_VALUE:
- error = clib_error_return (0, "External port already in use.");
- goto done;
- case VNET_API_ERROR_NO_SUCH_ENTRY:
- if (is_add)
- error = clib_error_return (0, "External addres must be allocated.");
- else
- error = clib_error_return (0, "Mapping not exist.");
- goto done;
- case VNET_API_ERROR_VALUE_EXIST:
- error = clib_error_return (0, "Mapping already exist.");
- goto done;
- default:
- break;
- }
-
-done:
- unformat_free (line_input);
- vec_free (locals);
-
- return error;
-}
-
-VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
- .path = "nat44 add load-balancing static mapping",
- .function = add_lb_static_mapping_command_fn,
- .short_help =
- "nat44 add load-balancing static mapping protocol tcp|udp external <addr>:<port> local <addr>:<port> probability <n> [vrf <table-id>] [del]",
-};
-
-static clib_error_t *
-set_workers_command_fn (vlib_main_t * vm,
- unformat_input_t * input,
- vlib_cli_command_t * cmd)
-{
- unformat_input_t _line_input, *line_input = &_line_input;
- uword *bitmap = 0;
- int rv = 0;
- clib_error_t *error = 0;
-
- /* Get a line of input. */
- if (!unformat_user (input, unformat_line_input, line_input))
- return 0;
-
- while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
- {
- if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
- ;
- else
- {
- error = clib_error_return (0, "unknown input '%U'",
- format_unformat_error, line_input);
- goto done;
- }
- }
-
- if (bitmap == 0)
- {
- error = clib_error_return (0, "List of workers must be specified.");
- goto done;
- }
-
- rv = snat_set_workers(bitmap);
-
- clib_bitmap_free (bitmap);
-
- switch (rv)
- {
- case VNET_API_ERROR_INVALID_WORKER:
- error = clib_error_return (0, "Invalid worker(s).");
- goto done;
- case VNET_API_ERROR_FEATURE_DISABLED:
- error = clib_error_return (0,
- "Supported only if 2 or more workes available.");
- goto done;
- default:
- break;
- }
-
-done:
- unformat_free (line_input);
-
- return error;
-}
-
-/*?
- * @cliexpar
- * @cliexstart{set snat workers}
- * Set NAT workers if 2 or more workers available, use:
- * vpp# set snat workers 0-2,5
- * @cliexend
-?*/
-VLIB_CLI_COMMAND (set_workers_command, static) = {
- .path = "set nat workers",
- .function = set_workers_command_fn,
- .short_help =
- "set nat workers <workers-list>",
-};
-
-static clib_error_t *
-snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
- unformat_input_t * input,
- vlib_cli_command_t * cmd)
-{
- unformat_input_t _line_input, *line_input = &_line_input;
- u32 domain_id = 0;
- u32 src_port = 0;
- u8 enable = 1;
- int rv = 0;
- clib_error_t *error = 0;
-
- /* Get a line of input. */
- if (!unformat_user (input, unformat_line_input, line_input))
- return 0;
-
- while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
- {
- if (unformat (line_input, "domain %d", &domain_id))
- ;
- else if (unformat (line_input, "src-port %d", &src_port))
- ;
- else if (unformat (line_input, "disable"))
- enable = 0;
- else
- {
- error = clib_error_return (0, "unknown input '%U'",
- format_unformat_error, line_input);
- goto done;
- }
- }
-
- rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
-
- if (rv)
- {
- error = clib_error_return (0, "ipfix logging enable failed");
- goto done;
- }
-
-done:
- unformat_free (line_input);
-
- return error;
-}
-
-/*?
- * @cliexpar
- * @cliexstart{snat ipfix logging}
- * To enable NAT IPFIX logging use:
- * vpp# nat ipfix logging
- * To set IPFIX exporter use:
- * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
- * @cliexend
-?*/
-VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
- .path = "nat ipfix logging",
- .function = snat_ipfix_logging_enable_disable_command_fn,
- .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
-};
-
-static u32
-snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
-{
- snat_main_t *sm = &snat_main;
- u32 next_worker_index = 0;
- u32 hash;
-
- next_worker_index = sm->first_worker_index;
- hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
- (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >>24);
-
- if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
- next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
- else
- next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
-
- return next_worker_index;
-}
-
-static u32
-snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
-{
- snat_main_t *sm = &snat_main;
- udp_header_t *udp;
- u16 port;
- snat_session_key_t m_key;
- clib_bihash_kv_8_8_t kv, value;
- snat_static_mapping_t *m;
- nat_ed_ses_key_t key;
- clib_bihash_kv_16_8_t s_kv, s_value;
- snat_main_per_thread_data_t *tsm;
- snat_session_t *s;
- int i;
- u32 proto;
-
- /* first try static mappings without port */
- if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
- {
- m_key.addr = ip0->dst_address;
- m_key.port = 0;
- m_key.protocol = 0;
- m_key.fib_index = rx_fib_index0;
- kv.key = m_key.as_u64;
- if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
- {
- m = pool_elt_at_index (sm->static_mappings, value.value);
- return m->worker_index;
- }
- }
-
- proto = ip_proto_to_snat_proto (ip0->protocol);
- udp = ip4_next_header (ip0);
- port = udp->dst_port;