4 * Copyright (c) 2021 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
24 #include <vppinfra/error.h>
25 #include <arping/arping.h>
27 #define __plugin_msg_base arping_test_main.msg_id_base
28 #include <vlibapi/vat_helper_macros.h>
29 #include <vlibmemory/vlib.api_types.h>
31 /* declare message IDs */
32 #include <vnet/format_fns.h>
33 #include <arping/arping.api_enum.h>
34 #include <arping/arping.api_types.h>
35 #include <vnet/ip/ip_types_api.h>
39 /* API message ID base */
45 arping_test_main_t arping_test_main;
47 /* arping request API */
49 api_arping (vat_main_t *vam)
52 arping_args_t args = { 0 };
54 unformat_input_t *input = vam->input;
55 vnet_main_t *vnm = vnet_get_main ();
56 f64 interval = ARPING_DEFAULT_INTERVAL;
57 vl_api_control_ping_t *mp_ping;
58 arping_test_main_t *atm = &arping_test_main;
60 args.repeat = ARPING_DEFAULT_REPEAT;
61 args.interval = ARPING_DEFAULT_INTERVAL;
62 args.sw_if_index = ~0;
64 if (unformat (input, "gratuitous"))
67 if (unformat (input, "%U", unformat_ip4_address, &args.address.ip.ip4))
68 args.address.version = AF_IP4;
69 else if (unformat (input, "%U", unformat_ip6_address, &args.address.ip.ip6))
70 args.address.version = AF_IP6;
73 errmsg ("expecting IP4/IP6 address `%U'. Usage: arping [gratuitous] "
74 "<addr> <intf> [repeat <count>] [interval <secs>]",
75 format_unformat_error, input);
79 if (!unformat_user (input, unformat_vnet_sw_interface, vnm,
82 errmsg ("unknown interface `%U'", format_unformat_error, input);
86 /* parse the rest of the parameters in a cycle */
87 while (!unformat_eof (input, NULL))
89 if (unformat (input, "interval"))
91 if (!unformat (input, "%f", &interval))
93 errmsg ("expecting interval (floating point number) got `%U'",
94 format_unformat_error, input);
97 args.interval = interval;
99 else if (unformat (input, "repeat"))
101 if (!unformat (input, "%u", &args.repeat))
103 errmsg ("expecting repeat count but got `%U'",
104 format_unformat_error, input);
110 errmsg ("unknown input `%U'", format_unformat_error, input);
117 mp->interval = clib_host_to_net_f64 (args.interval);
118 mp->repeat = clib_host_to_net_u32 (args.repeat);
119 mp->is_garp = args.is_garp;
120 mp->sw_if_index = clib_host_to_net_u32 (args.sw_if_index);
121 ip_address_encode2 (&args.address, &mp->address);
125 /* Use a control ping for synchronization */
127 atm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
128 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
129 mp_ping->_vl_msg_id = htons (atm->ping_id);
130 mp_ping->client_index = vam->my_client_index;
132 fformat (vam->ofp, "Sending ping id=%d\n", atm->ping_id);
134 vam->result_ready = 0;
142 /* arping-create reply handler */
144 vl_api_arping_reply_t_handler (vl_api_arping_reply_t *mp)
146 vat_main_t *vam = arping_test_main.vat_main;
147 i32 retval = ntohl (mp->retval);
151 fformat (vam->ofp, "arping request reply count = %d\n",
152 ntohl (mp->reply_count));
155 vam->retval = retval;
156 vam->result_ready = 1;
159 #include <arping/arping.api_test.c>
162 * fd.io coding-style-patch-verification: ON
165 * eval: (c-set-style "gnu")