summaryrefslogtreecommitdiffstats
path: root/src/plugins/arping/arping_test.c
blob: 7cd85912bd251e591ecee26c52dbbd54790b75cc (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * arping VAT support
 *
 * Copyright (c) 2021 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 <inttypes.h>

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

#include <vppinfra/error.h>
#include <arping/arping.h>

#define __plugin_msg_base arping_test_main.msg_id_base
#include <vlibapi/vat_helper_macros.h>
#include <vlibmemory/vlib.api_types.h>

/* declare message IDs */
#include <vnet/format_fns.h>
#include <arping/arping.api_enum.h>
#include <arping/arping.api_types.h>
#include <vnet/ip/ip_types_api.h>

typedef struct
{
  /* API message ID base */
  u16 msg_id_base;
  u32 ping_id;
  vat_main_t *vat_main;
} arping_test_main_t;

arping_test_main_t arping_test_main;

/* arping request API */
static int
api_arping (vat_main_t *vam)
{
  vl_api_arping_t *mp;
  arping_args_t args = { 0 };
  int ret;
  unformat_input_t *input = vam->input;
  f64 interval = ARPING_DEFAULT_INTERVAL;
  vl_api_control_ping_t *mp_ping;
  arping_test_main_t *atm = &arping_test_main;

  args.repeat = ARPING_DEFAULT_REPEAT;
  args.interval = ARPING_DEFAULT_INTERVAL;
  args.sw_if_index = ~0;

  if (unformat (input, "gratuitous"))
    args.is_garp = 1;

  if (unformat (input, "%U", unformat_ip4_address, &args.address.ip.ip4))
    args.address.version = AF_IP4;
  else if (unformat (input, "%U", unformat_ip6_address, &args.address.ip.ip6))
    args.address.version = AF_IP6;
  else
    {
      errmsg ("expecting IP4/IP6 address `%U'. Usage: arping [gratuitous] "
	      "<addr> <intf> [repeat <count>] [interval <secs>]",
	      format_unformat_error, input);
      return -99;
    }

  if (!unformat_user (input, api_unformat_sw_if_index, vam, &args.sw_if_index))
    {
      errmsg ("unknown interface `%U'", format_unformat_error, input);
      return -99;
    }

  /* parse the rest of the parameters  in a cycle */
  while (!unformat_eof (input, NULL))
    {
      if (unformat (input, "interval"))
	{
	  if (!unformat (input, "%f", &interval))
	    {
	      errmsg ("expecting interval (floating point number) got `%U'",
		      format_unformat_error, input);
	      return -99;
	    }
	  args.interval = interval;
	}
      else if (unformat (input, "repeat"))
	{
	  if (!unformat (input, "%u", &args.repeat))
	    {
	      errmsg ("expecting repeat count but got `%U'",
		      format_unformat_error, input);
	      return -99;
	    }
	}
      else
	{
	  errmsg ("unknown input `%U'", format_unformat_error, input);
	  return -99;
	}
    }

  M (ARPING, mp);

  mp->interval = clib_host_to_net_f64 (args.interval);
  mp->repeat = clib_host_to_net_u32 (args.repeat);
  mp->is_garp = args.is_garp;
  mp->sw_if_index = clib_host_to_net_u32 (args.sw_if_index);
  ip_address_encode2 (&args.address, &mp->address);

  S (mp);

  /* Use a control ping for synchronization */
  if (!atm->ping_id)
    atm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
  mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
  mp_ping->_vl_msg_id = htons (atm->ping_id);
  mp_ping->client_index = vam->my_client_index;

  fformat (vam->ofp, "Sending ping id=%d\n", atm->ping_id);

  vam->result_ready = 0;
  S (mp_ping);

  W (ret);

  return ret;
}

/* arping-create reply handler */
static void
vl_api_arping_reply_t_handler (vl_api_arping_reply_t *mp)
{
  vat_main_t *vam = arping_test_main.vat_main;
  i32 retval = ntohl (mp->retval);

  if (retval == 0)
    {
      fformat (vam->ofp, "arping request reply count = %d\n",
	       ntohl (mp->reply_count));
    }

  vam->retval = retval;
  vam->result_ready = 1;
}

static int
api_arping_acd (vat_main_t *vam)
{
  // NOT YET IMPLEMENTED
  return -99;
}

static void
vl_api_arping_acd_reply_t_handler (vl_api_arping_reply_t *mp)
{
  // NOT YET IMPLEMENTED
}

#include <arping/arping.api_test.c>

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */