-/*
- * The next two routines address a longstanding script hemorrhoid.
- * Probing a v4 or v6 neighbor needs to appear to be synchronous,
- * or dependent route-adds will simply fail.
- */
-static clib_error_t *
-ip6_probe_neighbor_wait (vlib_main_t * vm, ip6_address_t * a, u32 sw_if_index,
- int retry_count)
-{
- vnet_main_t *vnm = vnet_get_main ();
- clib_error_t *e;
- int i;
- int resolved = 0;
- uword event_type;
- uword *event_data = 0;
-
- ASSERT (vlib_in_process_context (vm));
-
- if (retry_count > 0)
- vnet_register_ip6_neighbor_resolution_event
- (vnm, a, vlib_get_current_process (vm)->node_runtime.node_index,
- 1 /* event */ , 0 /* data */ );
-
- for (i = 0; i < retry_count; i++)
- {
- /* The interface may be down, etc. */
- e = ip6_probe_neighbor (vm, a, sw_if_index);
-
- if (e)
- return e;
-
- vlib_process_wait_for_event_or_clock (vm, 1.0);
- event_type = vlib_process_get_events (vm, &event_data);
- switch (event_type)
- {
- case 1: /* resolved... */
- vlib_cli_output (vm, "Resolved %U", format_ip6_address, a);
- resolved = 1;
- goto done;
-
- case ~0: /* timeout */
- break;
-
- default:
- clib_warning ("unknown event_type %d", event_type);
- }
- vec_reset_length (event_data);
- }
-
-done:
-
- if (!resolved)
- return clib_error_return (0, "Resolution failed for %U",
- format_ip6_address, a);
- return 0;
-}
-
-static clib_error_t *
-ip4_probe_neighbor_wait (vlib_main_t * vm, ip4_address_t * a, u32 sw_if_index,
- int retry_count)
-{
- vnet_main_t *vnm = vnet_get_main ();
- clib_error_t *e;
- int i;
- int resolved = 0;
- uword event_type;
- uword *event_data = 0;
-
- ASSERT (vlib_in_process_context (vm));
-
- if (retry_count > 0)
- vnet_register_ip4_arp_resolution_event
- (vnm, a, vlib_get_current_process (vm)->node_runtime.node_index,
- 1 /* event */ , 0 /* data */ );
-
- for (i = 0; i < retry_count; i++)
- {
- /* The interface may be down, etc. */
- e = ip4_probe_neighbor (vm, a, sw_if_index);
-
- if (e)
- return e;
-
- vlib_process_wait_for_event_or_clock (vm, 1.0);
- event_type = vlib_process_get_events (vm, &event_data);
- switch (event_type)
- {
- case 1: /* resolved... */
- vlib_cli_output (vm, "Resolved %U", format_ip4_address, a);
- resolved = 1;
- goto done;
-
- case ~0: /* timeout */
- break;
-
- default:
- clib_warning ("unknown event_type %d", event_type);
- }
- vec_reset_length (event_data);
- }
-
-done:
-
- vec_reset_length (event_data);
-
- if (!resolved)
- return clib_error_return (0, "Resolution failed for %U",
- format_ip4_address, a);
- return 0;
-}
-
-static clib_error_t *
-probe_neighbor_address (vlib_main_t * vm,
- unformat_input_t * input, vlib_cli_command_t * cmd)
-{
- vnet_main_t *vnm = vnet_get_main ();
- unformat_input_t _line_input, *line_input = &_line_input;
- ip4_address_t a4;
- ip6_address_t a6;
- clib_error_t *error = 0;
- u32 sw_if_index = ~0;
- int retry_count = 3;
- int is_ip4 = 1;
- int address_set = 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_user (line_input, unformat_vnet_sw_interface, vnm,
- &sw_if_index))
- ;
- else if (unformat (line_input, "retry %d", &retry_count))
- ;
-
- else if (unformat (line_input, "%U", unformat_ip4_address, &a4))
- address_set++;
- else if (unformat (line_input, "%U", unformat_ip6_address, &a6))
- {
- address_set++;
- is_ip4 = 0;
- }
- else
- {
- error = clib_error_return (0, "unknown input '%U'",
- format_unformat_error, line_input);
- goto done;
- }
- }
-
- if (sw_if_index == ~0)
- {
- error = clib_error_return (0, "Interface required, not set.");
- goto done;
- }
- if (address_set == 0)
- {
- error = clib_error_return (0, "ip address required, not set.");
- goto done;
- }
- if (address_set > 1)
- {
- error = clib_error_return (0, "Multiple ip addresses not supported.");
- goto done;
- }
-
- if (is_ip4)
- error = ip4_probe_neighbor_wait (vm, &a4, sw_if_index, retry_count);
- else
- error = ip6_probe_neighbor_wait (vm, &a6, sw_if_index, retry_count);
-
-done:
- unformat_free (line_input);
-
- return error;
-}
-
-/*?
- * The '<em>ip probe-neighbor</em>' command ARPs for IPv4 addresses or
- * attempts IPv6 neighbor discovery depending on the supplied IP address
- * format.
- *
- * @note This command will not immediately affect the indicated FIB; it
- * is not suitable for use in establishing a FIB entry prior to adding
- * recursive FIB entries. As in: don't use it in a script to probe a
- * gateway prior to adding a default route. It won't work. Instead,
- * configure a static ARP cache entry [see '<em>set ip arp</em>'], or
- * a static IPv6 neighbor [see '<em>set ip6 neighbor</em>'].
- *
- * @cliexpar
- * Example of probe for an IPv4 address:
- * @cliexcmd{ip probe-neighbor GigabitEthernet2/0/0 172.16.1.2}
-?*/
-/* *INDENT-OFF* */
-VLIB_CLI_COMMAND (ip_probe_neighbor_command, static) = {
- .path = "ip probe-neighbor",
- .function = probe_neighbor_address,
- .short_help = "ip probe-neighbor <interface> <ip4-addr> | <ip6-addr> [retry nn]",
- .is_mp_safe = 1,
-};
-/* *INDENT-ON* */
-
-clib_error_t *
-vnet_ip_container_proxy_add_del (vnet_ip_container_proxy_args_t * args)
-{
- u32 fib_index;
-
- if (!vnet_sw_interface_is_api_valid (vnet_get_main (), args->sw_if_index))
- return clib_error_return_code (0, VNET_API_ERROR_INVALID_INTERFACE, 0,
- "invalid sw_if_index");
-
- fib_index = fib_table_get_table_id_for_sw_if_index (args->prefix.fp_proto,
- args->sw_if_index);
- if (args->is_add)
- {
- dpo_id_t proxy_dpo = DPO_INVALID;
- l3_proxy_dpo_add_or_lock (fib_proto_to_dpo (args->prefix.fp_proto),
- args->sw_if_index, &proxy_dpo);
- fib_table_entry_special_dpo_add (fib_index,
- &args->prefix,
- FIB_SOURCE_PROXY,
- FIB_ENTRY_FLAG_EXCLUSIVE, &proxy_dpo);
- dpo_reset (&proxy_dpo);
- }
- else
- {
- fib_table_entry_special_remove (fib_index, &args->prefix,
- FIB_SOURCE_PROXY);
- }
- return 0;
-}
-
-u8
-ip_container_proxy_is_set (fib_prefix_t * pfx, u32 sw_if_index)
-{
- u32 fib_index;
- fib_node_index_t fei;
- const dpo_id_t *dpo;
- l3_proxy_dpo_t *l3p;
- load_balance_t *lb0;
-
- fib_index = fib_table_get_table_id_for_sw_if_index (pfx->fp_proto,
- sw_if_index);
- if (fib_index == ~0)
- return 0;
-
- fei = fib_table_lookup_exact_match (fib_index, pfx);
- if (fei == FIB_NODE_INDEX_INVALID)
- return 0;
-
- dpo = fib_entry_contribute_ip_forwarding (fei);
- lb0 = load_balance_get (dpo->dpoi_index);
- dpo = load_balance_get_bucket_i (lb0, 0);
- if (dpo->dpoi_type != DPO_L3_PROXY)
- return 0;
-
- l3p = l3_proxy_dpo_get (dpo->dpoi_index);
- return (l3p->l3p_sw_if_index == sw_if_index);
-}
-
-clib_error_t *
-ip_container_cmd (vlib_main_t * vm,
- unformat_input_t * main_input, vlib_cli_command_t * cmd)
-{
- unformat_input_t _line_input, *line_input = &_line_input;
- fib_prefix_t pfx;
- u32 is_del, addr_set = 0;
- vnet_main_t *vnm;
- u32 sw_if_index;
-
- vnm = vnet_get_main ();
- is_del = 0;
- sw_if_index = ~0;
- memset (&pfx, 0, sizeof (pfx));
-
- /* Get a line of input. */
- if (!unformat_user (main_input, unformat_line_input, line_input))
- return 0;
-
- while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
- {
- if (unformat (line_input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
- {
- pfx.fp_proto = FIB_PROTOCOL_IP4;
- pfx.fp_len = 32;
- addr_set = 1;
- }
- else if (unformat (line_input, "%U",
- unformat_ip6_address, &pfx.fp_addr.ip6))
- {
- pfx.fp_proto = FIB_PROTOCOL_IP6;
- pfx.fp_len = 128;
- addr_set = 1;
- }
- else if (unformat (line_input, "%U",
- unformat_vnet_sw_interface, vnm, &sw_if_index))
- ;
- else if (unformat (line_input, "del"))
- is_del = 1;
- else
- return (clib_error_return (0, "unknown input '%U'",
- format_unformat_error, line_input));
- }
-
- if (~0 == sw_if_index || !addr_set)
- {
- vlib_cli_output (vm, "interface and address must be set");
- return 0;
- }
-
- vnet_ip_container_proxy_args_t args = {
- .prefix = pfx,
- .sw_if_index = sw_if_index,
- .is_add = !is_del,
- };
- vnet_ip_container_proxy_add_del (&args);
- unformat_free (line_input);
- return (NULL);
-}
-
-/* *INDENT-OFF* */
-VLIB_CLI_COMMAND (ip_container_command_node, static) = {
- .path = "ip container",
- .function = ip_container_cmd,
- .short_help = "ip container <address> <interface>",
- .is_mp_safe = 1,
-};
-/* *INDENT-ON* */
-
-clib_error_t *
-show_ip_container_cmd_fn (vlib_main_t * vm, unformat_input_t * main_input,
- vlib_cli_command_t * cmd)
-{
- unformat_input_t _line_input, *line_input = &_line_input;
- vnet_main_t *vnm = vnet_get_main ();
- fib_prefix_t pfx;
- u32 sw_if_index = ~0;
- u8 has_proxy;
-
- if (!unformat_user (main_input, unformat_line_input, line_input))
- return 0;
- while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
- {
- if (unformat (line_input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
- {
- pfx.fp_proto = FIB_PROTOCOL_IP4;
- pfx.fp_len = 32;
- }
- else if (unformat (line_input, "%U",
- unformat_ip6_address, &pfx.fp_addr.ip6))
- {
- pfx.fp_proto = FIB_PROTOCOL_IP6;
- pfx.fp_len = 128;
- }
- else if (unformat (line_input, "%U",
- unformat_vnet_sw_interface, vnm, &sw_if_index))
- ;
- else
- return (clib_error_return (0, "unknown input '%U'",
- format_unformat_error, line_input));
- }
-
- if (~0 == sw_if_index)
- {
- vlib_cli_output (vm, "no interface");
- return (clib_error_return (0, "no interface"));
- }
-
- has_proxy = ip_container_proxy_is_set (&pfx, sw_if_index);
- vlib_cli_output (vm, "ip container proxy is: %s", has_proxy ? "on" : "off");
-
- unformat_free (line_input);
- return 0;
-}
-
-/* *INDENT-OFF* */
-VLIB_CLI_COMMAND (show_ip_container_command, static) = {
- .path = "show ip container",
- .function = show_ip_container_cmd_fn,
- .short_help = "show ip container <address> <interface>",
- .is_mp_safe = 1,
-};
-/* *INDENT-ON* */
-