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