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