LCOV - code coverage report
Current view: top level - plugins/nat/dslite - dslite_api.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 33 57 57.9 %
Date: 2023-10-26 01:39:38 Functions: 4 8 50.0 %

          Line data    Source code
       1             : /*
       2             :  *------------------------------------------------------------------
       3             :  * dslite_api.c - DS-Lite API
       4             :  *
       5             :  * Copyright (c) 2019 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             : #include <vnet/ip/ip_types_api.h>
      20             : #include <nat/dslite/dslite.h>
      21             : #include <nat/dslite/dslite.api_enum.h>
      22             : #include <nat/dslite/dslite.api_types.h>
      23             : #include <vnet/ip/ip.h>
      24             : #include <vnet/fib/fib_table.h>
      25             : #include <vlibmemory/api.h>
      26             : 
      27             : #define REPLY_MSG_ID_BASE dm->msg_id_base
      28             : #include <vlibapi/api_helper_macros.h>
      29             : 
      30             : static void
      31           2 : vl_api_dslite_set_aftr_addr_t_handler (vl_api_dslite_set_aftr_addr_t * mp)
      32             : {
      33             :   vl_api_dslite_set_aftr_addr_reply_t *rmp;
      34           2 :   dslite_main_t *dm = &dslite_main;
      35           2 :   int rv = 0;
      36             :   ip6_address_t ip6_addr;
      37             :   ip4_address_t ip4_addr;
      38             : 
      39           2 :   memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
      40           2 :   memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
      41             : 
      42           2 :   rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
      43           2 :   if (rv == 0)
      44           2 :     rv = dslite_set_aftr_ip4_addr (dm, &ip4_addr);
      45             : 
      46           2 :   REPLY_MACRO (VL_API_DSLITE_SET_AFTR_ADDR_REPLY);
      47             : }
      48             : 
      49             : static void
      50           0 : vl_api_dslite_get_aftr_addr_t_handler (vl_api_dslite_get_aftr_addr_t * mp)
      51             : {
      52             :   vl_api_dslite_get_aftr_addr_reply_t *rmp;
      53           0 :   dslite_main_t *dm = &dslite_main;
      54           0 :   int rv = 0;
      55             : 
      56             :   /* *INDENT-OFF* */
      57           0 :   REPLY_MACRO2 (VL_API_DSLITE_GET_AFTR_ADDR_REPLY,
      58             :   ({
      59             :     memcpy (rmp->ip4_addr, &dm->aftr_ip4_addr.as_u8, 4);
      60             :     memcpy (rmp->ip6_addr, &dm->aftr_ip6_addr.as_u8, 16);
      61             :   }))
      62             :   /* *INDENT-ON* */
      63             : }
      64             : 
      65             : static void
      66           1 : vl_api_dslite_set_b4_addr_t_handler (vl_api_dslite_set_b4_addr_t * mp)
      67             : {
      68             :   vl_api_dslite_set_b4_addr_reply_t *rmp;
      69           1 :   dslite_main_t *dm = &dslite_main;
      70           1 :   int rv = 0;
      71             :   ip6_address_t ip6_addr;
      72             :   ip4_address_t ip4_addr;
      73             : 
      74           1 :   memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
      75           1 :   memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
      76             : 
      77           1 :   rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
      78           1 :   if (rv == 0)
      79           1 :     rv = dslite_set_b4_ip4_addr (dm, &ip4_addr);
      80             : 
      81           1 :   REPLY_MACRO (VL_API_DSLITE_SET_B4_ADDR_REPLY);
      82             : }
      83             : 
      84             : static void
      85           0 : vl_api_dslite_get_b4_addr_t_handler (vl_api_dslite_get_b4_addr_t * mp)
      86             : {
      87             :   vl_api_dslite_get_b4_addr_reply_t *rmp;
      88           0 :   dslite_main_t *dm = &dslite_main;
      89           0 :   int rv = 0;
      90             : 
      91             :   /* *INDENT-OFF* */
      92           0 :   REPLY_MACRO2 (VL_API_DSLITE_GET_B4_ADDR_REPLY,
      93             :   ({
      94             :     memcpy (rmp->ip4_addr, &dm->b4_ip4_addr.as_u8, 4);
      95             :     memcpy (rmp->ip6_addr, &dm->b4_ip6_addr.as_u8, 16);
      96             :   }))
      97             :   /* *INDENT-ON* */
      98             : }
      99             : 
     100             : static void
     101           1 :   vl_api_dslite_add_del_pool_addr_range_t_handler
     102             :   (vl_api_dslite_add_del_pool_addr_range_t * mp)
     103             : {
     104             :   vl_api_dslite_add_del_pool_addr_range_reply_t *rmp;
     105           1 :   dslite_main_t *dm = &dslite_main;
     106           1 :   int rv = 0;
     107             :   ip4_address_t this_addr;
     108             :   u32 start_host_order, end_host_order;
     109             :   int count;
     110             :   u32 *tmp;
     111             : 
     112           1 :   tmp = (u32 *) mp->start_addr;
     113           1 :   start_host_order = clib_host_to_net_u32 (tmp[0]);
     114           1 :   tmp = (u32 *) mp->end_addr;
     115           1 :   end_host_order = clib_host_to_net_u32 (tmp[0]);
     116             : 
     117             :   // TODO:
     118             :   // end_host_order < start_host_order
     119             : 
     120           1 :   count = (end_host_order - start_host_order) + 1;
     121           1 :   memcpy (&this_addr.as_u8, mp->start_addr, 4);
     122             : 
     123           1 :   rv = nat_add_del_ip4_pool_addrs (&dm->pool, this_addr, count, mp->is_add, 0);
     124             : 
     125           1 :   REPLY_MACRO (VL_API_DSLITE_ADD_DEL_POOL_ADDR_RANGE_REPLY);
     126             : }
     127             : 
     128             : static void
     129           0 : send_dslite_address_details (nat_ip4_pool_addr_t * a,
     130             :                              vl_api_registration_t * reg, u32 context)
     131             : {
     132           0 :   dslite_main_t *dm = &dslite_main;
     133             :   vl_api_dslite_address_details_t *rmp;
     134             : 
     135           0 :   rmp = vl_msg_api_alloc (sizeof (*rmp));
     136             : 
     137           0 :   clib_memset (rmp, 0, sizeof (*rmp));
     138             : 
     139           0 :   rmp->_vl_msg_id = ntohs (VL_API_DSLITE_ADDRESS_DETAILS + dm->msg_id_base);
     140           0 :   clib_memcpy (rmp->ip_address, &(a->addr), 4);
     141           0 :   rmp->context = context;
     142             : 
     143           0 :   vl_api_send_msg (reg, (u8 *) rmp);
     144           0 : }
     145             : 
     146             : static void
     147           0 : vl_api_dslite_address_dump_t_handler (vl_api_dslite_address_dump_t * mp)
     148             : {
     149             :   vl_api_registration_t *reg;
     150           0 :   dslite_main_t *dm = &dslite_main;
     151             :   nat_ip4_pool_addr_t *a;
     152             : 
     153           0 :   reg = vl_api_client_index_to_registration (mp->client_index);
     154           0 :   if (!reg)
     155           0 :     return;
     156             : 
     157             :   /* *INDENT-OFF* */
     158           0 :   vec_foreach (a, dm->pool.pool_addr)
     159             :     {
     160           0 :       send_dslite_address_details (a, reg, mp->context);
     161             :     }
     162             :   /* *INDENT-ON* */
     163             : }
     164             : 
     165             : /* API definitions */
     166             : #include <vnet/format_fns.h>
     167             : #include <nat/dslite/dslite.api.c>
     168             : 
     169             : /* Set up the API message handling tables */
     170             : clib_error_t *
     171         575 : dslite_api_hookup (vlib_main_t * vm)
     172             : {
     173         575 :   dslite_main_t *dm = &dslite_main;
     174             : 
     175         575 :   dm->msg_id_base = setup_message_id_table ();
     176         575 :   return 0;
     177             : }

Generated by: LCOV version 1.14