ping: Simple binary API for running ping based on events
[vpp.git] / src / plugins / ping / ping.c
index 2cc6b77..ca2fc1f 100644 (file)
@@ -99,70 +99,6 @@ format_ip46_ping_result (u8 * s, va_list * args)
  *
  */
 
-
-static_always_inline uword
-get_cli_process_id_by_icmp_id_mt (vlib_main_t * vm, u16 icmp_id)
-{
-  ping_main_t *pm = &ping_main;
-  uword cli_process_id = PING_CLI_UNKNOWN_NODE;
-  ping_run_t *pr;
-
-  clib_spinlock_lock_if_init (&pm->ping_run_check_lock);
-  vec_foreach (pr, pm->active_ping_runs)
-  {
-    if (pr->icmp_id == icmp_id)
-      {
-       cli_process_id = pr->cli_process_id;
-       break;
-      }
-  }
-  clib_spinlock_unlock_if_init (&pm->ping_run_check_lock);
-  return cli_process_id;
-}
-
-
-static_always_inline void
-set_cli_process_id_by_icmp_id_mt (vlib_main_t * vm, u16 icmp_id,
-                                 uword cli_process_id)
-{
-  ping_main_t *pm = &ping_main;
-  ping_run_t *pr;
-
-  clib_spinlock_lock_if_init (&pm->ping_run_check_lock);
-  vec_foreach (pr, pm->active_ping_runs)
-  {
-    if (pr->icmp_id == icmp_id)
-      {
-       pr->cli_process_id = cli_process_id;
-       goto have_found_and_set;
-      }
-  }
-  /* no such key yet - add a new one */
-  ping_run_t new_pr = {.icmp_id = icmp_id,.cli_process_id = cli_process_id };
-  vec_add1 (pm->active_ping_runs, new_pr);
-have_found_and_set:
-  clib_spinlock_unlock_if_init (&pm->ping_run_check_lock);
-}
-
-
-static_always_inline void
-clear_cli_process_id_by_icmp_id_mt (vlib_main_t * vm, u16 icmp_id)
-{
-  ping_main_t *pm = &ping_main;
-  ping_run_t *pr;
-
-  clib_spinlock_lock_if_init (&pm->ping_run_check_lock);
-  vec_foreach (pr, pm->active_ping_runs)
-  {
-    if (pr->icmp_id == icmp_id)
-      {
-       vec_del1 (pm->active_ping_runs, pr - pm->active_ping_runs);
-       break;
-      }
-  }
-  clib_spinlock_unlock_if_init (&pm->ping_run_check_lock);
-}
-
 static_always_inline int
 ip46_get_icmp_id_and_seq (vlib_main_t * vm, vlib_buffer_t * b0,
                          u16 * out_icmp_id, u16 * out_icmp_seq, int is_ip6)
@@ -662,15 +598,20 @@ ip6_icmp_echo_request (vlib_main_t *vm, vlib_node_runtime_t *node,
          ip0->hop_limit = im->host_config.ttl;
          ip1->hop_limit = im->host_config.ttl;
 
-         /* Determine the correct lookup fib indices... */
-         fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
-                               vnet_buffer (p0)->sw_if_index[VLIB_RX]);
-         vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index0;
-         /* Determine the correct lookup fib indices... */
-         fib_index1 = vec_elt (im->fib_index_by_sw_if_index,
-                               vnet_buffer (p1)->sw_if_index[VLIB_RX]);
-         vnet_buffer (p1)->sw_if_index[VLIB_TX] = fib_index1;
-
+         if (ip6_address_is_link_local_unicast (&ip0->src_address) &&
+             !ip6_address_is_link_local_unicast (&ip0->dst_address))
+           {
+             fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
+                                   vnet_buffer (p0)->sw_if_index[VLIB_RX]);
+             vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index0;
+           }
+         if (ip6_address_is_link_local_unicast (&ip1->src_address) &&
+             !ip6_address_is_link_local_unicast (&ip1->dst_address))
+           {
+             fib_index1 = vec_elt (im->fib_index_by_sw_if_index,
+                                   vnet_buffer (p1)->sw_if_index[VLIB_RX]);
+             vnet_buffer (p1)->sw_if_index[VLIB_TX] = fib_index1;
+           }
          p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
          p1->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
 
@@ -722,12 +663,19 @@ ip6_icmp_echo_request (vlib_main_t *vm, vlib_node_runtime_t *node,
 
          ip0->hop_limit = im->host_config.ttl;
 
-         /* if the packet is link local, we'll bounce through the link-local
-          * table with the RX interface correctly set */
-         fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
-                               vnet_buffer (p0)->sw_if_index[VLIB_RX]);
-         vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index0;
-
+         if (ip6_address_is_link_local_unicast (&ip0->src_address) &&
+             !ip6_address_is_link_local_unicast (&ip0->dst_address))
+           {
+             /* if original packet was to the link local, then the
+              * fib index is that of the LL table, we can't use that
+              * to foward the response if the new destination
+              * is global, so reset to the fib index of the link.
+              * In other case, the fib index we need has been written
+              * to the buffer already. */
+             fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
+                                   vnet_buffer (p0)->sw_if_index[VLIB_RX]);
+             vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index0;
+           }
          p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
          /* Verify speculative enqueue, maybe switch current next frame */
          vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
@@ -1217,9 +1165,8 @@ done:
   return err;
 }
 
-static send_ip46_ping_result_t
-send_ip6_ping (vlib_main_t * vm,
-              u32 table_id, ip6_address_t * pa6,
+send_ip46_ping_result_t
+send_ip6_ping (vlib_main_t *vm, u32 table_id, ip6_address_t *pa6,
               u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
               u32 burst, u8 verbose)
 {
@@ -1229,9 +1176,8 @@ send_ip6_ping (vlib_main_t * vm,
                         id_host, data_len, burst, verbose, 1 /* is_ip6 */ );
 }
 
-static send_ip46_ping_result_t
-send_ip4_ping (vlib_main_t * vm,
-              u32 table_id, ip4_address_t * pa4,
+send_ip46_ping_result_t
+send_ip4_ping (vlib_main_t *vm, u32 table_id, ip4_address_t *pa4,
               u32 sw_if_index, u16 seq_host, u16 id_host, u16 data_len,
               u32 burst, u8 verbose)
 {
@@ -1666,6 +1612,8 @@ ping_cli_init (vlib_main_t * vm)
   icmp6_register_type (vm, ICMP6_echo_request,
                       ip6_icmp_echo_request_node.index);
 
+  ping_plugin_api_hookup (vm);
+
   return 0;
 }