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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 #include <vppinfra/error.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ip/ip_types_api.h>
23 #include <geneve/geneve.h>
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>
33 /* API message ID base */
39 geneve_test_main_t geneve_test_main;
41 #define __plugin_msg_base geneve_test_main.msg_id_base
42 #include <vlibapi/vat_helper_macros.h>
46 vlib_cli_output (handle, (char *) s); \
50 static void vl_api_geneve_add_del_tunnel_reply_t_handler
51 (vl_api_geneve_add_del_tunnel_reply_t * mp)
53 vat_main_t *vam = &vat_main;
54 i32 retval = ntohl (mp->retval);
57 vam->async_errors += (retval < 0);
62 vam->sw_if_index = ntohl (mp->sw_if_index);
63 vam->result_ready = 1;
67 static void vl_api_geneve_add_del_tunnel2_reply_t_handler
68 (vl_api_geneve_add_del_tunnel2_reply_t * mp)
70 vat_main_t *vam = &vat_main;
71 i32 retval = ntohl (mp->retval);
74 vam->async_errors += (retval < 0);
79 vam->sw_if_index = ntohl (mp->sw_if_index);
80 vam->result_ready = 1;
85 api_sw_interface_set_geneve_bypass (vat_main_t * vam)
87 unformat_input_t *i = vam->input;
88 vl_api_sw_interface_set_geneve_bypass_t *mp;
90 u8 sw_if_index_set = 0;
95 /* Parse args required to build the message */
96 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
98 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
100 else if (unformat (i, "sw_if_index %d", &sw_if_index))
102 else if (unformat (i, "enable"))
104 else if (unformat (i, "disable"))
106 else if (unformat (i, "ip4"))
108 else if (unformat (i, "ip6"))
114 if (sw_if_index_set == 0)
116 errmsg ("missing interface name or sw_if_index");
120 /* Construct the API message */
121 M (SW_INTERFACE_SET_GENEVE_BYPASS, mp);
123 mp->sw_if_index = ntohl (sw_if_index);
124 mp->enable = is_enable;
125 mp->is_ipv6 = is_ipv6;
130 /* Wait for a reply... */
135 static uword unformat_geneve_decap_next
136 (unformat_input_t * input, va_list * args)
138 u32 *result = va_arg (*args, u32 *);
141 if (unformat (input, "l2"))
142 *result = GENEVE_INPUT_NEXT_L2_INPUT;
143 else if (unformat (input, "%d", &tmp))
151 api_geneve_add_del_tunnel (vat_main_t * vam)
153 unformat_input_t *line_input = vam->input;
154 vl_api_geneve_add_del_tunnel_t *mp;
155 ip46_address_t src, dst;
157 u8 ipv4_set = 0, ipv6_set = 0;
161 u32 mcast_sw_if_index = ~0;
162 u32 encap_vrf_id = 0;
163 u32 decap_next_index = ~0;
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);
171 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
173 if (unformat (line_input, "del"))
176 if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
182 if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
188 if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
194 if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
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))
203 grp_set = dst_set = 1;
206 else if (unformat (line_input, "group %U",
207 unformat_ip4_address, &dst.ip4))
209 grp_set = dst_set = 1;
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))
216 grp_set = dst_set = 1;
219 else if (unformat (line_input, "group %U",
220 unformat_ip6_address, &dst.ip6))
222 grp_set = dst_set = 1;
226 if (unformat (line_input, "mcast_sw_if_index %u", &mcast_sw_if_index))
228 else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
230 else if (unformat (line_input, "decap-next %U",
231 unformat_geneve_decap_next, &decap_next_index))
233 else if (unformat (line_input, "vni %d", &vni))
237 errmsg ("parse error '%U'", format_unformat_error, line_input);
244 errmsg ("tunnel src address not specified");
249 errmsg ("tunnel dst address not specified");
253 if (grp_set && !ip46_address_is_multicast (&dst))
255 errmsg ("tunnel group address not multicast");
258 if (grp_set && mcast_sw_if_index == ~0)
260 errmsg ("tunnel nonexistent multicast device");
263 if (grp_set == 0 && ip46_address_is_multicast (&dst))
265 errmsg ("tunnel dst address must be unicast");
270 if (ipv4_set && ipv6_set)
272 errmsg ("both IPv4 and IPv6 addresses specified");
276 if ((vni == 0) || (vni >> 24))
278 errmsg ("vni not specified or out of range");
282 M (GENEVE_ADD_DEL_TUNNEL, mp);
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));
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));
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);
306 api_geneve_add_del_tunnel2 (vat_main_t * vam)
308 return api_geneve_add_del_tunnel (vam);
311 static void vl_api_geneve_tunnel_details_t_handler
312 (vl_api_geneve_tunnel_details_t * mp)
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 };
318 if (mp->src_address.af == ADDRESS_IP6)
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));
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));
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));
339 api_geneve_tunnel_dump (vat_main_t * vam)
341 unformat_input_t *i = vam->input;
342 vl_api_geneve_tunnel_dump_t *mp;
343 vl_api_control_ping_t *mp_ping;
345 u8 sw_if_index_set = 0;
348 /* Parse args required to build the message */
349 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
351 if (unformat (i, "sw_if_index %d", &sw_if_index))
357 if (sw_if_index_set == 0)
362 if (!vam->json_output)
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");
369 /* Get list of geneve-tunnel interfaces */
370 M (GENEVE_TUNNEL_DUMP, mp);
372 mp->sw_if_index = htonl (sw_if_index);
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;
384 fformat (vam->ofp, "Sending ping id=%d\n", geneve_test_main.ping_id);
386 vam->result_ready = 0;
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>]") */
402 #include <geneve/geneve.api_test.c>
405 * fd.io coding-style-patch-verification: ON
408 * eval: (c-set-style "gnu")