LCOV - code coverage report
Current view: top level - vnet/ip - ip_container_proxy.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 2 101 2.0 %
Date: 2023-10-26 01:39:38 Functions: 4 10 40.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015 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             :  * ip/ip_lookup.c: ip4/6 adjacency and lookup table management
      17             :  *
      18             :  * Copyright (c) 2008 Eliot Dresselhaus
      19             :  *
      20             :  * Permission is hereby granted, free of charge, to any person obtaining
      21             :  * a copy of this software and associated documentation files (the
      22             :  * "Software"), to deal in the Software without restriction, including
      23             :  * without limitation the rights to use, copy, modify, merge, publish,
      24             :  * distribute, sublicense, and/or sell copies of the Software, and to
      25             :  * permit persons to whom the Software is furnished to do so, subject to
      26             :  * the following conditions:
      27             :  *
      28             :  * The above copyright notice and this permission notice shall be
      29             :  * included in all copies or substantial portions of the Software.
      30             :  *
      31             :  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      32             :  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      33             :  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
      34             :  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      35             :  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
      36             :  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
      37             :  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      38             :  */
      39             : 
      40             : #include <vnet/ip/ip_container_proxy.h>
      41             : #include <vnet/ip/format.h>
      42             : #include <vnet/fib/fib_table.h>
      43             : #include <vnet/dpo/l3_proxy_dpo.h>
      44             : #include <vnet/dpo/load_balance.h>
      45             : 
      46             : clib_error_t *
      47           0 : vnet_ip_container_proxy_add_del (vnet_ip_container_proxy_args_t * args)
      48             : {
      49             :   u32 fib_index;
      50             : 
      51           0 :   if (!vnet_sw_interface_is_api_valid (vnet_get_main (), args->sw_if_index))
      52           0 :     return clib_error_return_code (0, VNET_API_ERROR_INVALID_INTERFACE, 0,
      53             :                                    "invalid sw_if_index");
      54             : 
      55           0 :   fib_index = fib_table_get_table_id_for_sw_if_index (args->prefix.fp_proto,
      56             :                                                       args->sw_if_index);
      57           0 :   if (args->is_add)
      58             :     {
      59           0 :       dpo_id_t proxy_dpo = DPO_INVALID;
      60           0 :       l3_proxy_dpo_add_or_lock (fib_proto_to_dpo (args->prefix.fp_proto),
      61             :                                 args->sw_if_index, &proxy_dpo);
      62           0 :       fib_table_entry_special_dpo_add (fib_index,
      63           0 :                                        &args->prefix,
      64             :                                        FIB_SOURCE_PROXY,
      65             :                                        FIB_ENTRY_FLAG_EXCLUSIVE, &proxy_dpo);
      66           0 :       dpo_reset (&proxy_dpo);
      67             :     }
      68             :   else
      69             :     {
      70           0 :       fib_table_entry_special_remove (fib_index, &args->prefix,
      71             :                                       FIB_SOURCE_PROXY);
      72             :     }
      73           0 :   return 0;
      74             : }
      75             : 
      76             : u8
      77           0 : ip_container_proxy_is_set (fib_prefix_t * pfx, u32 sw_if_index)
      78             : {
      79             :   u32 fib_index;
      80             :   fib_node_index_t fei;
      81             :   const dpo_id_t *dpo;
      82             :   l3_proxy_dpo_t *l3p;
      83             :   load_balance_t *lb0;
      84             : 
      85           0 :   fib_index = fib_table_get_table_id_for_sw_if_index (pfx->fp_proto,
      86             :                                                       sw_if_index);
      87           0 :   if (fib_index == ~0)
      88           0 :     return 0;
      89             : 
      90           0 :   fei = fib_table_lookup_exact_match (fib_index, pfx);
      91           0 :   if (fei == FIB_NODE_INDEX_INVALID)
      92           0 :     return 0;
      93             : 
      94           0 :   dpo = fib_entry_contribute_ip_forwarding (fei);
      95           0 :   lb0 = load_balance_get (dpo->dpoi_index);
      96           0 :   dpo = load_balance_get_bucket_i (lb0, 0);
      97           0 :   if (dpo->dpoi_type != DPO_L3_PROXY)
      98           0 :     return 0;
      99             : 
     100           0 :   l3p = l3_proxy_dpo_get (dpo->dpoi_index);
     101           0 :   return (l3p->l3p_sw_if_index == sw_if_index);
     102             : }
     103             : 
     104             : typedef struct ip_container_proxy_walk_ctx_t_
     105             : {
     106             :   ip_container_proxy_cb_t cb;
     107             :   void *ctx;
     108             : } ip_container_proxy_walk_ctx_t;
     109             : 
     110             : static fib_table_walk_rc_t
     111           0 : ip_container_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
     112             : {
     113           0 :   ip_container_proxy_walk_ctx_t *ctx = arg;
     114             :   const fib_prefix_t *pfx;
     115             :   const dpo_id_t *dpo;
     116             :   load_balance_t *lb;
     117             :   l3_proxy_dpo_t *l3p;
     118             : 
     119           0 :   pfx = fib_entry_get_prefix (fei);
     120           0 :   if (fib_entry_is_sourced (fei, FIB_SOURCE_PROXY))
     121             :     {
     122           0 :       dpo = fib_entry_contribute_ip_forwarding (fei);
     123           0 :       lb = load_balance_get (dpo->dpoi_index);
     124           0 :       dpo = load_balance_get_bucket_i (lb, 0);
     125           0 :       l3p = l3_proxy_dpo_get (dpo->dpoi_index);
     126           0 :       ctx->cb (pfx, l3p->l3p_sw_if_index, ctx->ctx);
     127             :     }
     128             : 
     129           0 :   return FIB_TABLE_WALK_CONTINUE;
     130             : }
     131             : 
     132             : void
     133           0 : ip_container_proxy_walk (ip_container_proxy_cb_t cb, void *ctx)
     134             : {
     135           0 :   ip_container_proxy_walk_ctx_t wctx = {
     136             :     .cb = cb,
     137             :     .ctx = ctx,
     138             :   };
     139             :   u32 fib_index;
     140             : 
     141             :   /* *INDENT-OFF* */
     142           0 :   pool_foreach_index (fib_index, ip4_main.fibs)
     143             :     {
     144           0 :       fib_table_walk (fib_index, FIB_PROTOCOL_IP4,
     145             :                       ip_container_proxy_fib_table_walk, &wctx);
     146             :     }
     147           0 :   pool_foreach_index (fib_index, ip6_main.fibs)
     148             :     {
     149           0 :       fib_table_walk (fib_index, FIB_PROTOCOL_IP6,
     150             :                       ip_container_proxy_fib_table_walk, &wctx);
     151             :     }
     152             :   /* *INDENT-ON* */
     153           0 : }
     154             : 
     155             : clib_error_t *
     156           0 : ip_container_cmd (vlib_main_t * vm,
     157             :                   unformat_input_t * main_input, vlib_cli_command_t * cmd)
     158             : {
     159           0 :   unformat_input_t _line_input, *line_input = &_line_input;
     160             :   fib_prefix_t pfx;
     161           0 :   u32 is_del, addr_set = 0;
     162             :   vnet_main_t *vnm;
     163             :   u32 sw_if_index;
     164             : 
     165           0 :   vnm = vnet_get_main ();
     166           0 :   is_del = 0;
     167           0 :   sw_if_index = ~0;
     168           0 :   clib_memset (&pfx, 0, sizeof (pfx));
     169             : 
     170             :   /* Get a line of input. */
     171           0 :   if (!unformat_user (main_input, unformat_line_input, line_input))
     172           0 :     return 0;
     173             : 
     174           0 :   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     175             :     {
     176           0 :       if (unformat (line_input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
     177             :         {
     178           0 :           pfx.fp_proto = FIB_PROTOCOL_IP4;
     179           0 :           pfx.fp_len = 32;
     180           0 :           addr_set = 1;
     181             :         }
     182           0 :       else if (unformat (line_input, "%U",
     183             :                          unformat_ip6_address, &pfx.fp_addr.ip6))
     184             :         {
     185           0 :           pfx.fp_proto = FIB_PROTOCOL_IP6;
     186           0 :           pfx.fp_len = 128;
     187           0 :           addr_set = 1;
     188             :         }
     189           0 :       else if (unformat (line_input, "%U",
     190             :                          unformat_vnet_sw_interface, vnm, &sw_if_index))
     191             :         ;
     192           0 :       else if (unformat (line_input, "del"))
     193           0 :         is_del = 1;
     194             :       else
     195             :         {
     196           0 :           unformat_free (line_input);
     197           0 :           return (clib_error_return (0, "unknown input '%U'",
     198             :                                      format_unformat_error, line_input));
     199             :         }
     200             :     }
     201             : 
     202           0 :   if (~0 == sw_if_index || !addr_set)
     203             :     {
     204           0 :       unformat_free (line_input);
     205           0 :       vlib_cli_output (vm, "interface and address must be set");
     206           0 :       return 0;
     207             :     }
     208             : 
     209           0 :   vnet_ip_container_proxy_args_t args = {
     210             :     .prefix = pfx,
     211             :     .sw_if_index = sw_if_index,
     212           0 :     .is_add = !is_del,
     213             :   };
     214           0 :   vnet_ip_container_proxy_add_del (&args);
     215           0 :   unformat_free (line_input);
     216           0 :   return (NULL);
     217             : }
     218             : 
     219             : /* *INDENT-OFF* */
     220      285289 : VLIB_CLI_COMMAND (ip_container_command_node, static) = {
     221             :   .path = "ip container",
     222             :   .function = ip_container_cmd,
     223             :   .short_help = "ip container <address> <interface>",
     224             :   .is_mp_safe = 1,
     225             : };
     226             : /* *INDENT-ON* */
     227             : 
     228             : clib_error_t *
     229           0 : show_ip_container_cmd_fn (vlib_main_t * vm, unformat_input_t * main_input,
     230             :                           vlib_cli_command_t * cmd)
     231             : {
     232           0 :   unformat_input_t _line_input, *line_input = &_line_input;
     233           0 :   vnet_main_t *vnm = vnet_get_main ();
     234             :   fib_prefix_t pfx;
     235           0 :   u32 sw_if_index = ~0;
     236             :   u8 has_proxy;
     237             : 
     238           0 :   if (!unformat_user (main_input, unformat_line_input, line_input))
     239           0 :     return 0;
     240           0 :   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     241             :     {
     242           0 :       if (unformat (line_input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
     243             :         {
     244           0 :           pfx.fp_proto = FIB_PROTOCOL_IP4;
     245           0 :           pfx.fp_len = 32;
     246             :         }
     247           0 :       else if (unformat (line_input, "%U",
     248             :                          unformat_ip6_address, &pfx.fp_addr.ip6))
     249             :         {
     250           0 :           pfx.fp_proto = FIB_PROTOCOL_IP6;
     251           0 :           pfx.fp_len = 128;
     252             :         }
     253           0 :       else if (unformat (line_input, "%U",
     254             :                          unformat_vnet_sw_interface, vnm, &sw_if_index))
     255             :         ;
     256             :       else
     257             :         {
     258           0 :           unformat_free (line_input);
     259           0 :           return (clib_error_return (0, "unknown input '%U'",
     260             :                                      format_unformat_error, line_input));
     261             :         }
     262             :     }
     263             : 
     264           0 :   if (~0 == sw_if_index)
     265             :     {
     266           0 :       unformat_free (line_input);
     267           0 :       vlib_cli_output (vm, "no interface");
     268           0 :       return (clib_error_return (0, "no interface"));
     269             :     }
     270             : 
     271           0 :   has_proxy = ip_container_proxy_is_set (&pfx, sw_if_index);
     272           0 :   vlib_cli_output (vm, "ip container proxy is: %s", has_proxy ? "on" : "off");
     273             : 
     274           0 :   unformat_free (line_input);
     275           0 :   return 0;
     276             : }
     277             : 
     278             : /* *INDENT-OFF* */
     279      285289 : VLIB_CLI_COMMAND (show_ip_container_command, static) = {
     280             :   .path = "show ip container",
     281             :   .function = show_ip_container_cmd_fn,
     282             :   .short_help = "show ip container <address> <interface>",
     283             :   .is_mp_safe = 1,
     284             : };
     285             : /* *INDENT-ON* */
     286             : 
     287             : /*
     288             :  * fd.io coding-style-patch-verification: ON
     289             :  *
     290             :  * Local Variables:
     291             :  * eval: (c-set-style "gnu")
     292             :  * End:
     293             :  */

Generated by: LCOV version 1.14