2 * Copyright (c) 2017 Intel 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.
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21 #include <gtpu/gtpu.h>
22 #include <vnet/ip/ip_types_api.h>
24 #define __plugin_msg_base gtpu_test_main.msg_id_base
25 #include <vlibapi/vat_helper_macros.h>
27 #include <vnet/format_fns.h>
28 #include <gtpu/gtpu.api_enum.h>
29 #include <gtpu/gtpu.api_types.h>
31 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
33 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
34 ip46_type_t type = va_arg (*args, ip46_type_t);
35 if ((type != IP46_TYPE_IP6) &&
36 unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
37 ip46_address_mask_ip4(ip46);
39 } else if ((type != IP46_TYPE_IP4) &&
40 unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
45 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
47 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
48 u8 *len = va_arg (*args, u8 *);
49 ip46_type_t type = va_arg (*args, ip46_type_t);
52 if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
56 ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
57 } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
66 /////////////////////////
69 /* API message ID base */
74 gtpu_test_main_t gtpu_test_main;
76 static void vl_api_gtpu_add_del_tunnel_reply_t_handler
77 (vl_api_gtpu_add_del_tunnel_reply_t * mp)
79 vat_main_t *vam = &vat_main;
80 i32 retval = ntohl (mp->retval);
83 vam->async_errors += (retval < 0);
88 vam->sw_if_index = ntohl (mp->sw_if_index);
89 vam->result_ready = 1;
94 api_unformat_sw_if_index (unformat_input_t * input, va_list * args)
96 vat_main_t *vam = va_arg (*args, vat_main_t *);
97 u32 *result = va_arg (*args, u32 *);
101 if (!unformat (input, "%s", &if_name))
104 p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name);
112 api_unformat_hw_if_index (unformat_input_t * input, va_list * args)
118 api_sw_interface_set_gtpu_bypass (vat_main_t * vam)
120 unformat_input_t *i = vam->input;
121 vl_api_sw_interface_set_gtpu_bypass_t *mp;
123 u8 sw_if_index_set = 0;
128 /* Parse args required to build the message */
129 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
131 if (unformat (i, "%U", api_unformat_sw_if_index, vam, &sw_if_index))
133 else if (unformat (i, "sw_if_index %d", &sw_if_index))
135 else if (unformat (i, "enable"))
137 else if (unformat (i, "disable"))
139 else if (unformat (i, "ip4"))
141 else if (unformat (i, "ip6"))
147 if (sw_if_index_set == 0)
149 errmsg ("missing interface name or sw_if_index");
153 /* Construct the API message */
154 M (SW_INTERFACE_SET_GTPU_BYPASS, mp);
156 mp->sw_if_index = ntohl (sw_if_index);
157 mp->enable = is_enable;
158 mp->is_ipv6 = is_ipv6;
163 /* Wait for a reply... */
168 static uword unformat_gtpu_decap_next
169 (unformat_input_t * input, va_list * args)
171 u32 *result = va_arg (*args, u32 *);
174 if (unformat (input, "l2"))
175 *result = GTPU_INPUT_NEXT_L2_INPUT;
176 else if (unformat (input, "%d", &tmp))
184 api_gtpu_offload_rx (vat_main_t * vam)
186 unformat_input_t *line_input = vam->input;
187 vl_api_gtpu_offload_rx_t *mp;
188 u32 rx_sw_if_index = ~0;
189 u32 hw_if_index = ~0;
193 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
195 if (unformat (line_input, "hw %U", api_unformat_hw_if_index, vam, &hw_if_index))
198 if (unformat (line_input, "rx %U", api_unformat_sw_if_index, vam, &rx_sw_if_index))
201 if (unformat (line_input, "del"))
208 errmsg ("parse error '%U'", format_unformat_error, line_input);
213 if (rx_sw_if_index == ~0)
215 errmsg ("missing rx interface");
219 if (hw_if_index == ~0)
221 errmsg ("missing hw interface");
225 M (GTPU_OFFLOAD_RX, mp);
226 mp->hw_if_index = ntohl (hw_if_index);
227 mp->sw_if_index = ntohl (rx_sw_if_index);
236 api_gtpu_add_del_tunnel (vat_main_t * vam)
238 unformat_input_t *line_input = vam->input;
239 vl_api_gtpu_add_del_tunnel_t *mp;
240 ip46_address_t src, dst;
242 u8 ipv4_set = 0, ipv6_set = 0;
246 u32 mcast_sw_if_index = ~0;
247 u32 encap_vrf_id = 0;
248 u32 decap_next_index = ~0;
249 u32 teid = 0, tteid = 0;
252 /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
253 clib_memset (&src, 0, sizeof src);
254 clib_memset (&dst, 0, sizeof dst);
256 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
258 if (unformat (line_input, "del"))
261 if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
267 if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
273 if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
279 if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
284 else if (unformat (line_input, "group %U %U",
285 unformat_ip4_address, &dst.ip4,
286 api_unformat_sw_if_index, vam, &mcast_sw_if_index))
288 grp_set = dst_set = 1;
291 else if (unformat (line_input, "group %U",
292 unformat_ip4_address, &dst.ip4))
294 grp_set = dst_set = 1;
297 else if (unformat (line_input, "group %U %U",
298 unformat_ip6_address, &dst.ip6,
299 api_unformat_sw_if_index, vam, &mcast_sw_if_index))
301 grp_set = dst_set = 1;
304 else if (unformat (line_input, "group %U",
305 unformat_ip6_address, &dst.ip6))
307 grp_set = dst_set = 1;
311 if (unformat (line_input, "mcast_sw_if_index %u", &mcast_sw_if_index))
313 else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
315 else if (unformat (line_input, "decap-next %U",
316 unformat_gtpu_decap_next, &decap_next_index))
318 else if (unformat (line_input, "teid %d", &teid))
320 else if (unformat (line_input, "tteid %d", &tteid))
324 errmsg ("parse error '%U'", format_unformat_error, line_input);
329 if (is_add && src_set == 0)
331 errmsg ("tunnel src address not specified");
336 errmsg ("tunnel dst address not specified");
340 if (grp_set && !ip46_address_is_multicast (&dst))
342 errmsg ("tunnel group address not multicast");
345 if (grp_set && mcast_sw_if_index == ~0)
347 errmsg ("tunnel nonexistent multicast device");
350 if (grp_set == 0 && ip46_address_is_multicast (&dst))
352 errmsg ("tunnel dst address must be unicast");
357 if (ipv4_set && ipv6_set)
359 errmsg ("both IPv4 and IPv6 addresses specified");
363 M (GTPU_ADD_DEL_TUNNEL, mp);
365 ip_address_encode(&src, ipv6_set ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
367 ip_address_encode(&dst, ipv6_set ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
369 mp->encap_vrf_id = ntohl (encap_vrf_id);
370 mp->decap_next_index = ntohl (decap_next_index);
371 mp->mcast_sw_if_index = ntohl (mcast_sw_if_index);
372 mp->teid = ntohl (teid);
373 mp->tteid = ntohl (tteid);
382 api_gtpu_tunnel_update_tteid (vat_main_t * vam)
384 unformat_input_t *line_input = vam->input;
385 vl_api_gtpu_tunnel_update_tteid_t *mp;
389 u32 encap_vrf_id = 0;
390 u32 teid = 0, tteid = 0;
393 /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
394 clib_memset (&dst, 0, sizeof dst);
396 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
398 if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
402 else if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
407 else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
409 else if (unformat (line_input, "teid %d", &teid))
411 else if (unformat (line_input, "tteid %d", &tteid))
415 errmsg ("parse error '%U'", format_unformat_error, line_input);
422 errmsg ("tunnel dst address not specified");
426 M (GTPU_TUNNEL_UPDATE_TTEID, mp);
428 ip_address_encode(&dst, ipv6_set ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
430 mp->encap_vrf_id = ntohl (encap_vrf_id);
431 mp->teid = ntohl (teid);
432 mp->tteid = ntohl (tteid);
439 static void vl_api_gtpu_tunnel_details_t_handler
440 (vl_api_gtpu_tunnel_details_t * mp)
442 vat_main_t *vam = &vat_main;
445 ip_address_decode(&mp->dst_address, &dst);
446 ip_address_decode(&mp->src_address, &src);
447 print (vam->ofp, "%11d%24U%24U%14d%18d%13d%13d%19d",
448 ntohl (mp->sw_if_index),
449 format_ip46_address, &src, IP46_TYPE_ANY,
450 format_ip46_address, &dst, IP46_TYPE_ANY,
451 ntohl (mp->encap_vrf_id),
452 ntohl (mp->decap_next_index),
453 ntohl (mp->teid), ntohl (mp->tteid),
454 ntohl (mp->mcast_sw_if_index));
458 api_gtpu_tunnel_dump (vat_main_t * vam)
460 unformat_input_t *i = vam->input;
461 vl_api_gtpu_tunnel_dump_t *mp;
463 u8 sw_if_index_set = 0;
465 /* Parse args required to build the message */
466 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
468 if (unformat (i, "sw_if_index %d", &sw_if_index))
474 if (sw_if_index_set == 0)
479 if (!vam->json_output)
481 print (vam->ofp, "%11s%24s%24s%14s%18s%13s%13s%19s",
482 "sw_if_index", "src_address", "dst_address",
483 "encap_vrf_id", "decap_next_index", "teid", "tteid",
484 "mcast_sw_if_index");
487 /* Get list of gtpu-tunnel interfaces */
488 M (GTPU_TUNNEL_DUMP, mp);
490 mp->sw_if_index = htonl (sw_if_index);
494 /* No status response for this API call.
495 * Wait 1 sec for any dump output before return to vat# */
501 #include <gtpu/gtpu.api_test.c>