LCOV - code coverage report
Current view: top level - vnet/ip - ip4_inlines.h (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 47 56 83.9 %
Date: 2023-10-26 01:39:38 Functions: 3 3 100.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/ip4.h: ip4 main include file
      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             : #ifndef included_ip_ip4_inlines_h
      41             : #define included_ip_ip4_inlines_h
      42             : 
      43             : #include <vnet/ip/ip_flow_hash.h>
      44             : #include <vnet/ip/ip4_packet.h>
      45             : #include <vnet/tcp/tcp_packet.h>
      46             : #include <vnet/udp/udp_packet.h>
      47             : 
      48             : #define IP_DF 0x4000            /* don't fragment */
      49             : 
      50             : /* Compute flow hash.  We'll use it to select which adjacency to use for this
      51             :    flow.  And other things. */
      52             : always_inline u32
      53        4909 : ip4_compute_flow_hash (const ip4_header_t * ip,
      54             :                        flow_hash_config_t flow_hash_config)
      55             : {
      56        4909 :   tcp_header_t *tcp = (void *) (ip + 1);
      57        4909 :   udp_header_t *udp = (void *) (ip + 1);
      58        4909 :   gtpv1u_header_t *gtpu = (void *) (udp + 1);
      59             :   u32 a, b, c, t1, t2;
      60        4909 :   uword is_udp = ip->protocol == IP_PROTOCOL_UDP;
      61        4909 :   uword is_tcp_udp = (ip->protocol == IP_PROTOCOL_TCP || is_udp);
      62             : 
      63        9818 :   t1 = (flow_hash_config & IP_FLOW_HASH_SRC_ADDR)
      64        4909 :     ? ip->src_address.data_u32 : 0;
      65        9818 :   t2 = (flow_hash_config & IP_FLOW_HASH_DST_ADDR)
      66        4909 :     ? ip->dst_address.data_u32 : 0;
      67             : 
      68        4909 :   a = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t2 : t1;
      69        4909 :   b = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t1 : t2;
      70             : 
      71        4909 :   t1 = is_tcp_udp ? tcp->src : 0;
      72        4909 :   t2 = is_tcp_udp ? tcp->dst : 0;
      73             : 
      74        4909 :   t1 = (flow_hash_config & IP_FLOW_HASH_SRC_PORT) ? t1 : 0;
      75        4909 :   t2 = (flow_hash_config & IP_FLOW_HASH_DST_PORT) ? t2 : 0;
      76             : 
      77        4909 :   if (flow_hash_config & IP_FLOW_HASH_SYMMETRIC)
      78             :     {
      79           0 :       if (b < a)
      80             :         {
      81           0 :           c = a;
      82           0 :           a = b;
      83           0 :           b = c;
      84             :         }
      85           0 :       if (t2 < t1)
      86             :         {
      87           0 :           t2 += t1;
      88           0 :           t1 = t2 - t1;
      89           0 :           t2 = t2 - t1;
      90             :         }
      91             :     }
      92             : 
      93        4909 :   b ^= (flow_hash_config & IP_FLOW_HASH_PROTO) ? ip->protocol : 0;
      94        9818 :   c = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ?
      95        4909 :     (t1 << 16) | t2 : (t2 << 16) | t1;
      96        4909 :   if (PREDICT_TRUE (is_udp) &&
      97        4429 :       PREDICT_FALSE ((flow_hash_config & IP_FLOW_HASH_GTPV1_TEID) &&
      98             :                      udp->dst_port == GTPV1_PORT_BE))
      99             :     {
     100         134 :       t1 = gtpu->teid;
     101         134 :       c ^= t1;
     102             :     }
     103        4909 :   a ^= ip_flow_hash_router_id;
     104             : 
     105        4909 :   hash_v3_mix32 (a, b, c);
     106        4909 :   hash_v3_finalize32 (a, b, c);
     107             : 
     108        4909 :   return c;
     109             : }
     110             : 
     111             : always_inline void *
     112     1121571 : vlib_buffer_push_ip4_custom (vlib_main_t *vm, vlib_buffer_t *b,
     113             :                              ip4_address_t *src, ip4_address_t *dst, int proto,
     114             :                              u8 csum_offload, u8 is_df, u8 dscp)
     115             : {
     116             :   ip4_header_t *ih;
     117             : 
     118             :   /* make some room */
     119     1121571 :   ih = vlib_buffer_push_uninit (b, sizeof (ip4_header_t));
     120             : 
     121     1121571 :   ih->ip_version_and_header_length = 0x45;
     122     1121571 :   ip4_header_set_dscp (ih, dscp);
     123     1121571 :   ip4_header_set_ecn (ih, 0);
     124     1121571 :   ih->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b));
     125             : 
     126             :   /* No fragments */
     127     1121571 :   ih->flags_and_fragment_offset = is_df ? clib_host_to_net_u16 (IP_DF) : 0;
     128     1121571 :   ih->ttl = 255;
     129     1121571 :   ih->protocol = proto;
     130     1121571 :   ih->src_address.as_u32 = src->as_u32;
     131     1121571 :   ih->dst_address.as_u32 = dst->as_u32;
     132             : 
     133     1121571 :   vnet_buffer (b)->l3_hdr_offset = (u8 *) ih - b->data;
     134     1121571 :   b->flags |= VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
     135             : 
     136             :   /* Offload ip4 header checksum generation */
     137     1121571 :   if (csum_offload)
     138             :     {
     139     1121571 :       ih->checksum = 0;
     140     1121571 :       vnet_buffer_offload_flags_set (b, VNET_BUFFER_OFFLOAD_F_IP_CKSUM);
     141             :     }
     142             :   else
     143           0 :     ih->checksum = ip4_header_checksum (ih);
     144             : 
     145     1121571 :   return ih;
     146             : }
     147             : 
     148             : /**
     149             :  * Push IPv4 header to buffer
     150             :  *
     151             :  * This does not support fragmentation.
     152             :  *
     153             :  * @param vm - vlib_main
     154             :  * @param b - buffer to write the header to
     155             :  * @param src - source IP
     156             :  * @param dst - destination IP
     157             :  * @param prot - payload proto
     158             :  *
     159             :  * @return - pointer to start of IP header
     160             :  */
     161             : always_inline void *
     162     1062506 : vlib_buffer_push_ip4 (vlib_main_t * vm, vlib_buffer_t * b,
     163             :                       ip4_address_t * src, ip4_address_t * dst, int proto,
     164             :                       u8 csum_offload)
     165             : {
     166     1062506 :   return vlib_buffer_push_ip4_custom (vm, b, src, dst, proto, csum_offload,
     167             :                                       1 /* is_df */, 0);
     168             : }
     169             : 
     170             : #endif /* included_ip_ip4_inlines_h */
     171             : 
     172             : /*
     173             :  * fd.io coding-style-patch-verification: ON
     174             :  *
     175             :  * Local Variables:
     176             :  * eval: (c-set-style "gnu")
     177             :  * End:
     178             :  */

Generated by: LCOV version 1.14