arping: api to return responder mac address
[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 #include <vlibmemory/vlib.api_types.h>
30
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>
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   f64 interval = ARPING_DEFAULT_INTERVAL;
56   vl_api_control_ping_t *mp_ping;
57   arping_test_main_t *atm = &arping_test_main;
58
59   args.repeat = ARPING_DEFAULT_REPEAT;
60   args.interval = ARPING_DEFAULT_INTERVAL;
61   args.sw_if_index = ~0;
62
63   if (unformat (input, "gratuitous"))
64     args.is_garp = 1;
65
66   if (unformat (input, "%U", unformat_ip4_address, &args.address.ip.ip4))
67     args.address.version = AF_IP4;
68   else if (unformat (input, "%U", unformat_ip6_address, &args.address.ip.ip6))
69     args.address.version = AF_IP6;
70   else
71     {
72       errmsg ("expecting IP4/IP6 address `%U'. Usage: arping [gratuitous] "
73               "<addr> <intf> [repeat <count>] [interval <secs>]",
74               format_unformat_error, input);
75       return -99;
76     }
77
78   if (!unformat_user (input, api_unformat_sw_if_index, vam, &args.sw_if_index))
79     {
80       errmsg ("unknown interface `%U'", format_unformat_error, input);
81       return -99;
82     }
83
84   /* parse the rest of the parameters  in a cycle */
85   while (!unformat_eof (input, NULL))
86     {
87       if (unformat (input, "interval"))
88         {
89           if (!unformat (input, "%f", &interval))
90             {
91               errmsg ("expecting interval (floating point number) got `%U'",
92                       format_unformat_error, input);
93               return -99;
94             }
95           args.interval = interval;
96         }
97       else if (unformat (input, "repeat"))
98         {
99           if (!unformat (input, "%u", &args.repeat))
100             {
101               errmsg ("expecting repeat count but got `%U'",
102                       format_unformat_error, input);
103               return -99;
104             }
105         }
106       else
107         {
108           errmsg ("unknown input `%U'", format_unformat_error, input);
109           return -99;
110         }
111     }
112
113   M (ARPING, mp);
114
115   mp->interval = clib_host_to_net_f64 (args.interval);
116   mp->repeat = clib_host_to_net_u32 (args.repeat);
117   mp->is_garp = args.is_garp;
118   mp->sw_if_index = clib_host_to_net_u32 (args.sw_if_index);
119   ip_address_encode2 (&args.address, &mp->address);
120
121   S (mp);
122
123   /* Use a control ping for synchronization */
124   if (!atm->ping_id)
125     atm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
126   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
127   mp_ping->_vl_msg_id = htons (atm->ping_id);
128   mp_ping->client_index = vam->my_client_index;
129
130   fformat (vam->ofp, "Sending ping id=%d\n", atm->ping_id);
131
132   vam->result_ready = 0;
133   S (mp_ping);
134
135   W (ret);
136
137   return ret;
138 }
139
140 /* arping-create reply handler */
141 static void
142 vl_api_arping_reply_t_handler (vl_api_arping_reply_t *mp)
143 {
144   vat_main_t *vam = arping_test_main.vat_main;
145   i32 retval = ntohl (mp->retval);
146
147   if (retval == 0)
148     {
149       fformat (vam->ofp, "arping request reply count = %d\n",
150                ntohl (mp->reply_count));
151     }
152
153   vam->retval = retval;
154   vam->result_ready = 1;
155 }
156
157 static int
158 api_arping_acd (vat_main_t *vam)
159 {
160   // NOT YET IMPLEMENTED
161   return -99;
162 }
163
164 static void
165 vl_api_arping_acd_reply_t_handler (vl_api_arping_reply_t *mp)
166 {
167   // NOT YET IMPLEMENTED
168 }
169
170 #include <arping/arping.api_test.c>
171
172 /*
173  * fd.io coding-style-patch-verification: ON
174  *
175  * Local Variables:
176  * eval: (c-set-style "gnu")
177  * End:
178  */