gtpu: remove api boilerplate
[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 #include <gtpu/gtpu.api_enum.h>
26 #include <gtpu/gtpu.api_types.h>
27
28 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
29 {
30   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
31   ip46_type_t type = va_arg (*args, ip46_type_t);
32   if ((type != IP46_TYPE_IP6) &&
33       unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
34     ip46_address_mask_ip4(ip46);
35     return 1;
36   } else if ((type != IP46_TYPE_IP4) &&
37       unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
38     return 1;
39   }
40   return 0;
41 }
42 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
43 {
44   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
45   u8 *len = va_arg (*args, u8 *);
46   ip46_type_t type = va_arg (*args, ip46_type_t);
47
48   u32 l;
49   if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
50     if (l > 32)
51       return 0;
52     *len = l + 96;
53     ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
54   } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
55     if (l > 128)
56       return 0;
57     *len = l;
58   } else {
59     return 0;
60   }
61   return 1;
62 }
63 /////////////////////////
64
65 typedef struct {
66     /* API message ID base */
67     u16 msg_id_base;
68     vat_main_t *vat_main;
69 } gtpu_test_main_t;
70
71 gtpu_test_main_t gtpu_test_main;
72
73 static void vl_api_gtpu_add_del_tunnel_reply_t_handler
74   (vl_api_gtpu_add_del_tunnel_reply_t * mp)
75 {
76   vat_main_t *vam = &vat_main;
77   i32 retval = ntohl (mp->retval);
78   if (vam->async_mode)
79     {
80       vam->async_errors += (retval < 0);
81     }
82   else
83     {
84       vam->retval = retval;
85       vam->sw_if_index = ntohl (mp->sw_if_index);
86       vam->result_ready = 1;
87     }
88 }
89
90 static uword
91 api_unformat_sw_if_index (unformat_input_t * input, va_list * args)
92 {
93   vat_main_t *vam = va_arg (*args, vat_main_t *);
94   u32 *result = va_arg (*args, u32 *);
95   u8 *if_name;
96   uword *p;
97
98   if (!unformat (input, "%s", &if_name))
99     return 0;
100
101   p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name);
102   if (p == 0)
103     return 0;
104   *result = p[0];
105   return 1;
106 }
107
108 static int
109 api_sw_interface_set_gtpu_bypass (vat_main_t * vam)
110 {
111   unformat_input_t *i = vam->input;
112   vl_api_sw_interface_set_gtpu_bypass_t *mp;
113   u32 sw_if_index = 0;
114   u8 sw_if_index_set = 0;
115   u8 is_enable = 1;
116   u8 is_ipv6 = 0;
117   int ret;
118
119   /* Parse args required to build the message */
120   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
121     {
122       if (unformat (i, "%U", api_unformat_sw_if_index, vam, &sw_if_index))
123       sw_if_index_set = 1;
124       else if (unformat (i, "sw_if_index %d", &sw_if_index))
125       sw_if_index_set = 1;
126       else if (unformat (i, "enable"))
127       is_enable = 1;
128       else if (unformat (i, "disable"))
129       is_enable = 0;
130       else if (unformat (i, "ip4"))
131       is_ipv6 = 0;
132       else if (unformat (i, "ip6"))
133       is_ipv6 = 1;
134       else
135       break;
136     }
137
138   if (sw_if_index_set == 0)
139     {
140       errmsg ("missing interface name or sw_if_index");
141       return -99;
142     }
143
144   /* Construct the API message */
145   M (SW_INTERFACE_SET_GTPU_BYPASS, mp);
146
147   mp->sw_if_index = ntohl (sw_if_index);
148   mp->enable = is_enable;
149   mp->is_ipv6 = is_ipv6;
150
151   /* send it... */
152   S (mp);
153
154   /* Wait for a reply... */
155   W (ret);
156   return ret;
157 }
158
159 static uword unformat_gtpu_decap_next
160   (unformat_input_t * input, va_list * args)
161 {
162   u32 *result = va_arg (*args, u32 *);
163   u32 tmp;
164
165   if (unformat (input, "l2"))
166     *result = GTPU_INPUT_NEXT_L2_INPUT;
167   else if (unformat (input, "%d", &tmp))
168     *result = tmp;
169   else
170     return 0;
171   return 1;
172 }
173
174 static int
175 api_gtpu_add_del_tunnel (vat_main_t * vam)
176 {
177   unformat_input_t *line_input = vam->input;
178   vl_api_gtpu_add_del_tunnel_t *mp;
179   ip46_address_t src, dst;
180   u8 is_add = 1;
181   u8 ipv4_set = 0, ipv6_set = 0;
182   u8 src_set = 0;
183   u8 dst_set = 0;
184   u8 grp_set = 0;
185   u32 mcast_sw_if_index = ~0;
186   u32 encap_vrf_id = 0;
187   u32 decap_next_index = ~0;
188   u32 teid = 0;
189   int ret;
190
191   /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
192   clib_memset (&src, 0, sizeof src);
193   clib_memset (&dst, 0, sizeof dst);
194
195   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
196     {
197       if (unformat (line_input, "del"))
198       is_add = 0;
199       else
200       if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
201       {
202         ipv4_set = 1;
203         src_set = 1;
204       }
205       else
206       if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
207       {
208         ipv4_set = 1;
209         dst_set = 1;
210       }
211       else
212       if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
213       {
214         ipv6_set = 1;
215         src_set = 1;
216       }
217       else
218       if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
219       {
220         ipv6_set = 1;
221         dst_set = 1;
222       }
223       else if (unformat (line_input, "group %U %U",
224                        unformat_ip4_address, &dst.ip4,
225                        api_unformat_sw_if_index, vam, &mcast_sw_if_index))
226       {
227         grp_set = dst_set = 1;
228         ipv4_set = 1;
229       }
230       else if (unformat (line_input, "group %U",
231                        unformat_ip4_address, &dst.ip4))
232       {
233         grp_set = dst_set = 1;
234         ipv4_set = 1;
235       }
236       else if (unformat (line_input, "group %U %U",
237                        unformat_ip6_address, &dst.ip6,
238                        api_unformat_sw_if_index, vam, &mcast_sw_if_index))
239       {
240         grp_set = dst_set = 1;
241         ipv6_set = 1;
242       }
243       else if (unformat (line_input, "group %U",
244                        unformat_ip6_address, &dst.ip6))
245       {
246         grp_set = dst_set = 1;
247         ipv6_set = 1;
248       }
249       else
250       if (unformat (line_input, "mcast_sw_if_index %u", &mcast_sw_if_index))
251       ;
252       else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id))
253       ;
254       else if (unformat (line_input, "decap-next %U",
255                        unformat_gtpu_decap_next, &decap_next_index))
256       ;
257       else if (unformat (line_input, "teid %d", &teid))
258       ;
259       else
260       {
261         errmsg ("parse error '%U'", format_unformat_error, line_input);
262         return -99;
263       }
264     }
265
266   if (src_set == 0)
267     {
268       errmsg ("tunnel src address not specified");
269       return -99;
270     }
271   if (dst_set == 0)
272     {
273       errmsg ("tunnel dst address not specified");
274       return -99;
275     }
276
277   if (grp_set && !ip46_address_is_multicast (&dst))
278     {
279       errmsg ("tunnel group address not multicast");
280       return -99;
281     }
282   if (grp_set && mcast_sw_if_index == ~0)
283     {
284       errmsg ("tunnel nonexistent multicast device");
285       return -99;
286     }
287   if (grp_set == 0 && ip46_address_is_multicast (&dst))
288     {
289       errmsg ("tunnel dst address must be unicast");
290       return -99;
291     }
292
293
294   if (ipv4_set && ipv6_set)
295     {
296       errmsg ("both IPv4 and IPv6 addresses specified");
297       return -99;
298     }
299
300   M (GTPU_ADD_DEL_TUNNEL, mp);
301
302   if (ipv6_set)
303     {
304       clib_memcpy (mp->src_address, &src.ip6, sizeof (src.ip6));
305       clib_memcpy (mp->dst_address, &dst.ip6, sizeof (dst.ip6));
306     }
307   else
308     {
309       clib_memcpy (mp->src_address, &src.ip4, sizeof (src.ip4));
310       clib_memcpy (mp->dst_address, &dst.ip4, sizeof (dst.ip4));
311     }
312   mp->encap_vrf_id = ntohl (encap_vrf_id);
313   mp->decap_next_index = ntohl (decap_next_index);
314   mp->mcast_sw_if_index = ntohl (mcast_sw_if_index);
315   mp->teid = ntohl (teid);
316   mp->is_add = is_add;
317   mp->is_ipv6 = ipv6_set;
318
319   S (mp);
320   W (ret);
321   return ret;
322 }
323
324 static void vl_api_gtpu_tunnel_details_t_handler
325   (vl_api_gtpu_tunnel_details_t * mp)
326 {
327   vat_main_t *vam = &vat_main;
328   ip46_address_t src = to_ip46 (mp->is_ipv6, mp->dst_address);
329   ip46_address_t dst = to_ip46 (mp->is_ipv6, mp->src_address);
330
331   print (vam->ofp, "%11d%24U%24U%14d%18d%13d%19d",
332        ntohl (mp->sw_if_index),
333        format_ip46_address, &src, IP46_TYPE_ANY,
334        format_ip46_address, &dst, IP46_TYPE_ANY,
335        ntohl (mp->encap_vrf_id),
336        ntohl (mp->decap_next_index), ntohl (mp->teid),
337        ntohl (mp->mcast_sw_if_index));
338 }
339
340 static int
341 api_gtpu_tunnel_dump (vat_main_t * vam)
342 {
343   unformat_input_t *i = vam->input;
344   vl_api_gtpu_tunnel_dump_t *mp;
345   u32 sw_if_index;
346   u8 sw_if_index_set = 0;
347   int ret;
348
349   /* Parse args required to build the message */
350   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
351     {
352       if (unformat (i, "sw_if_index %d", &sw_if_index))
353       sw_if_index_set = 1;
354       else
355       break;
356     }
357
358   if (sw_if_index_set == 0)
359     {
360       sw_if_index = ~0;
361     }
362
363   if (!vam->json_output)
364     {
365       print (vam->ofp, "%11s%24s%24s%14s%18s%13s%19s",
366            "sw_if_index", "src_address", "dst_address",
367            "encap_vrf_id", "decap_next_index", "teid", "mcast_sw_if_index");
368     }
369
370   /* Get list of gtpu-tunnel interfaces */
371   M (GTPU_TUNNEL_DUMP, mp);
372
373   mp->sw_if_index = htonl (sw_if_index);
374
375   S (mp);
376
377   W (ret);
378   return ret;
379 }
380
381 #include <gtpu/gtpu.api_test.c>