LCOV - code coverage report
Current view: top level - plugins/gtpu - gtpu_test.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 0 232 0.0 %
Date: 2023-07-05 22:20:52 Functions: 0 11 0.0 %

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

Generated by: LCOV version 1.14