aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/ping/ping_api.c
blob: 5578fa560f2f0d4127cd997787116db05d040cc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 *------------------------------------------------------------------
 * Copyright (c) 2023 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vlib/pci/pci.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/format_fns.h>
#include <vnet/ip/ip_types_api.h>

#include <vlibapi/api.h>
#include <vlibmemory/api.h>

#include <ping/ping.h>

/* define message IDs */
#include <ping/ping.api_enum.h>
#include <ping/ping.api_types.h>

#define REPLY_MSG_ID_BASE pm->msg_id_base
#include <vlibapi/api_helper_macros.h>

static void
ping_api_send_ping_event (vl_api_want_ping_finished_events_t *mp,
			  u32 request_count, u32 reply_count)
{
  ping_main_t *pm = &ping_main;

  vl_api_registration_t *rp;
  rp = vl_api_client_index_to_registration (mp->client_index);

  vl_api_ping_finished_event_t *e = vl_msg_api_alloc (sizeof (*e));
  clib_memset (e, 0, sizeof (*e));

  e->_vl_msg_id = htons (VL_API_PING_FINISHED_EVENT + pm->msg_id_base);
  e->request_count = htonl (request_count);
  e->reply_count = htonl (reply_count);

  vl_api_send_msg (rp, (u8 *) e);
}

void
vl_api_want_ping_finished_events_t_handler (
  vl_api_want_ping_finished_events_t *mp)
{
  vlib_main_t *vm = vlib_get_main ();
  ping_main_t *pm = &ping_main;
  vl_api_want_ping_finished_events_reply_t *rmp;

  uword curr_proc = vlib_current_process (vm);

  u16 icmp_id;
  static u32 rand_seed = 0;

  if (PREDICT_FALSE (!rand_seed))
    rand_seed = random_default_seed ();

  icmp_id = random_u32 (&rand_seed) & 0xffff;

  while (~0 != get_cli_process_id_by_icmp_id_mt (vm, icmp_id))
    icmp_id++;

  set_cli_process_id_by_icmp_id_mt (vm, icmp_id, curr_proc);

  int rv = 0;
  u32 request_count = 0;
  u32 reply_count = 0;

  u32 table_id = 0;
  ip_address_t dst_addr = { 0 };
  u32 sw_if_index = ~0;
  f64 ping_interval = clib_net_to_host_f64 (mp->interval);
  u32 ping_repeat = ntohl (mp->repeat);
  u32 data_len = PING_DEFAULT_DATA_LEN;
  u32 ping_burst = 1;
  u32 verbose = 0;
  ip_address_decode2 (&mp->address, &dst_addr);

  vl_api_registration_t *rp;
  rp = vl_api_client_index_to_registration (mp->client_index);

  rmp = vl_msg_api_alloc (sizeof (*rmp));
  rmp->_vl_msg_id =
    htons ((VL_API_WANT_PING_FINISHED_EVENTS_REPLY) + (REPLY_MSG_ID_BASE));
  rmp->context = mp->context;
  rmp->retval = ntohl (rv);
  vl_api_send_msg (rp, (u8 *) rmp);

  int i;
  send_ip46_ping_result_t res = SEND_PING_OK;
  for (i = 1; i <= ping_repeat; i++)
    {
      f64 sleep_interval;
      f64 time_ping_sent = vlib_time_now (vm);

      if (dst_addr.version == AF_IP4)
	res = send_ip4_ping (vm, table_id, &dst_addr.ip.ip4, sw_if_index, i,
			     icmp_id, data_len, ping_burst, verbose);
      else
	res = send_ip6_ping (vm, table_id, &dst_addr.ip.ip6, sw_if_index, i,
			     icmp_id, data_len, ping_burst, verbose);

      if (SEND_PING_OK == res)
	request_count += 1;
      else
	continue;

      while ((sleep_interval =
		time_ping_sent + ping_interval - vlib_time_now (vm)) > 0.0)
	{
	  uword event_type;
	  vlib_process_wait_for_event_or_clock (vm, sleep_interval);
	  event_type = vlib_process_get_events (vm, 0);

	  if (event_type == ~0)
	    break;

	  if (event_type == PING_RESPONSE_IP4 ||
	      event_type == PING_RESPONSE_IP6)
	    reply_count += 1;
	}
    }

  ping_api_send_ping_event (mp, request_count, reply_count);

  clear_cli_process_id_by_icmp_id_mt (vm, icmp_id);
}

/* set tup the API message handling tables */
#include <ping/ping.api.c>

clib_error_t *
ping_plugin_api_hookup (vlib_main_t *vm)
{
  ping_main_t *pm = &ping_main;

  /* ask for a correctly-sized block of API message decode slots */
  pm->msg_id_base = setup_message_id_table ();

  return 0;
}