interface: add api test file
[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 <unistd.h>
17 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21 #include <gtpu/gtpu.h>
22 #include <vnet/ip/ip_types_api.h>
23
24 #define __plugin_msg_base gtpu_test_main.msg_id_base
25 #include <vlibapi/vat_helper_macros.h>
26
27 #include <vnet/format_fns.h>
28 #include <gtpu/gtpu.api_enum.h>
29 #include <gtpu/gtpu.api_types.h>
30
31 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
32 {
33   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
34   ip46_type_t type = va_arg (*args, ip46_type_t);
35   if ((type != IP46_TYPE_IP6) &&
36       unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
37     ip46_address_mask_ip4(ip46);
38     return 1;
39   } else if ((type != IP46_TYPE_IP4) &&
40       unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
41     return 1;
42   }
43   return 0;
44 }
45 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
46 {
47   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
48   u8 *len = va_arg (*args, u8 *);
49   ip46_type_t type = va_arg (*args, ip46_type_t);
50
51   u32 l;
52   if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
53     if (l > 32)
54       return 0;
55     *len = l + 96;
56     ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
57   } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
58     if (l > 128)
59       return 0;
60     *len = l;
61   } else {
62     return 0;
63   }
64   return 1;
65 }
66 /////////////////////////
67
68 typedef struct {
69     /* API message ID base */
70     u16 msg_id_base;
71     vat_main_t *vat_main;
72 } gtpu_test_main_t;
73
74 gtpu_test_main_t gtpu_test_main;
75
76 static void vl_api_gtpu_add_del_tunnel_reply_t_handler
77   (vl_api_gtpu_add_del_tunnel_reply_t * mp)
78 {
79   vat_main_t *vam = &vat_main;
80   i32 retval = ntohl (mp->retval);
81   if (vam->async_mode)
82     {
83       vam->async_errors += (retval < 0);
84     }
85   else
86     {
87       vam->retval = retval;
88       vam->sw_if_index = ntohl (mp->sw_if_index);
89       vam->result_ready = 1;
90     }
91 }
92
93 static uword
94 api_unformat_hw_if_index (unformat_input_t * input, va_list * args)
95 {
96   return 0;
97 }
98
99 static int
100 api_sw_interface_set_gtpu_bypass (vat_main_t * vam)
101 {
102   unformat_input_t *i = vam->input;
103   vl_api_sw_interface_set_gtpu_bypass_t *mp;
104   u32 sw_if_index = 0;
105   u8 sw_if_index_set = 0;
106   u8 is_enable = 1;
107   u8 is_ipv6 = 0;
108   int ret;
109
110   /* Parse args required to build the message */
111   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
112     {
113       if (unformat (i, "%U", api_unformat_sw_if_index, vam, &sw_if_index))
114       sw_if_index_set = 1;
115       else if (unformat (i, "sw_if_index %d", &sw_if_index))
116       sw_if_index_set = 1;
117       else if (unformat (i, "enable"))
118       is_enable = 1;
119       else if (unformat (i, "disable"))
120       is_enable = 0;
121       else if (unformat (i, "ip4"))
122       is_ipv6 = 0;
123       else if (unformat (i, "ip6"))
124       is_ipv6 = 1;
125       else
126       break;
127     }
128
129   if (sw_if_index_set == 0)
130     {
131       errmsg ("missing interface name or sw_if_index");
132       return -99;
133     }
134
135   /* Construct the API message */
136   M (SW_INTERFACE_SET_GTPU_BYPASS, mp);
137
138   mp->sw_if_index = ntohl (sw_if_index);
139   mp->enable = is_enable;
140   mp->is_ipv6 = is_ipv6;
141
142   /* send it... */
143   S (mp);
144
145   /* Wait for a reply... */
146   W (ret);
147   return ret;
148 }
149
150 static uword unformat_gtpu_decap_next
151   (unformat_input_t * input, va_list * args)
152 {
153   u32 *result = va_arg (*args, u32 *);
154   u32 tmp;
155
156   if (unformat (input, "l2"))
157     *result = GTPU_INPUT_NEXT_L2_INPUT;
158   else if (unformat (input, "%d", &tmp))
159     *result = tmp;
160   else
161     return 0;
162   return 1;
163 }
164
165 static int
166 api_gtpu_offload_rx (vat_main_t * vam)
167 {
168   unformat_input_t *line_input = vam->input;
169   vl_api_gtpu_offload_rx_t *mp;
170   u32 rx_sw_if_index = ~0;
171   u32 hw_if_index = ~0;
172   int is_add = 1;
173   int ret;
174
175   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
176     {
177                   if (unformat (line_input, "hw %U", api_unformat_hw_if_index, vam, &hw_if_index))
178                     ;
179                   else
180                   if (unformat (line_input, "rx %U", api_unformat_sw_if_index, vam, &rx_sw_if_index))
181                     ;
182                   else
183       if (unformat (line_input, "del"))
184       {
185         is_add = 0;
186         continue;
187             }
188       else
189       {
190         errmsg ("parse error '%U'", format_unformat_error, line_input);
191         return -99;
192       }
193     }
194
195   if (rx_sw_if_index == ~0)
196     {
197       errmsg ("missing rx interface");
198       return -99;
199     }
200
201   if (hw_if_index == ~0)
202     {
203       errmsg ("missing hw interface");
204       return -99;
205     }
206
207   M (GTPU_OFFLOAD_RX, mp);
208   mp->hw_if_index = ntohl (hw_if_index);
209   mp->sw_if_index = ntohl (rx_sw_if_index);
210   mp->enable = is_add;
211
212   S (mp);
213   W (ret);
214   return ret;
215 }
216
217 static int
218 api_gtpu_add_del_tunnel (vat_main_t * vam)
219 {
220   unformat_input_t *line_input = vam->input;
221   vl_api_gtpu_add_del_tunnel_t *mp;
222   ip46_address_t src, dst;
223   u8 is_add = 1;
224   u8 ipv4_set = 0, ipv6_set = 0;
225   u8 src_set = 0;
226   u8 dst_set = 0;
227   u8 grp_set = 0;
228   u32 mcast_sw_if_index = ~0;
229   u32 encap_vrf_id = 0;
230   u32 decap_next_index = ~0;
231   u32 teid = 0, tteid = 0;
232   int ret;
233
234   /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
235   clib_memset (&src, 0, sizeof src);
236   clib_memset (&dst, 0, sizeof dst);
237
238   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
239     {
240       if (unformat (line_input, "del"))
241       is_add = 0;
242       else
243       if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
244       {
245         ipv4_set = 1;
246         src_set = 1;
247       }
248       else
249       if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
250       {
251         ipv4_set = 1;
252         dst_set = 1;
253       }
254       else
255       if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
256       {
257         ipv6_set = 1;
258         src_set = 1;
259       }
260       else
261       if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
262       {
263         ipv6_set = 1;
264         dst_set = 1;
265       }
266       else if (unformat (line_input, "group %U %U",
267                        unformat_ip4_address, &dst.ip4,
268                        api_unformat_sw_if_index, vam, &mcast_sw_if_index))
269       {
270         grp_set = dst_set = 1;
271         ipv4_set = 1;
272       }
273       else if (unformat (line_input, "group %U",
274                        unformat_ip4_address, &dst.ip4))
275       {
276         grp_set = dst_set = 1;
277         ipv4_set = 1;
278       }
279       else if (unformat (line_input, "group %U %U",
280                        unformat_ip6_address, &dst.ip6,
281                        api_unformat_sw_if_index, vam, &mcast_sw_if_index))
282       {
283         grp_set = dst_set = 1;
284         ipv6_set = 1;
285       }
286       else if (unformat (line_input, "group %U",
287                        unformat_ip6_address, &dst.ip6))
288       {
289         grp_set = dst_set = 1;
290         ipv6_set = 1;
291       }
292       else
293       if (unformat (line_input, "mcast_sw_if_index %u", &mcast_sw_if_index))
294       ;
295       else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
296       ;
297       else if (unformat (line_input, "decap-next %U",
298                        unformat_gtpu_decap_next, &decap_next_index))
299       ;
300       else if (unformat (line_input, "teid %d", &teid))
301       ;
302       else if (unformat (line_input, "tteid %d", &tteid))
303       ;
304       else
305       {
306         errmsg ("parse error '%U'", format_unformat_error, line_input);
307         return -99;
308       }
309     }
310
311   if (is_add && src_set == 0)
312     {
313       errmsg ("tunnel src address not specified");
314       return -99;
315     }
316   if (dst_set == 0)
317     {
318       errmsg ("tunnel dst address not specified");
319       return -99;
320     }
321
322   if (grp_set && !ip46_address_is_multicast (&dst))
323     {
324       errmsg ("tunnel group address not multicast");
325       return -99;
326     }
327   if (grp_set && mcast_sw_if_index == ~0)
328     {
329       errmsg ("tunnel nonexistent multicast device");
330       return -99;
331     }
332   if (grp_set == 0 && ip46_address_is_multicast (&dst))
333     {
334       errmsg ("tunnel dst address must be unicast");
335       return -99;
336     }
337
338
339   if (ipv4_set && ipv6_set)
340     {
341       errmsg ("both IPv4 and IPv6 addresses specified");
342       return -99;
343     }
344
345   M (GTPU_ADD_DEL_TUNNEL, mp);
346
347   ip_address_encode(&src, ipv6_set ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
348                     &mp->src_address);
349   ip_address_encode(&dst, ipv6_set ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
350                     &mp->dst_address);
351   mp->encap_vrf_id = ntohl (encap_vrf_id);
352   mp->decap_next_index = ntohl (decap_next_index);
353   mp->mcast_sw_if_index = ntohl (mcast_sw_if_index);
354   mp->teid = ntohl (teid);
355   mp->tteid = ntohl (tteid);
356   mp->is_add = is_add;
357
358   S (mp);
359   W (ret);
360   return ret;
361 }
362
363 static int
364 api_gtpu_tunnel_update_tteid (vat_main_t * vam)
365 {
366   unformat_input_t *line_input = vam->input;
367   vl_api_gtpu_tunnel_update_tteid_t *mp;
368   ip46_address_t dst;
369   u8 ipv6_set = 0;
370   u8 dst_set = 0;
371   u32 encap_vrf_id = 0;
372   u32 teid = 0, tteid = 0;
373   int ret;
374
375   /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
376   clib_memset (&dst, 0, sizeof dst);
377
378   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
379     {
380       if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
381       {
382         dst_set = 1;
383       }
384       else if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
385       {
386         ipv6_set = 1;
387         dst_set = 1;
388       }
389       else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
390       ;
391       else if (unformat (line_input, "teid %d", &teid))
392       ;
393       else if (unformat (line_input, "tteid %d", &tteid))
394       ;
395       else
396       {
397         errmsg ("parse error '%U'", format_unformat_error, line_input);
398         return -99;
399       }
400     }
401
402   if (dst_set == 0)
403     {
404       errmsg ("tunnel dst address not specified");
405       return -99;
406     }
407
408   M (GTPU_TUNNEL_UPDATE_TTEID, mp);
409
410   ip_address_encode(&dst, ipv6_set ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
411                     &mp->dst_address);
412   mp->encap_vrf_id = ntohl (encap_vrf_id);
413   mp->teid = ntohl (teid);
414   mp->tteid = ntohl (tteid);
415
416   S (mp);
417   W (ret);
418   return ret;
419 }
420
421 static void vl_api_gtpu_tunnel_details_t_handler
422   (vl_api_gtpu_tunnel_details_t * mp)
423 {
424   vat_main_t *vam = &vat_main;
425   ip46_address_t src;
426   ip46_address_t dst;
427   ip_address_decode(&mp->dst_address, &dst);
428   ip_address_decode(&mp->src_address, &src);
429   print (vam->ofp, "%11d%24U%24U%14d%18d%13d%13d%19d",
430          ntohl (mp->sw_if_index),
431          format_ip46_address, &src, IP46_TYPE_ANY,
432          format_ip46_address, &dst, IP46_TYPE_ANY,
433          ntohl (mp->encap_vrf_id),
434          ntohl (mp->decap_next_index),
435          ntohl (mp->teid), ntohl (mp->tteid),
436          ntohl (mp->mcast_sw_if_index));
437 }
438
439 static int
440 api_gtpu_tunnel_dump (vat_main_t * vam)
441 {
442   unformat_input_t *i = vam->input;
443   vl_api_gtpu_tunnel_dump_t *mp;
444   u32 sw_if_index;
445   u8 sw_if_index_set = 0;
446
447   /* Parse args required to build the message */
448   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
449     {
450       if (unformat (i, "sw_if_index %d", &sw_if_index))
451       sw_if_index_set = 1;
452       else
453       break;
454     }
455
456   if (sw_if_index_set == 0)
457     {
458       sw_if_index = ~0;
459     }
460
461   if (!vam->json_output)
462     {
463       print (vam->ofp, "%11s%24s%24s%14s%18s%13s%13s%19s",
464              "sw_if_index", "src_address", "dst_address",
465              "encap_vrf_id", "decap_next_index", "teid", "tteid",
466              "mcast_sw_if_index");
467     }
468
469   /* Get list of gtpu-tunnel interfaces */
470   M (GTPU_TUNNEL_DUMP, mp);
471
472   mp->sw_if_index = htonl (sw_if_index);
473
474   S (mp);
475
476   /* No status response for this API call.
477    * Wait 1 sec for any dump output before return to vat# */
478   sleep (1);
479   
480   return 0;
481 }
482
483 #include <gtpu/gtpu.api_test.c>