LCOV - code coverage report
Current view: top level - vnet/adj - rewrite.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 60 80 75.0 %
Date: 2023-07-05 22:20:52 Functions: 8 10 80.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             :  * rewrite.c: packet rewrite
      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/vnet.h>
      41             : #include <vnet/ip/lookup.h>
      42             : 
      43             : u8 *
      44      361834 : format_vnet_rewrite_flags (u8 *s, va_list *ap)
      45             : {
      46      361834 :   vnet_rewrite_flags_t f = va_arg (*ap, int);
      47             : 
      48      361834 :   if (f & VNET_REWRITE_HAS_FEATURES)
      49      176497 :     s = format (s, "features ");
      50      361834 :   if (f & VNET_REWRITE_FIXUP_IP4_O_4)
      51        7609 :     s = format (s, "fixup-ip4o4 ");
      52      361834 :   if (f & VNET_REWRITE_FIXUP_FLOW_HASH)
      53           0 :     s = format (s, "fixup-flow-hash ");
      54             : 
      55      361834 :   return (s);
      56             : }
      57             : 
      58             : u8 *
      59      361834 : format_vnet_rewrite (u8 * s, va_list * args)
      60             : {
      61      361834 :   vnet_rewrite_header_t *rw = va_arg (*args, vnet_rewrite_header_t *);
      62      361834 :   u32 max_data_bytes = va_arg (*args, u32);
      63      361834 :   CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
      64      361834 :   vnet_main_t *vnm = vnet_get_main ();
      65             : 
      66      361834 :   ASSERT (rw->data_bytes <= max_data_bytes);
      67             : 
      68      361834 :   if (rw->sw_if_index != ~0)
      69             :     {
      70             :       vnet_sw_interface_t *si;
      71      361834 :       si = vnet_get_sw_interface_or_null (vnm, rw->sw_if_index);
      72      361834 :       if (NULL != si)
      73      361824 :         s = format (s, "%U:", format_vnet_sw_interface_name, vnm, si);
      74             :       else
      75          10 :         s = format (s, "DELETED:%d", rw->sw_if_index);
      76             :     }
      77             : 
      78      361834 :   s = format (s, " mtu:%d next:%d", rw->max_l3_packet_bytes, rw->next_index);
      79      361834 :   s = format (s, " flags:[%U]", format_vnet_rewrite_flags, rw->flags);
      80             : 
      81             :   /* Format rewrite string. */
      82      361834 :   if (rw->data_bytes > 0)
      83      347390 :     s = format (s, " %U", format_hex_bytes, rw->data, rw->data_bytes);
      84             : 
      85      361834 :   return s;
      86             : }
      87             : 
      88             : u32
      89       49005 : vnet_tx_node_index_for_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
      90             : {
      91       49005 :   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
      92       49005 :   return (hw->output_node_index);
      93             : }
      94             : 
      95             : void
      96       22160 : vnet_rewrite_init (vnet_main_t * vnm,
      97             :                    u32 sw_if_index,
      98             :                    vnet_link_t linkt,
      99             :                    u32 this_node, u32 next_node, vnet_rewrite_header_t * rw)
     100             : {
     101       22160 :   rw->sw_if_index = sw_if_index;
     102       22160 :   rw->next_index = vlib_node_add_next (vnm->vlib_main, this_node, next_node);
     103       22160 :   rw->max_l3_packet_bytes =
     104       22160 :     vnet_sw_interface_get_mtu (vnm, sw_if_index, vnet_link_to_mtu (linkt));
     105       22160 : }
     106             : 
     107             : void
     108       10313 : vnet_rewrite_update_mtu (vnet_main_t * vnm, vnet_link_t linkt,
     109             :                          vnet_rewrite_header_t * rw)
     110             : {
     111       10313 :   rw->max_l3_packet_bytes =
     112       10313 :     vnet_sw_interface_get_mtu (vnm, rw->sw_if_index,
     113             :                                vnet_link_to_mtu (linkt));
     114       10313 : }
     115             : 
     116             : void
     117       10149 : vnet_rewrite_for_sw_interface (vnet_main_t * vnm,
     118             :                                vnet_link_t link_type,
     119             :                                u32 sw_if_index,
     120             :                                u32 node_index,
     121             :                                void *dst_address,
     122             :                                vnet_rewrite_header_t * rw,
     123             :                                u32 max_rewrite_bytes)
     124             : {
     125             : 
     126       10149 :   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
     127             :   vnet_hw_interface_class_t *hc =
     128       10149 :     vnet_get_hw_interface_class (vnm, hw->hw_class_index);
     129       10149 :   u8 *rewrite = NULL;
     130             : 
     131       10149 :   vnet_rewrite_init (vnm, sw_if_index, link_type, node_index,
     132             :                      vnet_tx_node_index_for_sw_interface (vnm, sw_if_index),
     133             :                      rw);
     134             : 
     135       10149 :   ASSERT (hc->build_rewrite);
     136       10149 :   rewrite = hc->build_rewrite (vnm, sw_if_index, link_type, dst_address);
     137             : 
     138       10149 :   ASSERT (vec_len (rewrite) < max_rewrite_bytes);
     139       10149 :   vnet_rewrite_set_data_internal (rw, max_rewrite_bytes, rewrite,
     140       10149 :                                   vec_len (rewrite));
     141       10149 :   vec_free (rewrite);
     142       10149 : }
     143             : 
     144             : void
     145           0 : serialize_vnet_rewrite (serialize_main_t * m, va_list * va)
     146             : {
     147           0 :   vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *);
     148           0 :   u32 max_data_bytes = va_arg (*va, u32);
     149             :   u8 *p;
     150             : 
     151           0 :   serialize_integer (m, rw->sw_if_index, sizeof (rw->sw_if_index));
     152           0 :   serialize_integer (m, rw->data_bytes, sizeof (rw->data_bytes));
     153           0 :   serialize_integer (m, rw->max_l3_packet_bytes,
     154             :                      sizeof (rw->max_l3_packet_bytes));
     155           0 :   p = serialize_get (m, rw->data_bytes);
     156           0 :   clib_memcpy (p, vnet_rewrite_get_data_internal (rw, max_data_bytes),
     157             :                rw->data_bytes);
     158           0 : }
     159             : 
     160             : void
     161           0 : unserialize_vnet_rewrite (serialize_main_t * m, va_list * va)
     162             : {
     163           0 :   vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *);
     164           0 :   u32 max_data_bytes = va_arg (*va, u32);
     165             :   u8 *p;
     166             : 
     167             :   /* It is up to user to fill these in. */
     168           0 :   rw->next_index = ~0;
     169             : 
     170           0 :   unserialize_integer (m, &rw->sw_if_index, sizeof (rw->sw_if_index));
     171           0 :   unserialize_integer (m, &rw->data_bytes, sizeof (rw->data_bytes));
     172           0 :   unserialize_integer (m, &rw->max_l3_packet_bytes,
     173             :                        sizeof (rw->max_l3_packet_bytes));
     174           0 :   p = unserialize_get (m, rw->data_bytes);
     175           0 :   clib_memcpy (vnet_rewrite_get_data_internal (rw, max_data_bytes), p,
     176             :                rw->data_bytes);
     177           0 : }
     178             : 
     179             : u8 *
     180          90 : vnet_build_rewrite_for_sw_interface (vnet_main_t * vnm,
     181             :                                      u32 sw_if_index,
     182             :                                      vnet_link_t link_type,
     183             :                                      const void *dst_address)
     184             : {
     185          90 :   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
     186             :   vnet_hw_interface_class_t *hc =
     187          90 :     vnet_get_hw_interface_class (vnm, hw->hw_class_index);
     188             : 
     189          90 :   ASSERT (hc->build_rewrite);
     190          90 :   return (hc->build_rewrite (vnm, sw_if_index, link_type, dst_address));
     191             : }
     192             : 
     193             : 
     194             : void
     195       22405 : vnet_update_adjacency_for_sw_interface (vnet_main_t * vnm,
     196             :                                         u32 sw_if_index, u32 ai)
     197             : {
     198       22405 :   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
     199             :   vnet_hw_interface_class_t *hc =
     200       22405 :     vnet_get_hw_interface_class (vnm, hw->hw_class_index);
     201             : 
     202       22405 :   ASSERT (hc->update_adjacency);
     203       22405 :   hc->update_adjacency (vnm, sw_if_index, ai);
     204       22405 : }
     205             : 
     206             : /*
     207             :  * fd.io coding-style-patch-verification: ON
     208             :  *
     209             :  * Local Variables:
     210             :  * eval: (c-set-style "gnu")
     211             :  * End:
     212             :  */

Generated by: LCOV version 1.14