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>
22 #define __plugin_msg_base gtpu_test_main.msg_id_base
23 #include <vlibapi/vat_helper_macros.h>
26 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
28 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
29 ip46_type_t type = va_arg (*args, ip46_type_t);
30 if ((type != IP46_TYPE_IP6) &&
31 unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
32 ip46_address_mask_ip4(ip46);
34 } else if ((type != IP46_TYPE_IP4) &&
35 unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
40 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
42 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
43 u8 *len = va_arg (*args, u8 *);
44 ip46_type_t type = va_arg (*args, ip46_type_t);
47 if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
51 ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
52 } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
61 /////////////////////////
63 #define vl_msg_id(n,h) n,
65 #include <gtpu/gtpu.api.h>
66 /* We'll want to know how many messages IDs we need... */
67 VL_MSG_FIRST_AVAILABLE,
71 /* define message structures */
73 #include <gtpu/gtpu.api.h>
76 /* declare message handlers for each api */
78 #define vl_endianfun /* define message structures */
79 #include <gtpu/gtpu.api.h>
82 /* instantiate all the print functions we know about */
83 #define vl_print(handle, ...)
85 #include <gtpu/gtpu.api.h>
88 /* Get the API version number. */
89 #define vl_api_version(n,v) static u32 api_version=(v);
90 #include <gtpu/gtpu.api.h>
94 /* API message ID base */
99 gtpu_test_main_t gtpu_test_main;
101 static void vl_api_gtpu_add_del_tunnel_reply_t_handler
102 (vl_api_gtpu_add_del_tunnel_reply_t * mp)
104 vat_main_t *vam = &vat_main;
105 i32 retval = ntohl (mp->retval);
108 vam->async_errors += (retval < 0);
112 vam->retval = retval;
113 vam->sw_if_index = ntohl (mp->sw_if_index);
114 vam->result_ready = 1;
119 #define foreach_standard_reply_retval_handler \
120 _(sw_interface_set_gtpu_bypass_reply)
123 static void vl_api_##n##_t_handler \
124 (vl_api_##n##_t * mp) \
126 vat_main_t * vam = gtpu_test_main.vat_main; \
127 i32 retval = ntohl(mp->retval); \
128 if (vam->async_mode) { \
129 vam->async_errors += (retval < 0); \
131 vam->retval = retval; \
132 vam->result_ready = 1; \
135 foreach_standard_reply_retval_handler;
139 * Table of message reply handlers, must include boilerplate handlers
142 #define foreach_vpe_api_reply_msg \
143 _(SW_INTERFACE_SET_GTPU_BYPASS_REPLY, sw_interface_set_gtpu_bypass_reply) \
144 _(GTPU_ADD_DEL_TUNNEL_REPLY, gtpu_add_del_tunnel_reply) \
145 _(GTPU_TUNNEL_DETAILS, gtpu_tunnel_details)
149 api_unformat_sw_if_index (unformat_input_t * input, va_list * args)
151 vat_main_t *vam = va_arg (*args, vat_main_t *);
152 u32 *result = va_arg (*args, u32 *);
156 if (!unformat (input, "%s", &if_name))
159 p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name);
167 api_sw_interface_set_gtpu_bypass (vat_main_t * vam)
169 unformat_input_t *i = vam->input;
170 vl_api_sw_interface_set_gtpu_bypass_t *mp;
172 u8 sw_if_index_set = 0;
177 /* Parse args required to build the message */
178 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
180 if (unformat (i, "%U", api_unformat_sw_if_index, vam, &sw_if_index))
182 else if (unformat (i, "sw_if_index %d", &sw_if_index))
184 else if (unformat (i, "enable"))
186 else if (unformat (i, "disable"))
188 else if (unformat (i, "ip4"))
190 else if (unformat (i, "ip6"))
196 if (sw_if_index_set == 0)
198 errmsg ("missing interface name or sw_if_index");
202 /* Construct the API message */
203 M (SW_INTERFACE_SET_GTPU_BYPASS, mp);
205 mp->sw_if_index = ntohl (sw_if_index);
206 mp->enable = is_enable;
207 mp->is_ipv6 = is_ipv6;
212 /* Wait for a reply... */
217 static uword unformat_gtpu_decap_next
218 (unformat_input_t * input, va_list * args)
220 u32 *result = va_arg (*args, u32 *);
223 if (unformat (input, "l2"))
224 *result = GTPU_INPUT_NEXT_L2_INPUT;
225 else if (unformat (input, "%d", &tmp))
233 api_gtpu_add_del_tunnel (vat_main_t * vam)
235 unformat_input_t *line_input = vam->input;
236 vl_api_gtpu_add_del_tunnel_t *mp;
237 ip46_address_t src, dst;
239 u8 ipv4_set = 0, ipv6_set = 0;
243 u32 mcast_sw_if_index = ~0;
244 u32 encap_vrf_id = 0;
245 u32 decap_next_index = ~0;
249 /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
250 memset (&src, 0, sizeof src);
251 memset (&dst, 0, sizeof dst);
253 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
255 if (unformat (line_input, "del"))
258 if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
264 if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
270 if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
276 if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
281 else if (unformat (line_input, "group %U %U",
282 unformat_ip4_address, &dst.ip4,
283 api_unformat_sw_if_index, vam, &mcast_sw_if_index))
285 grp_set = dst_set = 1;
288 else if (unformat (line_input, "group %U",
289 unformat_ip4_address, &dst.ip4))
291 grp_set = dst_set = 1;
294 else if (unformat (line_input, "group %U %U",
295 unformat_ip6_address, &dst.ip6,
296 api_unformat_sw_if_index, vam, &mcast_sw_if_index))
298 grp_set = dst_set = 1;
301 else if (unformat (line_input, "group %U",
302 unformat_ip6_address, &dst.ip6))
304 grp_set = dst_set = 1;
308 if (unformat (line_input, "mcast_sw_if_index %u", &mcast_sw_if_index))
310 else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
312 else if (unformat (line_input, "decap-next %U",
313 unformat_gtpu_decap_next, &decap_next_index))
315 else if (unformat (line_input, "teid %d", &teid))
319 errmsg ("parse error '%U'", format_unformat_error, line_input);
326 errmsg ("tunnel src address not specified");
331 errmsg ("tunnel dst address not specified");
335 if (grp_set && !ip46_address_is_multicast (&dst))
337 errmsg ("tunnel group address not multicast");
340 if (grp_set && mcast_sw_if_index == ~0)
342 errmsg ("tunnel nonexistent multicast device");
345 if (grp_set == 0 && ip46_address_is_multicast (&dst))
347 errmsg ("tunnel dst address must be unicast");
352 if (ipv4_set && ipv6_set)
354 errmsg ("both IPv4 and IPv6 addresses specified");
358 M (GTPU_ADD_DEL_TUNNEL, mp);
362 clib_memcpy (mp->src_address, &src.ip6, sizeof (src.ip6));
363 clib_memcpy (mp->dst_address, &dst.ip6, sizeof (dst.ip6));
367 clib_memcpy (mp->src_address, &src.ip4, sizeof (src.ip4));
368 clib_memcpy (mp->dst_address, &dst.ip4, sizeof (dst.ip4));
370 mp->encap_vrf_id = ntohl (encap_vrf_id);
371 mp->decap_next_index = ntohl (decap_next_index);
372 mp->mcast_sw_if_index = ntohl (mcast_sw_if_index);
373 mp->teid = ntohl (teid);
375 mp->is_ipv6 = ipv6_set;
382 static void vl_api_gtpu_tunnel_details_t_handler
383 (vl_api_gtpu_tunnel_details_t * mp)
385 vat_main_t *vam = &vat_main;
386 ip46_address_t src = to_ip46 (mp->is_ipv6, mp->dst_address);
387 ip46_address_t dst = to_ip46 (mp->is_ipv6, mp->src_address);
389 print (vam->ofp, "%11d%24U%24U%14d%18d%13d%19d",
390 ntohl (mp->sw_if_index),
391 format_ip46_address, &src, IP46_TYPE_ANY,
392 format_ip46_address, &dst, IP46_TYPE_ANY,
393 ntohl (mp->encap_vrf_id),
394 ntohl (mp->decap_next_index), ntohl (mp->teid),
395 ntohl (mp->mcast_sw_if_index));
399 api_gtpu_tunnel_dump (vat_main_t * vam)
401 unformat_input_t *i = vam->input;
402 vl_api_gtpu_tunnel_dump_t *mp;
404 u8 sw_if_index_set = 0;
407 /* Parse args required to build the message */
408 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
410 if (unformat (i, "sw_if_index %d", &sw_if_index))
416 if (sw_if_index_set == 0)
421 if (!vam->json_output)
423 print (vam->ofp, "%11s%24s%24s%14s%18s%13s%19s",
424 "sw_if_index", "src_address", "dst_address",
425 "encap_vrf_id", "decap_next_index", "teid", "mcast_sw_if_index");
428 /* Get list of gtpu-tunnel interfaces */
429 M (GTPU_TUNNEL_DUMP, mp);
431 mp->sw_if_index = htonl (sw_if_index);
440 * List of messages that the api test plugin sends,
441 * and that the data plane plugin processes
443 #define foreach_vpe_api_msg \
444 _(sw_interface_set_gtpu_bypass, \
445 "<intfc> | sw_if_index <id> [ip4 | ip6] [enable | disable]") \
446 _(gtpu_add_del_tunnel, \
447 "src <ip-addr> { dst <ip-addr> | group <mcast-ip-addr>\n" \
448 "{ <intfc> | mcast_sw_if_index <nn> } }\n" \
449 "teid <teid> [encap-vrf-id <nn>] [decap-next <l2|nn>] [del]") \
450 _(gtpu_tunnel_dump, "[<intfc> | sw_if_index <nn>]") \
453 gtpu_vat_api_hookup (vat_main_t *vam)
455 gtpu_test_main_t * gtm = >pu_test_main;
456 /* Hook up handlers for replies from the data plane plug-in */
458 vl_msg_api_set_handlers((VL_API_##N + gtm->msg_id_base), \
460 vl_api_##n##_t_handler, \
462 vl_api_##n##_t_endian, \
463 vl_api_##n##_t_print, \
464 sizeof(vl_api_##n##_t), 1);
465 foreach_vpe_api_reply_msg;
468 /* API messages we can send */
469 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
474 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
479 clib_error_t * vat_plugin_register (vat_main_t *vam)
481 gtpu_test_main_t * gtm = >pu_test_main;
487 /* Ask the vpp engine for the first assigned message-id */
488 name = format (0, "gtpu_%08x%c", api_version, 0);
489 gtm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
491 if (gtm->msg_id_base != (u16) ~0)
492 gtpu_vat_api_hookup (vam);