LCOV - code coverage report
Current view: top level - plugins/svs - svs_api.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 47 63 74.6 %
Date: 2023-07-05 22:20:52 Functions: 8 9 88.9 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2018 Cisco 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 <stddef.h>
      17             : 
      18             : #include <vnet/vnet.h>
      19             : #include <vnet/plugin/plugin.h>
      20             : #include <svs/svs.h>
      21             : #include <vnet/fib/fib_api.h>
      22             : #include <vnet/ip/ip_types_api.h>
      23             : 
      24             : #include <vpp/app/version.h>
      25             : 
      26             : #include <vlibapi/api.h>
      27             : #include <vlibmemory/api.h>
      28             : 
      29             : /* define message IDs */
      30             : #include <vnet/format_fns.h>
      31             : #include <svs/svs.api_enum.h>
      32             : #include <svs/svs.api_types.h>
      33             : 
      34             : /**
      35             :  * Base message ID fot the plugin
      36             :  */
      37             : static u32 svs_base_msg_id;
      38             : #define REPLY_MSG_ID_BASE (svs_base_msg_id)
      39             : #include <vlibapi/api_helper_macros.h>
      40             : 
      41             : static void
      42           0 : vl_api_svs_plugin_get_version_t_handler (vl_api_svs_plugin_get_version_t * mp)
      43             : {
      44             :   vl_api_svs_plugin_get_version_reply_t *rmp;
      45           0 :   int msg_size = sizeof (*rmp);
      46             :   vl_api_registration_t *rp;
      47             : 
      48           0 :   rp = vl_api_client_index_to_registration (mp->client_index);
      49           0 :   if (rp == 0)
      50           0 :     return;
      51             : 
      52           0 :   rmp = vl_msg_api_alloc (msg_size);
      53           0 :   clib_memset (rmp, 0, msg_size);
      54           0 :   rmp->_vl_msg_id =
      55           0 :     ntohs (VL_API_SVS_PLUGIN_GET_VERSION_REPLY + svs_base_msg_id);
      56           0 :   rmp->context = mp->context;
      57           0 :   rmp->major = htonl (SVS_PLUGIN_VERSION_MAJOR);
      58           0 :   rmp->minor = htonl (SVS_PLUGIN_VERSION_MINOR);
      59             : 
      60           0 :   vl_api_send_msg (rp, (u8 *) rmp);
      61             : }
      62             : 
      63             : static void
      64           8 : vl_api_svs_table_add_del_t_handler (vl_api_svs_table_add_del_t * mp)
      65             : {
      66             :   vl_api_svs_table_add_del_reply_t *rmp;
      67             :   fib_protocol_t fproto;
      68           8 :   int rv = 0;
      69             : 
      70           8 :   rv = fib_proto_from_api_address_family (mp->af, &fproto);
      71           8 :   if (rv < 0)
      72           0 :     goto error;
      73             : 
      74           8 :   if (mp->is_add)
      75             :     {
      76           4 :       rv = svs_table_add (fproto, ntohl (mp->table_id));
      77             :     }
      78             :   else
      79             :     {
      80           4 :       rv = svs_table_delete (fproto, ntohl (mp->table_id));
      81             :     }
      82             : 
      83           8 : error:
      84           8 :   REPLY_MACRO (VL_API_SVS_TABLE_ADD_DEL_REPLY);
      85             : }
      86             : 
      87             : static void
      88          24 : vl_api_svs_route_add_del_t_handler (vl_api_svs_route_add_del_t * mp)
      89             : {
      90             :   vl_api_svs_route_add_del_reply_t *rmp;
      91             :   fib_prefix_t pfx;
      92          24 :   int rv = 0;
      93             : 
      94          24 :   ip_prefix_decode (&mp->prefix, &pfx);
      95             : 
      96          24 :   if (mp->is_add)
      97             :     {
      98          12 :       rv = svs_route_add (ntohl (mp->table_id), &pfx,
      99             :                           ntohl (mp->source_table_id));
     100             :     }
     101             :   else
     102             :     {
     103          12 :       rv = svs_route_delete (ntohl (mp->table_id), &pfx);
     104             :     }
     105             : 
     106          24 :   REPLY_MACRO (VL_API_SVS_ROUTE_ADD_DEL_REPLY);
     107             : }
     108             : 
     109             : static void
     110           8 : vl_api_svs_enable_disable_t_handler (vl_api_svs_enable_disable_t * mp)
     111             : {
     112             :   vl_api_svs_enable_disable_reply_t *rmp;
     113             :   fib_protocol_t fproto;
     114           8 :   int rv = 0;
     115             : 
     116           8 :   VALIDATE_SW_IF_INDEX (mp);
     117             : 
     118           8 :   rv = fib_proto_from_api_address_family (mp->af, &fproto);
     119           8 :   if (rv < 0)
     120           0 :     goto error;
     121             : 
     122           8 :   if (mp->is_enable)
     123             :     {
     124           4 :       rv = svs_enable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
     125             :     }
     126             :   else
     127             :     {
     128             :       rv =
     129           4 :         svs_disable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
     130             :     }
     131             : 
     132           8 :   BAD_SW_IF_INDEX_LABEL;
     133           8 : error:
     134           8 :   REPLY_MACRO (VL_API_SVS_ENABLE_DISABLE_REPLY);
     135             : }
     136             : 
     137             : typedef struct svs_dump_walk_ctx_t_
     138             : {
     139             :   vl_api_registration_t *rp;
     140             :   u32 context;
     141             : } svs_dump_walk_ctx_t;
     142             : 
     143             : 
     144             : static walk_rc_t
     145           4 : svs_send_details (fib_protocol_t fproto,
     146             :                   u32 table_id, u32 sw_if_index, void *args)
     147             : {
     148             :   vl_api_svs_details_t *mp;
     149             :   svs_dump_walk_ctx_t *ctx;
     150             : 
     151           4 :   ctx = args;
     152             : 
     153           4 :   mp = vl_msg_api_alloc (sizeof (*mp));
     154           4 :   mp->_vl_msg_id = ntohs (VL_API_SVS_DETAILS + svs_base_msg_id);
     155             : 
     156           4 :   mp->context = ctx->context;
     157           4 :   mp->sw_if_index = htonl (sw_if_index);
     158           4 :   mp->table_id = htonl (table_id);
     159           4 :   mp->af = fib_proto_to_api_address_family (fproto);
     160             : 
     161           4 :   vl_api_send_msg (ctx->rp, (u8 *) mp);
     162             : 
     163           4 :   return (WALK_CONTINUE);
     164             : }
     165             : 
     166             : static void
     167           2 : vl_api_svs_dump_t_handler (vl_api_svs_dump_t * mp)
     168             : {
     169             :   vl_api_registration_t *rp;
     170             : 
     171           2 :   rp = vl_api_client_index_to_registration (mp->client_index);
     172           2 :   if (rp == 0)
     173           0 :     return;
     174             : 
     175           2 :   svs_dump_walk_ctx_t ctx = {
     176             :     .rp = rp,
     177           2 :     .context = mp->context,
     178             :   };
     179             : 
     180           2 :   svs_walk (svs_send_details, &ctx);
     181             : }
     182             : 
     183             : #include <svs/svs.api.c>
     184             : static clib_error_t *
     185         559 : svs_api_init (vlib_main_t * vm)
     186             : {
     187             :   /* Ask for a correctly-sized block of API message decode slots */
     188         559 :   svs_base_msg_id = setup_message_id_table ();
     189             : 
     190         559 :   return 0;
     191             : }
     192             : 
     193        1119 : VLIB_INIT_FUNCTION (svs_api_init);
     194             : 
     195             : VLIB_PLUGIN_REGISTER () = {
     196             :   .version = VPP_BUILD_VER,
     197             :   .description = "Source Virtual Routing and Forwarding (VRF) Select",
     198             : };
     199             : 
     200             : /*
     201             :  * fd.io coding-style-patch-verification: ON
     202             :  *
     203             :  * Local Variables:
     204             :  * eval: (c-set-style "gnu")
     205             :  * End:
     206             :  */

Generated by: LCOV version 1.14