LCOV - code coverage report
Current view: top level - plugins/npt66 - npt66_cli.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 2 50 4.0 %
Date: 2023-10-26 01:39:38 Functions: 4 7 57.1 %

          Line data    Source code
       1             : // SPDX-License-Identifier: Apache-2.0
       2             : // Copyright(c) 2023 Cisco Systems, Inc.
       3             : 
       4             : #include <stdbool.h>
       5             : #include <vlib/vlib.h>
       6             : #include <vnet/feature/feature.h>
       7             : #include <vnet/ip/ip.h>
       8             : #include <vppinfra/clib_error.h>
       9             : #include "npt66.h"
      10             : 
      11             : static clib_error_t *
      12           0 : set_npt66_binding_command_fn (vlib_main_t *vm, unformat_input_t *input,
      13             :                               vlib_cli_command_t *cmd)
      14             : {
      15           0 :   unformat_input_t _line_input, *line_input = &_line_input;
      16           0 :   clib_error_t *error = 0;
      17           0 :   bool internal_set = false, external_set = false;
      18           0 :   bool add = true;
      19           0 :   u32 sw_if_index = ~0;
      20             :   ip6_address_t internal, external;
      21           0 :   int internal_plen = 0, external_plen = 0;
      22             : 
      23             :   /* Get a line of input. */
      24           0 :   if (!unformat_user (input, unformat_line_input, line_input))
      25           0 :     return 0;
      26             : 
      27           0 :   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
      28             :     {
      29           0 :       if (unformat (line_input, "internal %U/%d", unformat_ip6_address,
      30             :                     &internal, &internal_plen))
      31           0 :         internal_set = true;
      32           0 :       else if (unformat (line_input, "external %U/%d", unformat_ip6_address,
      33             :                          &external, &external_plen))
      34           0 :         external_set = true;
      35           0 :       else if (unformat (line_input, "interface %U",
      36             :                          unformat_vnet_sw_interface, vnet_get_main (),
      37             :                          &sw_if_index))
      38             :         ;
      39           0 :       else if (unformat (line_input, "del"))
      40             :         {
      41           0 :           add = false;
      42             :         }
      43             :       else
      44             :         {
      45           0 :           error = clib_error_return (0, "unknown input `%U'",
      46             :                                      format_unformat_error, line_input);
      47           0 :           goto done;
      48             :         }
      49             :     }
      50           0 :   if (sw_if_index == ~0)
      51             :     {
      52           0 :       error = clib_error_return (0, "interface is required `%U'",
      53             :                                  format_unformat_error, line_input);
      54           0 :       goto done;
      55             :     }
      56           0 :   if (!internal_set)
      57             :     {
      58           0 :       error = clib_error_return (0, "missing parameter: internal `%U'",
      59             :                                  format_unformat_error, line_input);
      60           0 :       goto done;
      61             :     }
      62           0 :   if (!external_set)
      63             :     {
      64           0 :       error = clib_error_return (0, "missing parameter: external `%U'",
      65             :                                  format_unformat_error, line_input);
      66           0 :       goto done;
      67             :     }
      68             : 
      69           0 :   int rv = npt66_binding_add_del (sw_if_index, &internal, internal_plen,
      70             :                                   &external, external_plen, add);
      71           0 :   if (rv)
      72             :     {
      73           0 :       error = clib_error_return (0, "Adding binding failed %d", rv);
      74           0 :       goto done;
      75             :     }
      76             : 
      77           0 : done:
      78           0 :   unformat_free (line_input);
      79             : 
      80           0 :   return error;
      81             : }
      82             : 
      83         109 : VLIB_CLI_COMMAND (set_npt66_binding_command, static) = {
      84             :   .path = "set npt66 binding",
      85             :   .short_help = "set npt66 binding interface <name> internal <pfx> "
      86             :                 "external <pfx> [del]",
      87             :   .function = set_npt66_binding_command_fn,
      88             : };
      89             : 
      90             : static u8 *
      91           0 : format_npt66_binding (u8 *s, va_list *args)
      92             : {
      93           0 :   u32 index = va_arg (*args, u32);
      94           0 :   npt66_binding_t *b = va_arg (*args, npt66_binding_t *);
      95           0 :   s = format (s, "[%d] internal: %U/%d external: %U/%d", index,
      96           0 :               format_ip6_address, &b->internal, b->internal_plen,
      97           0 :               format_ip6_address, &b->external, b->external_plen);
      98           0 :   return s;
      99             : }
     100             : 
     101             : static clib_error_t *
     102           0 : show_npt66_bindings_command_fn (vlib_main_t *vm, unformat_input_t *input,
     103             :                                 vlib_cli_command_t *cmd)
     104             : {
     105           0 :   npt66_main_t *nm = &npt66_main;
     106             :   npt66_binding_t *b;
     107           0 :   clib_error_t *error = 0;
     108             : 
     109             :   /* Get a line of input. */
     110           0 :   pool_foreach (b, nm->bindings)
     111             :     {
     112           0 :       vlib_cli_output (vm, "%U", format_npt66_binding, b - nm->bindings, b);
     113             :     }
     114           0 :   return error;
     115             : }
     116             : 
     117         109 : VLIB_CLI_COMMAND (show_npt66_bindings_command, static) = {
     118             :   .path = "show npt66 bindings",
     119             :   .short_help = "show npt66 bindings",
     120             :   .function = show_npt66_bindings_command_fn,
     121             : };

Generated by: LCOV version 1.14