hs-test: more debug output in http3 test
[vpp.git] / src / plugins / geneve / geneve_test.c
1 /*
2  * Copyright (c) 2020 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 #include <vat/vat.h>
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 #include <vppinfra/error.h>
20
21 #include <vnet/ip/ip.h>
22 #include <vnet/ip/ip_types_api.h>
23 #include <geneve/geneve.h>
24
25 /* define message IDs */
26 #include <vnet/format_fns.h>
27 #include <geneve/geneve.api_enum.h>
28 #include <geneve/geneve.api_types.h>
29 #include <vlibmemory/vlib.api_types.h>
30
31 typedef struct
32 {
33   /* API message ID base */
34   u16 msg_id_base;
35   u32 ping_id;
36   vat_main_t *vat_main;
37 } geneve_test_main_t;
38
39 geneve_test_main_t geneve_test_main;
40
41 #define __plugin_msg_base geneve_test_main.msg_id_base
42 #include <vlibapi/vat_helper_macros.h>
43
44 #define FINISH                                                                \
45   vec_add1 (s, 0);                                                            \
46   vlib_cli_output (handle, (char *) s);                                       \
47   vec_free (s);                                                               \
48   return handle;
49
50 static void vl_api_geneve_add_del_tunnel_reply_t_handler
51   (vl_api_geneve_add_del_tunnel_reply_t * mp)
52 {
53   vat_main_t *vam = &vat_main;
54   i32 retval = ntohl (mp->retval);
55   if (vam->async_mode)
56     {
57       vam->async_errors += (retval < 0);
58     }
59   else
60     {
61       vam->retval = retval;
62       vam->sw_if_index = ntohl (mp->sw_if_index);
63       vam->result_ready = 1;
64     }
65 }
66
67 static void vl_api_geneve_add_del_tunnel2_reply_t_handler
68   (vl_api_geneve_add_del_tunnel2_reply_t * mp)
69 {
70   vat_main_t *vam = &vat_main;
71   i32 retval = ntohl (mp->retval);
72   if (vam->async_mode)
73     {
74       vam->async_errors += (retval < 0);
75     }
76   else
77     {
78       vam->retval = retval;
79       vam->sw_if_index = ntohl (mp->sw_if_index);
80       vam->result_ready = 1;
81     }
82 }
83
84 static int
85 api_sw_interface_set_geneve_bypass (vat_main_t * vam)
86 {
87   unformat_input_t *i = vam->input;
88   vl_api_sw_interface_set_geneve_bypass_t *mp;
89   u32 sw_if_index = 0;
90   u8 sw_if_index_set = 0;
91   u8 is_enable = 1;
92   u8 is_ipv6 = 0;
93   int ret;
94
95   /* Parse args required to build the message */
96   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
97     {
98       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
99         sw_if_index_set = 1;
100       else if (unformat (i, "sw_if_index %d", &sw_if_index))
101         sw_if_index_set = 1;
102       else if (unformat (i, "enable"))
103         is_enable = 1;
104       else if (unformat (i, "disable"))
105         is_enable = 0;
106       else if (unformat (i, "ip4"))
107         is_ipv6 = 0;
108       else if (unformat (i, "ip6"))
109         is_ipv6 = 1;
110       else
111         break;
112     }
113
114   if (sw_if_index_set == 0)
115     {
116       errmsg ("missing interface name or sw_if_index");
117       return -99;
118     }
119
120   /* Construct the API message */
121   M (SW_INTERFACE_SET_GENEVE_BYPASS, mp);
122
123   mp->sw_if_index = ntohl (sw_if_index);
124   mp->enable = is_enable;
125   mp->is_ipv6 = is_ipv6;
126
127   /* send it... */
128   S (mp);
129
130   /* Wait for a reply... */
131   W (ret);
132   return ret;
133 }
134
135 static uword unformat_geneve_decap_next
136   (unformat_input_t * input, va_list * args)
137 {
138   u32 *result = va_arg (*args, u32 *);
139   u32 tmp;
140
141   if (unformat (input, "l2"))
142     *result = GENEVE_INPUT_NEXT_L2_INPUT;
143   else if (unformat (input, "%d", &tmp))
144     *result = tmp;
145   else
146     return 0;
147   return 1;
148 }
149
150 static int
151 api_geneve_add_del_tunnel (vat_main_t * vam)
152 {
153   unformat_input_t *line_input = vam->input;
154   vl_api_geneve_add_del_tunnel_t *mp;
155   ip46_address_t src, dst;
156   u8 is_add = 1;
157   u8 ipv4_set = 0, ipv6_set = 0;
158   u8 src_set = 0;
159   u8 dst_set = 0;
160   u8 grp_set = 0;
161   u32 mcast_sw_if_index = ~0;
162   u32 encap_vrf_id = 0;
163   u32 decap_next_index = ~0;
164   u32 vni = 0;
165   int ret;
166
167   /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
168   clib_memset (&src, 0, sizeof src);
169   clib_memset (&dst, 0, sizeof dst);
170
171   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
172     {
173       if (unformat (line_input, "del"))
174         is_add = 0;
175       else
176         if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
177         {
178           ipv4_set = 1;
179           src_set = 1;
180         }
181       else
182         if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
183         {
184           ipv4_set = 1;
185           dst_set = 1;
186         }
187       else
188         if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
189         {
190           ipv6_set = 1;
191           src_set = 1;
192         }
193       else
194         if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
195         {
196           ipv6_set = 1;
197           dst_set = 1;
198         }
199       else if (unformat (line_input, "group %U %U",
200                          unformat_ip4_address, &dst.ip4,
201                          unformat_sw_if_index, vam, &mcast_sw_if_index))
202         {
203           grp_set = dst_set = 1;
204           ipv4_set = 1;
205         }
206       else if (unformat (line_input, "group %U",
207                          unformat_ip4_address, &dst.ip4))
208         {
209           grp_set = dst_set = 1;
210           ipv4_set = 1;
211         }
212       else if (unformat (line_input, "group %U %U",
213                          unformat_ip6_address, &dst.ip6,
214                          unformat_sw_if_index, vam, &mcast_sw_if_index))
215         {
216           grp_set = dst_set = 1;
217           ipv6_set = 1;
218         }
219       else if (unformat (line_input, "group %U",
220                          unformat_ip6_address, &dst.ip6))
221         {
222           grp_set = dst_set = 1;
223           ipv6_set = 1;
224         }
225       else
226         if (unformat (line_input, "mcast_sw_if_index %u", &mcast_sw_if_index))
227         ;
228       else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
229         ;
230       else if (unformat (line_input, "decap-next %U",
231                          unformat_geneve_decap_next, &decap_next_index))
232         ;
233       else if (unformat (line_input, "vni %d", &vni))
234         ;
235       else
236         {
237           errmsg ("parse error '%U'", format_unformat_error, line_input);
238           return -99;
239         }
240     }
241
242   if (src_set == 0)
243     {
244       errmsg ("tunnel src address not specified");
245       return -99;
246     }
247   if (dst_set == 0)
248     {
249       errmsg ("tunnel dst address not specified");
250       return -99;
251     }
252
253   if (grp_set && !ip46_address_is_multicast (&dst))
254     {
255       errmsg ("tunnel group address not multicast");
256       return -99;
257     }
258   if (grp_set && mcast_sw_if_index == ~0)
259     {
260       errmsg ("tunnel nonexistent multicast device");
261       return -99;
262     }
263   if (grp_set == 0 && ip46_address_is_multicast (&dst))
264     {
265       errmsg ("tunnel dst address must be unicast");
266       return -99;
267     }
268
269
270   if (ipv4_set && ipv6_set)
271     {
272       errmsg ("both IPv4 and IPv6 addresses specified");
273       return -99;
274     }
275
276   if ((vni == 0) || (vni >> 24))
277     {
278       errmsg ("vni not specified or out of range");
279       return -99;
280     }
281
282   M (GENEVE_ADD_DEL_TUNNEL, mp);
283
284   if (ipv6_set)
285     {
286       clib_memcpy (&mp->local_address.un.ip6, &src.ip6, sizeof (src.ip6));
287       clib_memcpy (&mp->remote_address.un.ip6, &dst.ip6, sizeof (dst.ip6));
288     }
289   else
290     {
291       clib_memcpy (&mp->local_address.un.ip4, &src.ip4, sizeof (src.ip4));
292       clib_memcpy (&mp->remote_address.un.ip4, &dst.ip4, sizeof (dst.ip4));
293     }
294   mp->encap_vrf_id = ntohl (encap_vrf_id);
295   mp->decap_next_index = ntohl (decap_next_index);
296   mp->mcast_sw_if_index = ntohl (mcast_sw_if_index);
297   mp->vni = ntohl (vni);
298   mp->is_add = is_add;
299
300   S (mp);
301   W (ret);
302   return ret;
303 }
304
305 static int
306 api_geneve_add_del_tunnel2 (vat_main_t * vam)
307 {
308   return api_geneve_add_del_tunnel (vam);
309 }
310
311 static void vl_api_geneve_tunnel_details_t_handler
312   (vl_api_geneve_tunnel_details_t * mp)
313 {
314   vat_main_t *vam = &vat_main;
315   ip46_address_t src = {.as_u64[0] = 0,.as_u64[1] = 0 };
316   ip46_address_t dst = {.as_u64[0] = 0,.as_u64[1] = 0 };
317
318   if (mp->src_address.af == ADDRESS_IP6)
319     {
320       clib_memcpy (&src.ip6, &mp->src_address.un.ip6, sizeof (ip6_address_t));
321       clib_memcpy (&dst.ip6, &mp->dst_address.un.ip6, sizeof (ip6_address_t));
322     }
323   else
324     {
325       clib_memcpy (&src.ip4, &mp->src_address.un.ip4, sizeof (ip4_address_t));
326       clib_memcpy (&dst.ip4, &mp->dst_address.un.ip4, sizeof (ip4_address_t));
327     }
328
329   print (vam->ofp, "%11d%24U%24U%14d%18d%13d%19d",
330          ntohl (mp->sw_if_index),
331          format_ip46_address, &src, IP46_TYPE_ANY,
332          format_ip46_address, &dst, IP46_TYPE_ANY,
333          ntohl (mp->encap_vrf_id),
334          ntohl (mp->decap_next_index), ntohl (mp->vni),
335          ntohl (mp->mcast_sw_if_index));
336 }
337
338 static int
339 api_geneve_tunnel_dump (vat_main_t * vam)
340 {
341   unformat_input_t *i = vam->input;
342   vl_api_geneve_tunnel_dump_t *mp;
343   vl_api_control_ping_t *mp_ping;
344   u32 sw_if_index;
345   u8 sw_if_index_set = 0;
346   int ret;
347
348   /* Parse args required to build the message */
349   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
350     {
351       if (unformat (i, "sw_if_index %d", &sw_if_index))
352         sw_if_index_set = 1;
353       else
354         break;
355     }
356
357   if (sw_if_index_set == 0)
358     {
359       sw_if_index = ~0;
360     }
361
362   if (!vam->json_output)
363     {
364       print (vam->ofp, "%11s%24s%24s%14s%18s%13s%19s",
365              "sw_if_index", "local_address", "remote_address",
366              "encap_vrf_id", "decap_next_index", "vni", "mcast_sw_if_index");
367     }
368
369   /* Get list of geneve-tunnel interfaces */
370   M (GENEVE_TUNNEL_DUMP, mp);
371
372   mp->sw_if_index = htonl (sw_if_index);
373
374   S (mp);
375
376   /* Use a control ping for synchronization */
377   if (!geneve_test_main.ping_id)
378     geneve_test_main.ping_id =
379       vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
380   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
381   mp_ping->_vl_msg_id = htons (geneve_test_main.ping_id);
382   mp_ping->client_index = vam->my_client_index;
383
384   fformat (vam->ofp, "Sending ping id=%d\n", geneve_test_main.ping_id);
385
386   vam->result_ready = 0;
387   S (mp_ping);
388
389   W (ret);
390   return ret;
391 }
392
393 /* _(sw_interface_set_geneve_bypass,                                        */
394 /*   "<intfc> | sw_if_index <id> [ip4 | ip6] [enable | disable]")           */
395 /* _(geneve_add_del_tunnel,                                                 */
396 /*   "src <ip-addr> { dst <ip-addr> | group <mcast-ip-addr>\n"              */
397 /*   "{ <intfc> | mcast_sw_if_index <nn> } }\n"                             */
398 /*   "vni <vni> [encap-vrf-id <nn>] [decap-next <l2|nn>] [del]")            */
399 /* _(geneve_tunnel_dump, "[<intfc> | sw_if_index <nn>]")                    */
400
401
402 #include <geneve/geneve.api_test.c>
403
404 /*
405  * fd.io coding-style-patch-verification: ON
406  *
407  * Local Variables:
408  * eval: (c-set-style "gnu")
409  * End:
410  */