1eb54d6a6b867919a34e2237ba97d360bea1df54
[vpp.git] / src / plugins / gtpu / gtpu_test.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vat/vat.h>
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 #include <vppinfra/error.h>
20 #include <gtpu/gtpu.h>
21
22 #define __plugin_msg_base gtpu_test_main.msg_id_base
23 #include <vlibapi/vat_helper_macros.h>
24
25
26 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
27 {
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);
33     return 1;
34   } else if ((type != IP46_TYPE_IP4) &&
35       unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
36     return 1;
37   }
38   return 0;
39 }
40 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
41 {
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);
45
46   u32 l;
47   if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
48     if (l > 32)
49       return 0;
50     *len = l + 96;
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)) {
53     if (l > 128)
54       return 0;
55     *len = l;
56   } else {
57     return 0;
58   }
59   return 1;
60 }
61 /////////////////////////
62
63 #define vl_msg_id(n,h) n,
64 typedef enum {
65 #include <gtpu/gtpu.api.h>
66     /* We'll want to know how many messages IDs we need... */
67     VL_MSG_FIRST_AVAILABLE,
68 } vl_msg_id_t;
69 #undef vl_msg_id
70
71 /* define message structures */
72 #define vl_typedefs
73 #include <gtpu/gtpu.api.h>
74 #undef vl_typedefs
75
76 /* declare message handlers for each api */
77
78 #define vl_endianfun             /* define message structures */
79 #include <gtpu/gtpu.api.h>
80 #undef vl_endianfun
81
82 /* instantiate all the print functions we know about */
83 #define vl_print(handle, ...)
84 #define vl_printfun
85 #include <gtpu/gtpu.api.h>
86 #undef vl_printfun
87
88 /* Get the API version number. */
89 #define vl_api_version(n,v) static u32 api_version=(v);
90 #include <gtpu/gtpu.api.h>
91 #undef vl_api_version
92
93 typedef struct {
94     /* API message ID base */
95     u16 msg_id_base;
96     vat_main_t *vat_main;
97 } gtpu_test_main_t;
98
99 gtpu_test_main_t gtpu_test_main;
100
101 static void vl_api_gtpu_add_del_tunnel_reply_t_handler
102   (vl_api_gtpu_add_del_tunnel_reply_t * mp)
103 {
104   vat_main_t *vam = &vat_main;
105   i32 retval = ntohl (mp->retval);
106   if (vam->async_mode)
107     {
108       vam->async_errors += (retval < 0);
109     }
110   else
111     {
112       vam->retval = retval;
113       vam->sw_if_index = ntohl (mp->sw_if_index);
114       vam->result_ready = 1;
115     }
116 }
117
118
119 #define foreach_standard_reply_retval_handler   \
120     _(sw_interface_set_gtpu_bypass_reply)
121
122 #define _(n)                                            \
123     static void vl_api_##n##_t_handler                  \
124     (vl_api_##n##_t * mp)                               \
125     {                                                   \
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);          \
130         } else {                                        \
131             vam->retval = retval;                       \
132             vam->result_ready = 1;                      \
133         }                                               \
134     }
135   foreach_standard_reply_retval_handler;
136 #undef _
137
138 /*
139  * Table of message reply handlers, must include boilerplate handlers
140  * we just generated
141  */
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)
146
147
148 static uword
149 api_unformat_sw_if_index (unformat_input_t * input, va_list * args)
150 {
151   vat_main_t *vam = va_arg (*args, vat_main_t *);
152   u32 *result = va_arg (*args, u32 *);
153   u8 *if_name;
154   uword *p;
155
156   if (!unformat (input, "%s", &if_name))
157     return 0;
158
159   p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name);
160   if (p == 0)
161     return 0;
162   *result = p[0];
163   return 1;
164 }
165
166 static int
167 api_sw_interface_set_gtpu_bypass (vat_main_t * vam)
168 {
169   unformat_input_t *i = vam->input;
170   vl_api_sw_interface_set_gtpu_bypass_t *mp;
171   u32 sw_if_index = 0;
172   u8 sw_if_index_set = 0;
173   u8 is_enable = 1;
174   u8 is_ipv6 = 0;
175   int ret;
176
177   /* Parse args required to build the message */
178   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
179     {
180       if (unformat (i, "%U", api_unformat_sw_if_index, vam, &sw_if_index))
181       sw_if_index_set = 1;
182       else if (unformat (i, "sw_if_index %d", &sw_if_index))
183       sw_if_index_set = 1;
184       else if (unformat (i, "enable"))
185       is_enable = 1;
186       else if (unformat (i, "disable"))
187       is_enable = 0;
188       else if (unformat (i, "ip4"))
189       is_ipv6 = 0;
190       else if (unformat (i, "ip6"))
191       is_ipv6 = 1;
192       else
193       break;
194     }
195
196   if (sw_if_index_set == 0)
197     {
198       errmsg ("missing interface name or sw_if_index");
199       return -99;
200     }
201
202   /* Construct the API message */
203   M (SW_INTERFACE_SET_GTPU_BYPASS, mp);
204
205   mp->sw_if_index = ntohl (sw_if_index);
206   mp->enable = is_enable;
207   mp->is_ipv6 = is_ipv6;
208
209   /* send it... */
210   S (mp);
211
212   /* Wait for a reply... */
213   W (ret);
214   return ret;
215 }
216
217 static uword unformat_gtpu_decap_next
218   (unformat_input_t * input, va_list * args)
219 {
220   u32 *result = va_arg (*args, u32 *);
221   u32 tmp;
222
223   if (unformat (input, "l2"))
224     *result = GTPU_INPUT_NEXT_L2_INPUT;
225   else if (unformat (input, "%d", &tmp))
226     *result = tmp;
227   else
228     return 0;
229   return 1;
230 }
231
232 static int
233 api_gtpu_add_del_tunnel (vat_main_t * vam)
234 {
235   unformat_input_t *line_input = vam->input;
236   vl_api_gtpu_add_del_tunnel_t *mp;
237   ip46_address_t src, dst;
238   u8 is_add = 1;
239   u8 ipv4_set = 0, ipv6_set = 0;
240   u8 src_set = 0;
241   u8 dst_set = 0;
242   u8 grp_set = 0;
243   u32 mcast_sw_if_index = ~0;
244   u32 encap_vrf_id = 0;
245   u32 decap_next_index = ~0;
246   u32 teid = 0;
247   int ret;
248
249   /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
250   memset (&src, 0, sizeof src);
251   memset (&dst, 0, sizeof dst);
252
253   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
254     {
255       if (unformat (line_input, "del"))
256       is_add = 0;
257       else
258       if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
259       {
260         ipv4_set = 1;
261         src_set = 1;
262       }
263       else
264       if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
265       {
266         ipv4_set = 1;
267         dst_set = 1;
268       }
269       else
270       if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
271       {
272         ipv6_set = 1;
273         src_set = 1;
274       }
275       else
276       if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
277       {
278         ipv6_set = 1;
279         dst_set = 1;
280       }
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))
284       {
285         grp_set = dst_set = 1;
286         ipv4_set = 1;
287       }
288       else if (unformat (line_input, "group %U",
289                        unformat_ip4_address, &dst.ip4))
290       {
291         grp_set = dst_set = 1;
292         ipv4_set = 1;
293       }
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))
297       {
298         grp_set = dst_set = 1;
299         ipv6_set = 1;
300       }
301       else if (unformat (line_input, "group %U",
302                        unformat_ip6_address, &dst.ip6))
303       {
304         grp_set = dst_set = 1;
305         ipv6_set = 1;
306       }
307       else
308       if (unformat (line_input, "mcast_sw_if_index %u", &mcast_sw_if_index))
309       ;
310       else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
311       ;
312       else if (unformat (line_input, "decap-next %U",
313                        unformat_gtpu_decap_next, &decap_next_index))
314       ;
315       else if (unformat (line_input, "teid %d", &teid))
316       ;
317       else
318       {
319         errmsg ("parse error '%U'", format_unformat_error, line_input);
320         return -99;
321       }
322     }
323
324   if (src_set == 0)
325     {
326       errmsg ("tunnel src address not specified");
327       return -99;
328     }
329   if (dst_set == 0)
330     {
331       errmsg ("tunnel dst address not specified");
332       return -99;
333     }
334
335   if (grp_set && !ip46_address_is_multicast (&dst))
336     {
337       errmsg ("tunnel group address not multicast");
338       return -99;
339     }
340   if (grp_set && mcast_sw_if_index == ~0)
341     {
342       errmsg ("tunnel nonexistent multicast device");
343       return -99;
344     }
345   if (grp_set == 0 && ip46_address_is_multicast (&dst))
346     {
347       errmsg ("tunnel dst address must be unicast");
348       return -99;
349     }
350
351
352   if (ipv4_set && ipv6_set)
353     {
354       errmsg ("both IPv4 and IPv6 addresses specified");
355       return -99;
356     }
357
358   M (GTPU_ADD_DEL_TUNNEL, mp);
359
360   if (ipv6_set)
361     {
362       clib_memcpy (mp->src_address, &src.ip6, sizeof (src.ip6));
363       clib_memcpy (mp->dst_address, &dst.ip6, sizeof (dst.ip6));
364     }
365   else
366     {
367       clib_memcpy (mp->src_address, &src.ip4, sizeof (src.ip4));
368       clib_memcpy (mp->dst_address, &dst.ip4, sizeof (dst.ip4));
369     }
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);
374   mp->is_add = is_add;
375   mp->is_ipv6 = ipv6_set;
376
377   S (mp);
378   W (ret);
379   return ret;
380 }
381
382 static void vl_api_gtpu_tunnel_details_t_handler
383   (vl_api_gtpu_tunnel_details_t * mp)
384 {
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);
388
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));
396 }
397
398 static int
399 api_gtpu_tunnel_dump (vat_main_t * vam)
400 {
401   unformat_input_t *i = vam->input;
402   vl_api_gtpu_tunnel_dump_t *mp;
403   u32 sw_if_index;
404   u8 sw_if_index_set = 0;
405   int ret;
406
407   /* Parse args required to build the message */
408   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
409     {
410       if (unformat (i, "sw_if_index %d", &sw_if_index))
411       sw_if_index_set = 1;
412       else
413       break;
414     }
415
416   if (sw_if_index_set == 0)
417     {
418       sw_if_index = ~0;
419     }
420
421   if (!vam->json_output)
422     {
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");
426     }
427
428   /* Get list of gtpu-tunnel interfaces */
429   M (GTPU_TUNNEL_DUMP, mp);
430
431   mp->sw_if_index = htonl (sw_if_index);
432
433   S (mp);
434
435   W (ret);
436   return ret;
437 }
438
439 /*
440  * List of messages that the api test plugin sends,
441  * and that the data plane plugin processes
442  */
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>]")                    \
451
452 static void
453 gtpu_vat_api_hookup (vat_main_t *vam)
454 {
455   gtpu_test_main_t * gtm = &gtpu_test_main;
456   /* Hook up handlers for replies from the data plane plug-in */
457 #define _(N,n)                                                  \
458   vl_msg_api_set_handlers((VL_API_##N + gtm->msg_id_base),       \
459                           #n,                                   \
460                           vl_api_##n##_t_handler,               \
461                           vl_noop_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;
466 #undef _
467
468   /* API messages we can send */
469 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
470   foreach_vpe_api_msg;
471 #undef _
472
473   /* Help strings */
474 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
475   foreach_vpe_api_msg;
476 #undef _
477 }
478
479 clib_error_t * vat_plugin_register (vat_main_t *vam)
480 {
481   gtpu_test_main_t * gtm = &gtpu_test_main;
482
483   u8 * name;
484
485   gtm->vat_main = vam;
486
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);
490
491   if (gtm->msg_id_base != (u16) ~0)
492     gtpu_vat_api_hookup (vam);
493
494   vec_free(name);
495
496   return 0;
497 }