LCOV - code coverage report
Current view: top level - vnet/vxlan-gpe - vxlan_gpe_api.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 6 157 3.8 %
Date: 2023-10-26 01:39:38 Functions: 3 10 30.0 %

          Line data    Source code
       1             : /*
       2             :  *------------------------------------------------------------------
       3             :  * vxlan_gpe_api.c - vxlan_gpe api
       4             :  *
       5             :  * Copyright (c) 2016 Cisco and/or its affiliates.
       6             :  * Licensed under the Apache License, Version 2.0 (the "License");
       7             :  * you may not use this file except in compliance with the License.
       8             :  * You may obtain a copy of the License at:
       9             :  *
      10             :  *     http://www.apache.org/licenses/LICENSE-2.0
      11             :  *
      12             :  * Unless required by applicable law or agreed to in writing, software
      13             :  * distributed under the License is distributed on an "AS IS" BASIS,
      14             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      15             :  * See the License for the specific language governing permissions and
      16             :  * limitations under the License.
      17             :  *------------------------------------------------------------------
      18             :  */
      19             : 
      20             : #include <vnet/vnet.h>
      21             : #include <vlibmemory/api.h>
      22             : 
      23             : #include <vnet/interface.h>
      24             : #include <vnet/api_errno.h>
      25             : #include <vnet/feature/feature.h>
      26             : #include <vnet/vxlan-gpe/vxlan_gpe.h>
      27             : #include <vnet/fib/fib_table.h>
      28             : #include <vnet/format_fns.h>
      29             : 
      30             : #include <vnet/ip/ip_types_api.h>
      31             : #include <vnet/vxlan-gpe/vxlan_gpe.api_enum.h>
      32             : #include <vnet/vxlan-gpe/vxlan_gpe.api_types.h>
      33             : 
      34             : #define REPLY_MSG_ID_BASE msg_id_base
      35             : #include <vlibapi/api_helper_macros.h>
      36             : 
      37             : static u16 msg_id_base;
      38             : 
      39             : static void
      40           0 :   vl_api_sw_interface_set_vxlan_gpe_bypass_t_handler
      41             :   (vl_api_sw_interface_set_vxlan_gpe_bypass_t * mp)
      42             : {
      43             :   vl_api_sw_interface_set_vxlan_gpe_bypass_reply_t *rmp;
      44           0 :   int rv = 0;
      45           0 :   u32 sw_if_index = ntohl (mp->sw_if_index);
      46             : 
      47           0 :   VALIDATE_SW_IF_INDEX (mp);
      48             : 
      49           0 :   vnet_int_vxlan_gpe_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
      50           0 :   BAD_SW_IF_INDEX_LABEL;
      51             : 
      52           0 :   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VXLAN_GPE_BYPASS_REPLY);
      53             : }
      54             : 
      55             : static void
      56           0 :   vl_api_vxlan_gpe_add_del_tunnel_t_handler
      57             :   (vl_api_vxlan_gpe_add_del_tunnel_t * mp)
      58             : {
      59             :   vl_api_vxlan_gpe_add_del_tunnel_reply_t *rmp;
      60           0 :   int rv = 0;
      61           0 :   vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
      62             :   u32 encap_fib_index, decap_fib_index;
      63             :   u8 protocol;
      64             :   uword *p;
      65           0 :   ip4_main_t *im = &ip4_main;
      66           0 :   u32 sw_if_index = ~0;
      67             : 
      68           0 :   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
      69           0 :   if (!p)
      70             :     {
      71           0 :       rv = VNET_API_ERROR_NO_SUCH_FIB;
      72           0 :       goto out;
      73             :     }
      74           0 :   encap_fib_index = p[0];
      75             : 
      76           0 :   protocol = mp->protocol;
      77             : 
      78             :   /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
      79           0 :   if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT)
      80             :     {
      81           0 :       p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id));
      82           0 :       if (!p)
      83             :         {
      84           0 :           rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
      85           0 :           goto out;
      86             :         }
      87           0 :       decap_fib_index = p[0];
      88             :     }
      89             :   else
      90             :     {
      91           0 :       decap_fib_index = ntohl (mp->decap_vrf_id);
      92             :     }
      93             : 
      94             : 
      95           0 :   clib_memset (a, 0, sizeof (*a));
      96             : 
      97           0 :   a->is_add = mp->is_add;
      98           0 :   ip_address_decode (&mp->local, &a->local);
      99           0 :   ip_address_decode (&mp->remote, &a->remote);
     100             : 
     101             :   /* Check src & dst are different */
     102           0 :   if (ip46_address_is_equal (&a->local, &a->remote))
     103             :     {
     104           0 :       rv = VNET_API_ERROR_SAME_SRC_DST;
     105           0 :       goto out;
     106             :     }
     107             : 
     108           0 :   a->is_ip6 = !ip46_address_is_ip4 (&a->local);
     109           0 :   a->mcast_sw_if_index = ntohl (mp->mcast_sw_if_index);
     110           0 :   a->encap_fib_index = encap_fib_index;
     111           0 :   a->decap_fib_index = decap_fib_index;
     112           0 :   a->protocol = protocol;
     113           0 :   a->vni = ntohl (mp->vni);
     114           0 :   rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
     115             : 
     116           0 : out:
     117             :   /* *INDENT-OFF* */
     118           0 :   REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
     119             :   ({
     120             :     rmp->sw_if_index = ntohl (sw_if_index);
     121             :   }));
     122             :   /* *INDENT-ON* */
     123             : }
     124             : 
     125             : static void
     126           0 : vl_api_vxlan_gpe_add_del_tunnel_v2_t_handler (
     127             :   vl_api_vxlan_gpe_add_del_tunnel_v2_t *mp)
     128             : {
     129             :   vl_api_vxlan_gpe_add_del_tunnel_v2_reply_t *rmp;
     130           0 :   int rv = 0;
     131           0 :   vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
     132             :   u32 encap_fib_index, decap_fib_index;
     133             :   u8 protocol;
     134             :   uword *p;
     135           0 :   ip4_main_t *im = &ip4_main;
     136           0 :   u32 sw_if_index = ~0;
     137             : 
     138           0 :   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
     139           0 :   if (!p)
     140             :     {
     141           0 :       rv = VNET_API_ERROR_NO_SUCH_FIB;
     142           0 :       goto out;
     143             :     }
     144           0 :   encap_fib_index = p[0];
     145             : 
     146           0 :   protocol = mp->protocol;
     147             : 
     148             :   /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
     149           0 :   if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT)
     150             :     {
     151           0 :       p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id));
     152           0 :       if (!p)
     153             :         {
     154           0 :           rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
     155           0 :           goto out;
     156             :         }
     157           0 :       decap_fib_index = p[0];
     158             :     }
     159             :   else
     160             :     {
     161           0 :       decap_fib_index = ntohl (mp->decap_vrf_id);
     162             :     }
     163             : 
     164           0 :   clib_memset (a, 0, sizeof (*a));
     165             : 
     166           0 :   a->is_add = mp->is_add;
     167           0 :   ip_address_decode (&mp->local, &a->local);
     168           0 :   ip_address_decode (&mp->remote, &a->remote);
     169             : 
     170             :   /* Check src & dst are different */
     171           0 :   if (ip46_address_is_equal (&a->local, &a->remote))
     172             :     {
     173           0 :       rv = VNET_API_ERROR_SAME_SRC_DST;
     174           0 :       goto out;
     175             :     }
     176             : 
     177           0 :   a->local_port = ntohs (mp->local_port);
     178           0 :   a->remote_port = ntohs (mp->remote_port);
     179           0 :   a->is_ip6 = !ip46_address_is_ip4 (&a->local);
     180           0 :   a->mcast_sw_if_index = ntohl (mp->mcast_sw_if_index);
     181           0 :   a->encap_fib_index = encap_fib_index;
     182           0 :   a->decap_fib_index = decap_fib_index;
     183           0 :   a->protocol = protocol;
     184           0 :   a->vni = ntohl (mp->vni);
     185           0 :   rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
     186             : 
     187           0 : out:
     188           0 :   REPLY_MACRO2 (VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_V2_REPLY,
     189             :                 ({ rmp->sw_if_index = ntohl (sw_if_index); }));
     190             : }
     191             : 
     192           0 : static void send_vxlan_gpe_tunnel_details
     193             :   (vxlan_gpe_tunnel_t * t, vl_api_registration_t * reg, u32 context)
     194             : {
     195             :   vl_api_vxlan_gpe_tunnel_details_t *rmp;
     196           0 :   ip4_main_t *im4 = &ip4_main;
     197           0 :   ip6_main_t *im6 = &ip6_main;
     198           0 :   u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
     199             : 
     200           0 :   rmp = vl_msg_api_alloc (sizeof (*rmp));
     201           0 :   clib_memset (rmp, 0, sizeof (*rmp));
     202           0 :   rmp->_vl_msg_id =
     203           0 :     ntohs (REPLY_MSG_ID_BASE + VL_API_VXLAN_GPE_TUNNEL_DETAILS);
     204             : 
     205           0 :   ip_address_encode (&t->local, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
     206             :                      &rmp->local);
     207           0 :   ip_address_encode (&t->remote, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
     208             :                      &rmp->remote);
     209             : 
     210           0 :   if (ip46_address_is_ip4 (&t->local))
     211             :     {
     212           0 :       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
     213           0 :       rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id);
     214             :     }
     215             :   else
     216             :     {
     217           0 :       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
     218           0 :       rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id);
     219             :     }
     220           0 :   rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
     221           0 :   rmp->vni = htonl (t->vni);
     222           0 :   rmp->protocol = t->protocol;
     223           0 :   rmp->sw_if_index = htonl (t->sw_if_index);
     224           0 :   rmp->context = context;
     225             : 
     226           0 :   vl_api_send_msg (reg, (u8 *) rmp);
     227           0 : }
     228             : 
     229           0 : static void vl_api_vxlan_gpe_tunnel_dump_t_handler
     230             :   (vl_api_vxlan_gpe_tunnel_dump_t * mp)
     231             : {
     232             :   vl_api_registration_t *reg;
     233           0 :   vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
     234             :   vxlan_gpe_tunnel_t *t;
     235             :   u32 sw_if_index;
     236             : 
     237           0 :   reg = vl_api_client_index_to_registration (mp->client_index);
     238           0 :   if (!reg)
     239           0 :     return;
     240             : 
     241           0 :   sw_if_index = ntohl (mp->sw_if_index);
     242             : 
     243           0 :   if (~0 == sw_if_index)
     244             :     {
     245             :       /* *INDENT-OFF* */
     246           0 :       pool_foreach (t, vgm->tunnels)
     247             :         {
     248           0 :           send_vxlan_gpe_tunnel_details (t, reg, mp->context);
     249             :         }
     250             :       /* *INDENT-ON* */
     251             :     }
     252             :   else
     253             :     {
     254           0 :       if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
     255           0 :           (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
     256             :         {
     257           0 :           return;
     258             :         }
     259           0 :       t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
     260           0 :       send_vxlan_gpe_tunnel_details (t, reg, mp->context);
     261             :     }
     262             : }
     263             : 
     264             : static void
     265           0 : send_vxlan_gpe_tunnel_v2_details (vxlan_gpe_tunnel_t *t,
     266             :                                   vl_api_registration_t *reg, u32 context)
     267             : {
     268             :   vl_api_vxlan_gpe_tunnel_v2_details_t *rmp;
     269           0 :   ip4_main_t *im4 = &ip4_main;
     270           0 :   ip6_main_t *im6 = &ip6_main;
     271           0 :   u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
     272             : 
     273           0 :   rmp = vl_msg_api_alloc (sizeof (*rmp));
     274           0 :   clib_memset (rmp, 0, sizeof (*rmp));
     275           0 :   rmp->_vl_msg_id =
     276           0 :     ntohs (REPLY_MSG_ID_BASE + VL_API_VXLAN_GPE_TUNNEL_V2_DETAILS);
     277             : 
     278           0 :   ip_address_encode (&t->local, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
     279             :                      &rmp->local);
     280           0 :   ip_address_encode (&t->remote, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
     281             :                      &rmp->remote);
     282           0 :   rmp->local_port = htons (t->local_port);
     283           0 :   rmp->remote_port = htons (t->remote_port);
     284             : 
     285           0 :   if (ip46_address_is_ip4 (&t->local))
     286             :     {
     287           0 :       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
     288           0 :       rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id);
     289             :     }
     290             :   else
     291             :     {
     292           0 :       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
     293           0 :       rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id);
     294             :     }
     295           0 :   rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
     296           0 :   rmp->vni = htonl (t->vni);
     297           0 :   rmp->protocol = t->protocol;
     298           0 :   rmp->sw_if_index = htonl (t->sw_if_index);
     299           0 :   rmp->context = context;
     300             : 
     301           0 :   vl_api_send_msg (reg, (u8 *) rmp);
     302           0 : }
     303             : 
     304             : static void
     305           0 : vl_api_vxlan_gpe_tunnel_v2_dump_t_handler (
     306             :   vl_api_vxlan_gpe_tunnel_v2_dump_t *mp)
     307             : {
     308             :   vl_api_registration_t *reg;
     309           0 :   vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
     310             :   vxlan_gpe_tunnel_t *t;
     311             :   u32 sw_if_index;
     312             : 
     313           0 :   reg = vl_api_client_index_to_registration (mp->client_index);
     314           0 :   if (!reg)
     315           0 :     return;
     316             : 
     317           0 :   sw_if_index = ntohl (mp->sw_if_index);
     318             : 
     319           0 :   if (~0 == sw_if_index)
     320             :     {
     321           0 :       pool_foreach (t, vgm->tunnels)
     322             :         {
     323           0 :           send_vxlan_gpe_tunnel_v2_details (t, reg, mp->context);
     324             :         }
     325             :     }
     326             :   else
     327             :     {
     328           0 :       if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
     329           0 :           (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
     330             :         {
     331           0 :           return;
     332             :         }
     333           0 :       t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
     334           0 :       send_vxlan_gpe_tunnel_v2_details (t, reg, mp->context);
     335             :     }
     336             : }
     337             : 
     338             : #include <vxlan-gpe/vxlan_gpe.api.c>
     339             : 
     340             : static clib_error_t *
     341         575 : vxlan_gpe_api_hookup (vlib_main_t * vm)
     342             : {
     343         575 :   api_main_t *am = vlibapi_get_main ();
     344             : 
     345         575 :   vl_api_increase_msg_trace_size (am, VL_API_VXLAN_GPE_ADD_DEL_TUNNEL,
     346             :                                   17 * sizeof (u32));
     347             : 
     348             :   /*
     349             :    * Set up the (msg_name, crc, message-id) table
     350             :    */
     351         575 :   msg_id_base = setup_message_id_table ();
     352             : 
     353         575 :   return 0;
     354             : }
     355             : 
     356       10943 : VLIB_API_INIT_FUNCTION (vxlan_gpe_api_hookup);
     357             : 
     358             : /*
     359             :  * fd.io coding-style-patch-verification: ON
     360             :  *
     361             :  * Local Variables:
     362             :  * eval: (c-set-style "gnu")
     363             :  * End:
     364             :  */

Generated by: LCOV version 1.14