LCOV - code coverage report
Current view: top level - vnet/ipip - ipip_api.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 59 67 88.1 %
Date: 2023-07-05 22:20:52 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /*
       2             :  * ipip_api.c - ipip api
       3             :  *
       4             :  * Copyright (c) 2018 Cisco and/or its affiliates.
       5             :  * Licensed under the Apache License, Version 2.0 (the "License");
       6             :  * you may not use this file except in compliance with the License.
       7             :  * You may obtain a copy of the License at:
       8             :  *
       9             :  *     http://www.apache.org/licenses/LICENSE-2.0
      10             :  *
      11             :  * Unless required by applicable law or agreed to in writing, software
      12             :  * distributed under the License is distributed on an "AS IS" BASIS,
      13             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14             :  * See the License for the specific language governing permissions and
      15             :  * limitations under the License.
      16             :  */
      17             : 
      18             : #include <vlibmemory/api.h>
      19             : #include <vnet/api_errno.h>
      20             : #include <vnet/fib/fib_table.h>
      21             : #include <vnet/interface.h>
      22             : #include <vnet/ipip/ipip.h>
      23             : #include <vnet/vnet.h>
      24             : #include <vnet/ip/ip_types_api.h>
      25             : #include <vnet/tunnel/tunnel_types_api.h>
      26             : 
      27             : #include <vnet/ipip/ipip.api_enum.h>
      28             : #include <vnet/ipip/ipip.api_types.h>
      29             : 
      30             : #define REPLY_MSG_ID_BASE im->msg_id_base
      31             : #include <vlibapi/api_helper_macros.h>
      32             : 
      33             : static void
      34         106 : vl_api_ipip_add_tunnel_t_handler (vl_api_ipip_add_tunnel_t * mp)
      35             : {
      36         106 :   ipip_main_t *im = &ipip_main;
      37             :   vl_api_ipip_add_tunnel_reply_t *rmp;
      38         106 :   int rv = 0;
      39         106 :   u32 fib_index, sw_if_index = ~0;
      40             :   tunnel_encap_decap_flags_t flags;
      41             :   ip46_address_t src, dst;
      42             :   ip46_type_t itype[2];
      43             :   tunnel_mode_t mode;
      44             : 
      45         106 :   itype[0] = ip_address_decode (&mp->tunnel.src, &src);
      46         106 :   itype[1] = ip_address_decode (&mp->tunnel.dst, &dst);
      47             : 
      48         106 :   if (itype[0] != itype[1])
      49             :     {
      50           0 :       rv = VNET_API_ERROR_INVALID_PROTOCOL;
      51           0 :       goto out;
      52             :     }
      53             : 
      54         106 :   if (ip46_address_is_equal (&src, &dst))
      55             :     {
      56           0 :       rv = VNET_API_ERROR_SAME_SRC_DST;
      57           0 :       goto out;
      58             :     }
      59             : 
      60         106 :   rv = tunnel_encap_decap_flags_decode (mp->tunnel.flags, &flags);
      61             : 
      62         106 :   if (rv)
      63           0 :     goto out;
      64             : 
      65         106 :   rv = tunnel_mode_decode (mp->tunnel.mode, &mode);
      66             : 
      67         106 :   if (rv)
      68           0 :     goto out;
      69             : 
      70         106 :   fib_index = fib_table_find (fib_proto_from_ip46 (itype[0]),
      71             :                               ntohl (mp->tunnel.table_id));
      72             : 
      73         106 :   if (~0 == fib_index)
      74             :     {
      75           0 :       rv = VNET_API_ERROR_NO_SUCH_FIB;
      76             :     }
      77             :   else
      78             :     {
      79         106 :       rv = ipip_add_tunnel ((itype[0] == IP46_TYPE_IP6 ?
      80             :                              IPIP_TRANSPORT_IP6 :
      81             :                              IPIP_TRANSPORT_IP4),
      82             :                             ntohl (mp->tunnel.instance), &src, &dst,
      83             :                             fib_index, flags,
      84         106 :                             ip_dscp_decode (mp->tunnel.dscp), mode,
      85             :                             &sw_if_index);
      86             :     }
      87             : 
      88         106 : out:
      89             :   /* *INDENT-OFF* */
      90         106 :   REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY,
      91             :   ({
      92             :     rmp->sw_if_index = ntohl(sw_if_index);
      93             :   }));
      94             :   /* *INDENT-ON* */
      95             : }
      96             : 
      97             : static void
      98         106 : vl_api_ipip_del_tunnel_t_handler (vl_api_ipip_del_tunnel_t * mp)
      99             : {
     100         106 :   ipip_main_t *im = &ipip_main;
     101             :   vl_api_ipip_del_tunnel_reply_t *rmp;
     102             : 
     103         106 :   int rv = ipip_del_tunnel (ntohl (mp->sw_if_index));
     104             : 
     105         106 :   REPLY_MACRO (VL_API_IPIP_DEL_TUNNEL_REPLY);
     106             : }
     107             : 
     108             : static void
     109         468 : send_ipip_tunnel_details (ipip_tunnel_t * t, vl_api_ipip_tunnel_dump_t * mp)
     110             : {
     111         468 :   ipip_main_t *im = &ipip_main;
     112             :   vl_api_ipip_tunnel_details_t *rmp;
     113         468 :   bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false;
     114             :   fib_table_t *ft;
     115             : 
     116         468 :   ft = fib_table_get (t->fib_index, (is_ipv6 ? FIB_PROTOCOL_IP6 :
     117             :                                      FIB_PROTOCOL_IP4));
     118             : 
     119             :   /* *INDENT-OFF* */
     120         468 :   REPLY_MACRO_DETAILS2(VL_API_IPIP_TUNNEL_DETAILS,
     121             :   ({
     122             :     ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src);
     123             :     ip_address_encode (&t->tunnel_dst, IP46_TYPE_ANY, &rmp->tunnel.dst);
     124             :     rmp->tunnel.table_id = htonl (ft->ft_table_id);
     125             :     rmp->tunnel.instance = htonl (t->user_instance);
     126             :     rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
     127             :     rmp->tunnel.dscp = ip_dscp_encode(t->dscp);
     128             :     rmp->tunnel.flags = tunnel_encap_decap_flags_encode(t->flags);
     129             :   }));
     130             :     /* *INDENT-ON* */
     131             : }
     132             : 
     133             : static void
     134         267 : vl_api_ipip_tunnel_dump_t_handler (vl_api_ipip_tunnel_dump_t * mp)
     135             : {
     136         267 :   ipip_main_t *im = &ipip_main;
     137             :   ipip_tunnel_t *t;
     138             :   u32 sw_if_index;
     139             : 
     140         267 :   sw_if_index = ntohl (mp->sw_if_index);
     141             : 
     142         267 :   if (sw_if_index == ~0)
     143             :     {
     144             :     /* *INDENT-OFF* */
     145         545 :     pool_foreach (t, im->tunnels)
     146             :      {
     147         373 :       send_ipip_tunnel_details(t, mp);
     148             :     }
     149             :     /* *INDENT-ON* */
     150             :     }
     151             :   else
     152             :     {
     153          95 :       t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
     154          95 :       if (t)
     155          95 :         send_ipip_tunnel_details (t, mp);
     156             :     }
     157         267 : }
     158             : 
     159             : static void
     160          11 : vl_api_ipip_6rd_add_tunnel_t_handler (vl_api_ipip_6rd_add_tunnel_t * mp)
     161             : {
     162          11 :   ipip_main_t *im = &ipip_main;
     163             :   vl_api_ipip_6rd_add_tunnel_reply_t *rmp;
     164             :   u32 sixrd_tunnel_index, ip4_fib_index, ip6_fib_index;
     165             :   int rv;
     166             : 
     167          11 :   sixrd_tunnel_index = ~0;
     168          11 :   ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->ip4_table_id));
     169          11 :   ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, ntohl (mp->ip6_table_id));
     170             : 
     171          11 :   if (~0 == ip4_fib_index || ~0 == ip6_fib_index)
     172             : 
     173             :     {
     174           0 :       rv = VNET_API_ERROR_NO_SUCH_FIB;
     175             :     }
     176             :   else
     177             :     {
     178          11 :       rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix.address,
     179          11 :                              mp->ip6_prefix.len,
     180          11 :                              (ip4_address_t *) & mp->ip4_prefix.address,
     181          11 :                              mp->ip4_prefix.len,
     182          11 :                              (ip4_address_t *) & mp->ip4_src,
     183          11 :                              mp->security_check,
     184             :                              ip4_fib_index, ip6_fib_index,
     185             :                              &sixrd_tunnel_index);
     186             :     }
     187             : 
     188             :   /* *INDENT-OFF* */
     189          11 :   REPLY_MACRO2 (VL_API_IPIP_6RD_ADD_TUNNEL_REPLY,
     190             :   ({
     191             :     rmp->sw_if_index = htonl (sixrd_tunnel_index);
     192             :   }));
     193             :   /* *INDENT-ON* */
     194             : }
     195             : 
     196             : static void
     197          11 : vl_api_ipip_6rd_del_tunnel_t_handler (vl_api_ipip_6rd_del_tunnel_t * mp)
     198             : {
     199          11 :   ipip_main_t *im = &ipip_main;
     200             :   vl_api_ipip_6rd_del_tunnel_reply_t *rmp;
     201             : 
     202          11 :   int rv = sixrd_del_tunnel (ntohl (mp->sw_if_index));
     203             : 
     204          11 :   REPLY_MACRO (VL_API_IPIP_6RD_DEL_TUNNEL_REPLY);
     205             : }
     206             : 
     207             : /*
     208             :  * ipip_api_hookup
     209             :  * Add vpe's API message handlers to the table.
     210             :  * vlib has already mapped shared memory and
     211             :  * added the client registration handlers.
     212             :  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
     213             :  */
     214             : /* API definitions */
     215             : #include <vnet/format_fns.h>
     216             : #include <vnet/ipip/ipip.api.c>
     217             : 
     218             : static clib_error_t *
     219         559 : ipip_api_hookup (vlib_main_t * vm)
     220             : {
     221         559 :   ipip_main_t *im = &ipip_main;
     222             : 
     223             :   /*
     224             :    * Set up the (msg_name, crc, message-id) table
     225             :    */
     226         559 :   im->msg_id_base = setup_message_id_table ();
     227             : 
     228         559 :   return 0;
     229             : }
     230             : 
     231        9519 : VLIB_API_INIT_FUNCTION (ipip_api_hookup);
     232             : 
     233             : /*
     234             :  * fd.io coding-style-patch-verification: ON
     235             :  *
     236             :  * Local Variables:
     237             :  * eval: (c-set-style "gnu")
     238             :  * End:
     239             :  */

Generated by: LCOV version 1.14