2 *------------------------------------------------------------------
5 * Copyright (c) 2020 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
23 #include <vnet/format_fns.h>
24 #include <vnet/ip/ip.h>
25 #include <vpp/api/types.h>
26 #include <vnet/geneve/geneve.h>
30 /* API message ID base */
36 geneve_test_main_t geneve_test_main;
38 /* Declare message IDs */
39 #include <vnet/geneve/geneve.api_enum.h>
40 #include <vnet/geneve/geneve.api_types.h>
41 #include <vpp/api/vpe.api_types.h>
43 #define __plugin_msg_base geneve_test_main.msg_id_base
44 #include <vlibapi/vat_helper_macros.h>
45 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
47 static void vl_api_geneve_add_del_tunnel2_reply_t_handler
48 (vl_api_geneve_add_del_tunnel2_reply_t * mp)
51 static void vl_api_geneve_add_del_tunnel_reply_t_handler
52 (vl_api_geneve_add_del_tunnel_reply_t * mp)
54 vat_main_t *vam = &vat_main;
55 i32 retval = ntohl (mp->retval);
58 vam->async_errors += (retval < 0);
63 vam->sw_if_index = ntohl (mp->sw_if_index);
64 vam->result_ready = 1;
69 api_sw_interface_set_geneve_bypass (vat_main_t * vam)
71 unformat_input_t *i = vam->input;
72 vl_api_sw_interface_set_geneve_bypass_t *mp;
74 u8 sw_if_index_set = 0;
79 /* Parse args required to build the message */
80 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
82 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
84 else if (unformat (i, "sw_if_index %d", &sw_if_index))
86 else if (unformat (i, "enable"))
88 else if (unformat (i, "disable"))
90 else if (unformat (i, "ip4"))
92 else if (unformat (i, "ip6"))
98 if (sw_if_index_set == 0)
100 errmsg ("missing interface name or sw_if_index");
104 /* Construct the API message */
105 M (SW_INTERFACE_SET_GENEVE_BYPASS, mp);
107 mp->sw_if_index = ntohl (sw_if_index);
108 mp->enable = is_enable;
109 mp->is_ipv6 = is_ipv6;
114 /* Wait for a reply... */
118 static uword unformat_geneve_decap_next
119 (unformat_input_t * input, va_list * args)
121 u32 *result = va_arg (*args, u32 *);
124 if (unformat (input, "l2"))
125 *result = GENEVE_INPUT_NEXT_L2_INPUT;
126 else if (unformat (input, "%d", &tmp))
134 api_geneve_add_del_tunnel2 (vat_main_t * vam)
136 return 0; // NOT YET IMPLEMENTED
140 api_geneve_add_del_tunnel (vat_main_t * vam)
142 unformat_input_t *line_input = vam->input;
143 vl_api_geneve_add_del_tunnel_t *mp;
144 ip46_address_t src, dst;
146 u8 ipv4_set = 0, ipv6_set = 0;
150 u32 mcast_sw_if_index = ~0;
151 u32 encap_vrf_id = 0;
152 u32 decap_next_index = ~0;
156 /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
157 clib_memset (&src, 0, sizeof src);
158 clib_memset (&dst, 0, sizeof dst);
160 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
162 if (unformat (line_input, "del"))
165 if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
171 if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
177 if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
183 if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
188 else if (unformat (line_input, "group %U %U",
189 unformat_ip4_address, &dst.ip4,
190 unformat_sw_if_index, vam, &mcast_sw_if_index))
192 grp_set = dst_set = 1;
195 else if (unformat (line_input, "group %U",
196 unformat_ip4_address, &dst.ip4))
198 grp_set = dst_set = 1;
201 else if (unformat (line_input, "group %U %U",
202 unformat_ip6_address, &dst.ip6,
203 unformat_sw_if_index, vam, &mcast_sw_if_index))
205 grp_set = dst_set = 1;
208 else if (unformat (line_input, "group %U",
209 unformat_ip6_address, &dst.ip6))
211 grp_set = dst_set = 1;
215 if (unformat (line_input, "mcast_sw_if_index %u", &mcast_sw_if_index))
217 else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
219 else if (unformat (line_input, "decap-next %U",
220 unformat_geneve_decap_next, &decap_next_index))
222 else if (unformat (line_input, "vni %d", &vni))
226 errmsg ("parse error '%U'", format_unformat_error, line_input);
233 errmsg ("tunnel src address not specified");
238 errmsg ("tunnel dst address not specified");
242 if (grp_set && !ip46_address_is_multicast (&dst))
244 errmsg ("tunnel group address not multicast");
247 if (grp_set && mcast_sw_if_index == ~0)
249 errmsg ("tunnel nonexistent multicast device");
252 if (grp_set == 0 && ip46_address_is_multicast (&dst))
254 errmsg ("tunnel dst address must be unicast");
259 if (ipv4_set && ipv6_set)
261 errmsg ("both IPv4 and IPv6 addresses specified");
265 if ((vni == 0) || (vni >> 24))
267 errmsg ("vni not specified or out of range");
271 M (GENEVE_ADD_DEL_TUNNEL, mp);
275 clib_memcpy (&mp->local_address.un.ip6, &src.ip6, sizeof (src.ip6));
276 clib_memcpy (&mp->remote_address.un.ip6, &dst.ip6, sizeof (dst.ip6));
280 clib_memcpy (&mp->local_address.un.ip4, &src.ip4, sizeof (src.ip4));
281 clib_memcpy (&mp->remote_address.un.ip4, &dst.ip4, sizeof (dst.ip4));
283 mp->encap_vrf_id = ntohl (encap_vrf_id);
284 mp->decap_next_index = ntohl (decap_next_index);
285 mp->mcast_sw_if_index = ntohl (mcast_sw_if_index);
286 mp->vni = ntohl (vni);
294 static void vl_api_geneve_tunnel_details_t_handler
295 (vl_api_geneve_tunnel_details_t * mp)
297 vat_main_t *vam = &vat_main;
298 ip46_address_t src = {.as_u64[0] = 0,.as_u64[1] = 0 };
299 ip46_address_t dst = {.as_u64[0] = 0,.as_u64[1] = 0 };
301 if (mp->src_address.af == ADDRESS_IP6)
303 clib_memcpy (&src.ip6, &mp->src_address.un.ip6, sizeof (ip6_address_t));
304 clib_memcpy (&dst.ip6, &mp->dst_address.un.ip6, sizeof (ip6_address_t));
308 clib_memcpy (&src.ip4, &mp->src_address.un.ip4, sizeof (ip4_address_t));
309 clib_memcpy (&dst.ip4, &mp->dst_address.un.ip4, sizeof (ip4_address_t));
312 print (vam->ofp, "%11d%24U%24U%14d%18d%13d%19d",
313 ntohl (mp->sw_if_index),
314 format_ip46_address, &src, IP46_TYPE_ANY,
315 format_ip46_address, &dst, IP46_TYPE_ANY,
316 ntohl (mp->encap_vrf_id),
317 ntohl (mp->decap_next_index), ntohl (mp->vni),
318 ntohl (mp->mcast_sw_if_index));
322 api_geneve_tunnel_dump (vat_main_t * vam)
324 unformat_input_t *i = vam->input;
325 vl_api_geneve_tunnel_dump_t *mp;
326 vl_api_control_ping_t *mp_ping;
328 u8 sw_if_index_set = 0;
330 geneve_test_main_t *gm = &geneve_test_main;
332 /* Parse args required to build the message */
333 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
335 if (unformat (i, "sw_if_index %d", &sw_if_index))
341 if (sw_if_index_set == 0)
346 if (!vam->json_output)
348 print (vam->ofp, "%11s%24s%24s%14s%18s%13s%19s",
349 "sw_if_index", "local_address", "remote_address",
350 "encap_vrf_id", "decap_next_index", "vni", "mcast_sw_if_index");
353 /* Get list of geneve-tunnel interfaces */
354 M (GENEVE_TUNNEL_DUMP, mp);
356 mp->sw_if_index = htonl (sw_if_index);
360 /* Use a control ping for synchronization */
362 gm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
363 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
364 mp_ping->_vl_msg_id = htons (gm->ping_id);
365 mp_ping->client_index = vam->my_client_index;
372 #include <vnet/geneve/geneve.api_test.c>
373 VAT_REGISTER_FEATURE_FUNCTION(vat_geneve_plugin_register);