fix udp_ping api naming error
[vpp.git] / src / plugins / ioam / udp-ping / udp_ping_test.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  *------------------------------------------------------------------
17  * udp_ping_test.c - test harness for udp ping plugin
18  *------------------------------------------------------------------
19  */
20
21 #include <vat/vat.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24
25 #include <vppinfra/error.h>
26 #include <vnet/ip/ip.h>
27
28 /* Declare message IDs */
29 #include <ioam/udp-ping/udp_ping_msg_enum.h>
30
31 /* define message structures */
32 #define vl_typedefs
33 #include <ioam/udp-ping/udp_ping_all_api_h.h>
34 #undef vl_typedefs
35
36 /* declare message handlers for each api */
37
38 #define vl_endianfun            /* define message structures */
39 #include <ioam/udp-ping/udp_ping_all_api_h.h>
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...)
44 #define vl_printfun
45 #include <ioam/udp-ping/udp_ping_all_api_h.h>
46 #undef vl_printfun
47
48 /* Get the API version number. */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <ioam/udp-ping/udp_ping_all_api_h.h>
51 #undef vl_api_version
52
53
54 typedef struct
55 {
56   /* API message ID base */
57   u16 msg_id_base;
58   vat_main_t *vat_main;
59 } udp_ping_test_main_t;
60
61 udp_ping_test_main_t udp_ping_test_main;
62
63 #define foreach_standard_reply_retval_handler     \
64 _(udp_ping_add_del_reply)                          \
65 _(udp_ping_export_reply)
66
67 #define _(n)                                            \
68     static void vl_api_##n##_t_handler                  \
69     (vl_api_##n##_t * mp)                               \
70     {                                                   \
71         vat_main_t * vam = udp_ping_test_main.vat_main;   \
72         i32 retval = ntohl(mp->retval);                 \
73         if (vam->async_mode) {                          \
74             vam->async_errors += (retval < 0);          \
75         } else {                                        \
76             vam->retval = retval;                       \
77             vam->result_ready = 1;                      \
78         }                                               \
79     }
80 foreach_standard_reply_retval_handler;
81 #undef _
82
83 /*
84  * Table of message reply handlers, must include boilerplate handlers
85  * we just generated
86  */
87 #define foreach_vpe_api_reply_msg                                       \
88 _(UDP_PING_ADD_DEL_REPLY, udp_ping_add_del_reply)                         \
89 _(UDP_PING_EXPORT_REPLY, udp_ping_export_reply)                         \
90
91
92 /* M: construct, but don't yet send a message */
93
94 #define M(T,t)                                                  \
95 do {                                                            \
96     vam->result_ready = 0;                                      \
97     mp = vl_msg_api_alloc(sizeof(*mp));                         \
98     memset (mp, 0, sizeof (*mp));                               \
99     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
100     mp->client_index = vam->my_client_index;                    \
101 } while(0);
102
103 /* S: send a message */
104 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
105
106 /* W: wait for results, with timeout */
107 #define W                                       \
108 do {                                            \
109     timeout = vat_time_now (vam) + 5.0;         \
110                                                 \
111     while (vat_time_now (vam) < timeout) {      \
112         if (vam->result_ready == 1) {           \
113             return (vam->retval);               \
114         }                                       \
115     }                                           \
116     return -99;                                 \
117 } while(0);
118
119 static int
120 api_udp_ping_add_del (vat_main_t * vam)
121 {
122   udp_ping_test_main_t *sm = &udp_ping_test_main;
123   unformat_input_t *input = vam->input;
124   vl_api_udp_ping_add_del_t *mp;
125   int rv = 0;
126   ip6_address_t dst, src;
127   u32 start_src_port, end_src_port;
128   u32 start_dst_port, end_dst_port;
129   u32 interval;
130   u8 is_disable = 0;
131   f64 timeout;
132
133   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
134     {
135       if (unformat (input, "src %U", unformat_ip6_address, &src))
136         ;
137       else if (unformat (input, "start-src-port %d", &start_src_port))
138         ;
139       else if (unformat (input, "end-src-port %d", &end_src_port))
140         ;
141       else if (unformat (input, "start-dst-port %d", &start_dst_port))
142         ;
143       else if (unformat (input, "end-dst-port %d", &end_dst_port))
144         ;
145       else if (unformat (input, "dst %U", unformat_ip6_address, &dst))
146         ;
147       else if (unformat (input, "interval %d", &interval))
148         ;
149       else if (unformat (input, "disable"))
150         is_disable = 1;
151       else
152         break;
153     }
154
155   M (UDP_PING_ADD_DEL, udp_ping_add);
156
157   clib_memcpy (mp->src_ip_address, &src, 16);
158   clib_memcpy (mp->dst_ip_address, &dst, 16);
159   mp->start_src_port = (u16) start_src_port;
160   mp->end_src_port = (u16) end_src_port;
161   mp->start_dst_port = (u16) start_dst_port;
162   mp->end_dst_port = (u16) end_dst_port;
163   mp->interval = (u16) interval;
164   mp->is_ipv4 = 0;
165   mp->dis = is_disable;
166
167   S;
168   W;
169
170   return (rv);
171 }
172
173 static int
174 api_udp_ping_export (vat_main_t * vam)
175 {
176   udp_ping_test_main_t *sm = &udp_ping_test_main;
177   unformat_input_t *input = vam->input;
178   vl_api_udp_ping_export_t *mp;
179   int rv = 0;
180   int is_add = 1;
181   f64 timeout;
182
183   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
184     {
185       if (unformat (input, "export"))
186         is_add = 1;
187       else if (unformat (input, "disable"))
188         is_add = 0;
189       else
190         break;
191     }
192
193   M (UDP_PING_EXPORT, udp_ping_export);
194
195   mp->enable = is_add;
196
197   S;
198   W;
199
200   return (rv);
201 }
202
203 /*
204  * List of messages that the api test plugin sends,
205  * and that the data plane plugin processes
206  */
207 #define foreach_vpe_api_msg \
208 _(udp_ping_add_del, "src <local IPv6 address>  start-src-port <first local port> "\
209   "end-src-port <last local port> " \
210   "dst <remote IPv6 address> start-dst-port <first destination port> "\
211   "end-dst-port <last destination port> "\
212   "interval <time interval in sec for which ping packet will be sent> "\
213   "[disable]")                         \
214 _(udp_ping_export, "export [disable]")                                         \
215
216
217 static void
218 udp_ping_test_api_hookup (vat_main_t * vam)
219 {
220   udp_ping_test_main_t *sm = &udp_ping_test_main;
221   /* Hook up handlers for replies from the data plane plug-in */
222 #define _(N,n)                                                  \
223     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
224                            #n,                                  \
225                            vl_api_##n##_t_handler,              \
226                            vl_noop_handler,                     \
227                            vl_api_##n##_t_endian,               \
228                            vl_api_##n##_t_print,                \
229                            sizeof(vl_api_##n##_t), 1);
230   foreach_vpe_api_reply_msg;
231 #undef _
232
233   /* API messages we can send */
234 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
235   foreach_vpe_api_msg;
236 #undef _
237
238   /* Help strings */
239 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
240   foreach_vpe_api_msg;
241 #undef _
242 }
243
244 clib_error_t *
245 vat_plugin_register (vat_main_t * vam)
246 {
247   udp_ping_test_main_t *sm = &udp_ping_test_main;
248   u8 *name;
249
250   sm->vat_main = vam;
251
252   name = format (0, "udp_ping_%08x%c", api_version, 0);
253   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
254
255   if (sm->msg_id_base != (u16) ~ 0)
256     udp_ping_test_api_hookup (vam);
257
258   vec_free (name);
259
260   return 0;
261 }
262
263 /*
264  * fd.io coding-style-patch-verification: ON
265  *
266  * Local Variables:
267  * eval: (c-set-style "gnu")
268  * End:
269  */