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