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 <vlibsocket/api.h>
20 #include <vppinfra/error.h>
21 #include <gtpu/gtpu.h>
23 #define __plugin_msg_base gtpu_test_main.msg_id_base
24 #include <vlibapi/vat_helper_macros.h>
27 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
29 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
30 ip46_type_t type = va_arg (*args, ip46_type_t);
31 if ((type != IP46_TYPE_IP6) &&
32 unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
33 ip46_address_mask_ip4(ip46);
35 } else if ((type != IP46_TYPE_IP4) &&
36 unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
41 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
43 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
44 u8 *len = va_arg (*args, u8 *);
45 ip46_type_t type = va_arg (*args, ip46_type_t);
48 if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
52 ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
53 } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
62 /////////////////////////
64 #define vl_msg_id(n,h) n,
66 #include <gtpu/gtpu.api.h>
67 /* We'll want to know how many messages IDs we need... */
68 VL_MSG_FIRST_AVAILABLE,
72 /* define message structures */
74 #include <gtpu/gtpu.api.h>
77 /* declare message handlers for each api */
79 #define vl_endianfun /* define message structures */
80 #include <gtpu/gtpu.api.h>
83 /* instantiate all the print functions we know about */
84 #define vl_print(handle, ...)
86 #include <gtpu/gtpu.api.h>
89 /* Get the API version number. */
90 #define vl_api_version(n,v) static u32 api_version=(v);
91 #include <gtpu/gtpu.api.h>
95 /* API message ID base */
100 gtpu_test_main_t gtpu_test_main;
102 static void vl_api_gtpu_add_del_tunnel_reply_t_handler
103 (vl_api_gtpu_add_del_tunnel_reply_t * mp)
105 vat_main_t *vam = &vat_main;
106 i32 retval = ntohl (mp->retval);
109 vam->async_errors += (retval < 0);
113 vam->retval = retval;
114 vam->sw_if_index = ntohl (mp->sw_if_index);
115 vam->result_ready = 1;
120 #define foreach_standard_reply_retval_handler \
121 _(sw_interface_set_gtpu_bypass_reply)
124 static void vl_api_##n##_t_handler \
125 (vl_api_##n##_t * mp) \
127 vat_main_t * vam = gtpu_test_main.vat_main; \
128 i32 retval = ntohl(mp->retval); \
129 if (vam->async_mode) { \
130 vam->async_errors += (retval < 0); \
132 vam->retval = retval; \
133 vam->result_ready = 1; \
136 foreach_standard_reply_retval_handler;
140 * Table of message reply handlers, must include boilerplate handlers
143 #define foreach_vpe_api_reply_msg \
144 _(SW_INTERFACE_SET_GTPU_BYPASS_REPLY, sw_interface_set_gtpu_bypass_reply) \
145 _(GTPU_ADD_DEL_TUNNEL_REPLY, gtpu_add_del_tunnel_reply) \
146 _(GTPU_TUNNEL_DETAILS, gtpu_tunnel_details)
150 api_unformat_sw_if_index (unformat_input_t * input, va_list * args)
152 vat_main_t *vam = va_arg (*args, vat_main_t *);
153 u32 *result = va_arg (*args, u32 *);
157 if (!unformat (input, "%s", &if_name))
160 p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name);
168 api_sw_interface_set_gtpu_bypass (vat_main_t * vam)
170 unformat_input_t *i = vam->input;
171 vl_api_sw_interface_set_gtpu_bypass_t *mp;
173 u8 sw_if_index_set = 0;
178 /* Parse args required to build the message */
179 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
181 if (unformat (i, "%U", api_unformat_sw_if_index, vam, &sw_if_index))
183 else if (unformat (i, "sw_if_index %d", &sw_if_index))
185 else if (unformat (i, "enable"))
187 else if (unformat (i, "disable"))
189 else if (unformat (i, "ip4"))
191 else if (unformat (i, "ip6"))
197 if (sw_if_index_set == 0)
199 errmsg ("missing interface name or sw_if_index");
203 /* Construct the API message */
204 M (SW_INTERFACE_SET_GTPU_BYPASS, mp);
206 mp->sw_if_index = ntohl (sw_if_index);
207 mp->enable = is_enable;
208 mp->is_ipv6 = is_ipv6;
213 /* Wait for a reply... */
218 static uword unformat_gtpu_decap_next
219 (unformat_input_t * input, va_list * args)
221 u32 *result = va_arg (*args, u32 *);
224 if (unformat (input, "l2"))
225 *result = GTPU_INPUT_NEXT_L2_INPUT;
226 else if (unformat (input, "%d", &tmp))
234 api_gtpu_add_del_tunnel (vat_main_t * vam)
236 unformat_input_t *line_input = vam->input;
237 vl_api_gtpu_add_del_tunnel_t *mp;
238 ip46_address_t src, dst;
240 u8 ipv4_set = 0, ipv6_set = 0;
244 u32 mcast_sw_if_index = ~0;
245 u32 encap_vrf_id = 0;
246 u32 decap_next_index = ~0;
250 /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
251 memset (&src, 0, sizeof src);
252 memset (&dst, 0, sizeof dst);
254 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
256 if (unformat (line_input, "del"))
259 if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
265 if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
271 if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
277 if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
282 else if (unformat (line_input, "group %U %U",
283 unformat_ip4_address, &dst.ip4,
284 api_unformat_sw_if_index, vam, &mcast_sw_if_index))
286 grp_set = dst_set = 1;
289 else if (unformat (line_input, "group %U",
290 unformat_ip4_address, &dst.ip4))
292 grp_set = dst_set = 1;
295 else if (unformat (line_input, "group %U %U",
296 unformat_ip6_address, &dst.ip6,
297 api_unformat_sw_if_index, vam, &mcast_sw_if_index))
299 grp_set = dst_set = 1;
302 else if (unformat (line_input, "group %U",
303 unformat_ip6_address, &dst.ip6))
305 grp_set = dst_set = 1;
309 if (unformat (line_input, "mcast_sw_if_index %u", &mcast_sw_if_index))
311 else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
313 else if (unformat (line_input, "decap-next %U",
314 unformat_gtpu_decap_next, &decap_next_index))
316 else if (unformat (line_input, "teid %d", &teid))
320 errmsg ("parse error '%U'", format_unformat_error, line_input);
327 errmsg ("tunnel src address not specified");
332 errmsg ("tunnel dst address not specified");
336 if (grp_set && !ip46_address_is_multicast (&dst))
338 errmsg ("tunnel group address not multicast");
341 if (grp_set && mcast_sw_if_index == ~0)
343 errmsg ("tunnel nonexistent multicast device");
346 if (grp_set == 0 && ip46_address_is_multicast (&dst))
348 errmsg ("tunnel dst address must be unicast");
353 if (ipv4_set && ipv6_set)
355 errmsg ("both IPv4 and IPv6 addresses specified");
359 M (GTPU_ADD_DEL_TUNNEL, mp);
363 clib_memcpy (mp->src_address, &src.ip6, sizeof (src.ip6));
364 clib_memcpy (mp->dst_address, &dst.ip6, sizeof (dst.ip6));
368 clib_memcpy (mp->src_address, &src.ip4, sizeof (src.ip4));
369 clib_memcpy (mp->dst_address, &dst.ip4, sizeof (dst.ip4));
371 mp->encap_vrf_id = ntohl (encap_vrf_id);
372 mp->decap_next_index = ntohl (decap_next_index);
373 mp->mcast_sw_if_index = ntohl (mcast_sw_if_index);
374 mp->teid = ntohl (teid);
376 mp->is_ipv6 = ipv6_set;
383 static void vl_api_gtpu_tunnel_details_t_handler
384 (vl_api_gtpu_tunnel_details_t * mp)
386 vat_main_t *vam = &vat_main;
387 ip46_address_t src = to_ip46 (mp->is_ipv6, mp->dst_address);
388 ip46_address_t dst = to_ip46 (mp->is_ipv6, mp->src_address);
390 print (vam->ofp, "%11d%24U%24U%14d%18d%13d%19d",
391 ntohl (mp->sw_if_index),
392 format_ip46_address, &src, IP46_TYPE_ANY,
393 format_ip46_address, &dst, IP46_TYPE_ANY,
394 ntohl (mp->encap_vrf_id),
395 ntohl (mp->decap_next_index), ntohl (mp->teid),
396 ntohl (mp->mcast_sw_if_index));
400 api_gtpu_tunnel_dump (vat_main_t * vam)
402 unformat_input_t *i = vam->input;
403 vl_api_gtpu_tunnel_dump_t *mp;
405 u8 sw_if_index_set = 0;
408 /* Parse args required to build the message */
409 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
411 if (unformat (i, "sw_if_index %d", &sw_if_index))
417 if (sw_if_index_set == 0)
422 if (!vam->json_output)
424 print (vam->ofp, "%11s%24s%24s%14s%18s%13s%19s",
425 "sw_if_index", "src_address", "dst_address",
426 "encap_vrf_id", "decap_next_index", "teid", "mcast_sw_if_index");
429 /* Get list of gtpu-tunnel interfaces */
430 M (GTPU_TUNNEL_DUMP, mp);
432 mp->sw_if_index = htonl (sw_if_index);
441 * List of messages that the api test plugin sends,
442 * and that the data plane plugin processes
444 #define foreach_vpe_api_msg \
445 _(sw_interface_set_gtpu_bypass, \
446 "<intfc> | sw_if_index <id> [ip4 | ip6] [enable | disable]") \
447 _(gtpu_add_del_tunnel, \
448 "src <ip-addr> { dst <ip-addr> | group <mcast-ip-addr>\n" \
449 "{ <intfc> | mcast_sw_if_index <nn> } }\n" \
450 "teid <teid> [encap-vrf-id <nn>] [decap-next <l2|nn>] [del]") \
451 _(gtpu_tunnel_dump, "[<intfc> | sw_if_index <nn>]") \
454 gtpu_vat_api_hookup (vat_main_t *vam)
456 gtpu_test_main_t * gtm = >pu_test_main;
457 /* Hook up handlers for replies from the data plane plug-in */
459 vl_msg_api_set_handlers((VL_API_##N + gtm->msg_id_base), \
461 vl_api_##n##_t_handler, \
463 vl_api_##n##_t_endian, \
464 vl_api_##n##_t_print, \
465 sizeof(vl_api_##n##_t), 1);
466 foreach_vpe_api_reply_msg;
469 /* API messages we can send */
470 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
475 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
480 clib_error_t * vat_plugin_register (vat_main_t *vam)
482 gtpu_test_main_t * gtm = >pu_test_main;
488 /* Ask the vpp engine for the first assigned message-id */
489 name = format (0, "gtpu_%08x%c", api_version, 0);
490 gtm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
492 if (gtm->msg_id_base != (u16) ~0)
493 gtpu_vat_api_hookup (vam);