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.
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 #include <vppinfra/error.h>
20 #include <gtpu/gtpu.h>
21 #include <vnet/ip/ip_types_api.h>
23 #define __plugin_msg_base gtpu_test_main.msg_id_base
24 #include <vlibapi/vat_helper_macros.h>
26 #include <vnet/format_fns.h>
27 #include <gtpu/gtpu.api_enum.h>
28 #include <gtpu/gtpu.api_types.h>
30 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
32 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
33 ip46_type_t type = va_arg (*args, ip46_type_t);
34 if ((type != IP46_TYPE_IP6) &&
35 unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
36 ip46_address_mask_ip4(ip46);
38 } else if ((type != IP46_TYPE_IP4) &&
39 unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
44 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
46 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
47 u8 *len = va_arg (*args, u8 *);
48 ip46_type_t type = va_arg (*args, ip46_type_t);
51 if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
55 ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
56 } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
65 /////////////////////////
68 /* API message ID base */
73 gtpu_test_main_t gtpu_test_main;
75 static void vl_api_gtpu_add_del_tunnel_reply_t_handler
76 (vl_api_gtpu_add_del_tunnel_reply_t * mp)
78 vat_main_t *vam = &vat_main;
79 i32 retval = ntohl (mp->retval);
82 vam->async_errors += (retval < 0);
87 vam->sw_if_index = ntohl (mp->sw_if_index);
88 vam->result_ready = 1;
93 api_unformat_sw_if_index (unformat_input_t * input, va_list * args)
95 vat_main_t *vam = va_arg (*args, vat_main_t *);
96 u32 *result = va_arg (*args, u32 *);
100 if (!unformat (input, "%s", &if_name))
103 p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name);
111 api_unformat_hw_if_index (unformat_input_t * input, va_list * args)
117 api_sw_interface_set_gtpu_bypass (vat_main_t * vam)
119 unformat_input_t *i = vam->input;
120 vl_api_sw_interface_set_gtpu_bypass_t *mp;
122 u8 sw_if_index_set = 0;
127 /* Parse args required to build the message */
128 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
130 if (unformat (i, "%U", api_unformat_sw_if_index, vam, &sw_if_index))
132 else if (unformat (i, "sw_if_index %d", &sw_if_index))
134 else if (unformat (i, "enable"))
136 else if (unformat (i, "disable"))
138 else if (unformat (i, "ip4"))
140 else if (unformat (i, "ip6"))
146 if (sw_if_index_set == 0)
148 errmsg ("missing interface name or sw_if_index");
152 /* Construct the API message */
153 M (SW_INTERFACE_SET_GTPU_BYPASS, mp);
155 mp->sw_if_index = ntohl (sw_if_index);
156 mp->enable = is_enable;
157 mp->is_ipv6 = is_ipv6;
162 /* Wait for a reply... */
167 static uword unformat_gtpu_decap_next
168 (unformat_input_t * input, va_list * args)
170 u32 *result = va_arg (*args, u32 *);
173 if (unformat (input, "l2"))
174 *result = GTPU_INPUT_NEXT_L2_INPUT;
175 else if (unformat (input, "%d", &tmp))
183 api_gtpu_offload_rx (vat_main_t * vam)
185 unformat_input_t *line_input = vam->input;
186 vl_api_gtpu_offload_rx_t *mp;
187 u32 rx_sw_if_index = ~0;
188 u32 hw_if_index = ~0;
192 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
194 if (unformat (line_input, "hw %U", api_unformat_hw_if_index, vam, &hw_if_index))
197 if (unformat (line_input, "rx %U", api_unformat_sw_if_index, vam, &rx_sw_if_index))
200 if (unformat (line_input, "del"))
207 errmsg ("parse error '%U'", format_unformat_error, line_input);
212 if (rx_sw_if_index == ~0)
214 errmsg ("missing rx interface");
218 if (hw_if_index == ~0)
220 errmsg ("missing hw interface");
224 M (GTPU_OFFLOAD_RX, mp);
225 mp->hw_if_index = ntohl (hw_if_index);
226 mp->sw_if_index = ntohl (rx_sw_if_index);
235 api_gtpu_add_del_tunnel (vat_main_t * vam)
237 unformat_input_t *line_input = vam->input;
238 vl_api_gtpu_add_del_tunnel_t *mp;
239 ip46_address_t src, dst;
241 u8 ipv4_set = 0, ipv6_set = 0;
245 u32 mcast_sw_if_index = ~0;
246 u32 encap_vrf_id = 0;
247 u32 decap_next_index = ~0;
251 /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
252 clib_memset (&src, 0, sizeof src);
253 clib_memset (&dst, 0, sizeof dst);
255 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
257 if (unformat (line_input, "del"))
260 if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
266 if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
272 if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
278 if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
283 else if (unformat (line_input, "group %U %U",
284 unformat_ip4_address, &dst.ip4,
285 api_unformat_sw_if_index, vam, &mcast_sw_if_index))
287 grp_set = dst_set = 1;
290 else if (unformat (line_input, "group %U",
291 unformat_ip4_address, &dst.ip4))
293 grp_set = dst_set = 1;
296 else if (unformat (line_input, "group %U %U",
297 unformat_ip6_address, &dst.ip6,
298 api_unformat_sw_if_index, vam, &mcast_sw_if_index))
300 grp_set = dst_set = 1;
303 else if (unformat (line_input, "group %U",
304 unformat_ip6_address, &dst.ip6))
306 grp_set = dst_set = 1;
310 if (unformat (line_input, "mcast_sw_if_index %u", &mcast_sw_if_index))
312 else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
314 else if (unformat (line_input, "decap-next %U",
315 unformat_gtpu_decap_next, &decap_next_index))
317 else if (unformat (line_input, "teid %d", &teid))
321 errmsg ("parse error '%U'", format_unformat_error, line_input);
328 errmsg ("tunnel src address not specified");
333 errmsg ("tunnel dst address not specified");
337 if (grp_set && !ip46_address_is_multicast (&dst))
339 errmsg ("tunnel group address not multicast");
342 if (grp_set && mcast_sw_if_index == ~0)
344 errmsg ("tunnel nonexistent multicast device");
347 if (grp_set == 0 && ip46_address_is_multicast (&dst))
349 errmsg ("tunnel dst address must be unicast");
354 if (ipv4_set && ipv6_set)
356 errmsg ("both IPv4 and IPv6 addresses specified");
360 M (GTPU_ADD_DEL_TUNNEL, mp);
362 ip_address_encode(&src, ipv6_set ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
364 ip_address_encode(&dst, ipv6_set ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
366 mp->encap_vrf_id = ntohl (encap_vrf_id);
367 mp->decap_next_index = ntohl (decap_next_index);
368 mp->mcast_sw_if_index = ntohl (mcast_sw_if_index);
369 mp->teid = ntohl (teid);
377 static void vl_api_gtpu_tunnel_details_t_handler
378 (vl_api_gtpu_tunnel_details_t * mp)
380 vat_main_t *vam = &vat_main;
383 ip_address_decode(&mp->dst_address, &dst);
384 ip_address_decode(&mp->src_address, &src);
385 print (vam->ofp, "%11d%24U%24U%14d%18d%13d%19d",
386 ntohl (mp->sw_if_index),
387 format_ip46_address, &src, IP46_TYPE_ANY,
388 format_ip46_address, &dst, IP46_TYPE_ANY,
389 ntohl (mp->encap_vrf_id),
390 ntohl (mp->decap_next_index), ntohl (mp->teid),
391 ntohl (mp->mcast_sw_if_index));
395 api_gtpu_tunnel_dump (vat_main_t * vam)
397 unformat_input_t *i = vam->input;
398 vl_api_gtpu_tunnel_dump_t *mp;
400 u8 sw_if_index_set = 0;
403 /* Parse args required to build the message */
404 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
406 if (unformat (i, "sw_if_index %d", &sw_if_index))
412 if (sw_if_index_set == 0)
417 if (!vam->json_output)
419 print (vam->ofp, "%11s%24s%24s%14s%18s%13s%19s",
420 "sw_if_index", "src_address", "dst_address",
421 "encap_vrf_id", "decap_next_index", "teid", "mcast_sw_if_index");
424 /* Get list of gtpu-tunnel interfaces */
425 M (GTPU_TUNNEL_DUMP, mp);
427 mp->sw_if_index = htonl (sw_if_index);
435 #include <gtpu/gtpu.api_test.c>