arping: add arping command
[vpp.git] / src / plugins / arping / arping_test.c
1 /*
2  * arping VAT support
3  *
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:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <inttypes.h>
19
20 #include <vat/vat.h>
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
23
24 #include <vppinfra/error.h>
25 #include <arping/arping.h>
26
27 #define __plugin_msg_base arping_test_main.msg_id_base
28 #include <vlibapi/vat_helper_macros.h>
29
30 /* declare message IDs */
31 #include <vnet/format_fns.h>
32 #include <arping/arping.api_enum.h>
33 #include <arping/arping.api_types.h>
34 #include <vpp/api/vpe.api_types.h>
35 #include <vnet/ip/ip_types_api.h>
36
37 typedef struct
38 {
39   /* API message ID base */
40   u16 msg_id_base;
41   u32 ping_id;
42   vat_main_t *vat_main;
43 } arping_test_main_t;
44
45 arping_test_main_t arping_test_main;
46
47 /* arping request API */
48 static int
49 api_arping (vat_main_t *vam)
50 {
51   vl_api_arping_t *mp;
52   arping_args_t args = { 0 };
53   int ret;
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;
59
60   args.repeat = ARPING_DEFAULT_REPEAT;
61   args.interval = ARPING_DEFAULT_INTERVAL;
62   args.sw_if_index = ~0;
63
64   if (unformat (input, "gratuitous"))
65     args.is_garp = 1;
66
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;
71   else
72     {
73       errmsg ("expecting IP4/IP6 address `%U'. Usage: arping [gratuitous] "
74               "<addr> <intf> [repeat <count>] [interval <secs>]",
75               format_unformat_error, input);
76       return -99;
77     }
78
79   if (!unformat_user (input, unformat_vnet_sw_interface, vnm,
80                       &args.sw_if_index))
81     {
82       errmsg ("unknown interface `%U'", format_unformat_error, input);
83       return -99;
84     }
85
86   /* parse the rest of the parameters  in a cycle */
87   while (!unformat_eof (input, NULL))
88     {
89       if (unformat (input, "interval"))
90         {
91           if (!unformat (input, "%f", &interval))
92             {
93               errmsg ("expecting interval (floating point number) got `%U'",
94                       format_unformat_error, input);
95               return -99;
96             }
97           args.interval = interval;
98         }
99       else if (unformat (input, "repeat"))
100         {
101           if (!unformat (input, "%u", &args.repeat))
102             {
103               errmsg ("expecting repeat count but got `%U'",
104                       format_unformat_error, input);
105               return -99;
106             }
107         }
108       else
109         {
110           errmsg ("unknown input `%U'", format_unformat_error, input);
111           return -99;
112         }
113     }
114
115   M (ARPING, mp);
116
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);
122
123   S (mp);
124
125   /* Use a control ping for synchronization */
126   if (!atm->ping_id)
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;
131
132   fformat (vam->ofp, "Sending ping id=%d\n", atm->ping_id);
133
134   vam->result_ready = 0;
135   S (mp_ping);
136
137   W (ret);
138
139   return ret;
140 }
141
142 /* arping-create reply handler */
143 static void
144 vl_api_arping_reply_t_handler (vl_api_arping_reply_t *mp)
145 {
146   vat_main_t *vam = arping_test_main.vat_main;
147   i32 retval = ntohl (mp->retval);
148
149   if (retval == 0)
150     {
151       fformat (vam->ofp, "arping request reply count = %d\n",
152                ntohl (mp->reply_count));
153     }
154
155   vam->retval = retval;
156   vam->result_ready = 1;
157 }
158
159 #include <arping/arping.api_test.c>
160
161 /*
162  * fd.io coding-style-patch-verification: ON
163  *
164  * Local Variables:
165  * eval: (c-set-style "gnu")
166  * End:
167  */