FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
ip4_inlines.h
Go to the documentation of this file.
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 
46 #define IP_DF 0x4000 /* don't fragment */
47 
48 /* Compute flow hash. We'll use it to select which adjacency to use for this
49  flow. And other things. */
52  flow_hash_config_t flow_hash_config)
53 {
54  tcp_header_t *tcp = (void *) (ip + 1);
55  u32 a, b, c, t1, t2;
56  uword is_tcp_udp = (ip->protocol == IP_PROTOCOL_TCP
57  || ip->protocol == IP_PROTOCOL_UDP);
58 
59  t1 = (flow_hash_config & IP_FLOW_HASH_SRC_ADDR)
60  ? ip->src_address.data_u32 : 0;
61  t2 = (flow_hash_config & IP_FLOW_HASH_DST_ADDR)
62  ? ip->dst_address.data_u32 : 0;
63 
64  a = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t2 : t1;
65  b = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t1 : t2;
66 
67  t1 = is_tcp_udp ? tcp->src : 0;
68  t2 = is_tcp_udp ? tcp->dst : 0;
69 
70  t1 = (flow_hash_config & IP_FLOW_HASH_SRC_PORT) ? t1 : 0;
71  t2 = (flow_hash_config & IP_FLOW_HASH_DST_PORT) ? t2 : 0;
72 
73  if (flow_hash_config & IP_FLOW_HASH_SYMMETRIC)
74  {
75  if (b < a)
76  {
77  c = a;
78  a = b;
79  b = c;
80  }
81  if (t2 < t1)
82  {
83  t2 += t1;
84  t1 = t2 - t1;
85  t2 = t2 - t1;
86  }
87  }
88 
89  b ^= (flow_hash_config & IP_FLOW_HASH_PROTO) ? ip->protocol : 0;
90  c = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ?
91  (t1 << 16) | t2 : (t2 << 16) | t1;
93 
94  hash_v3_mix32 (a, b, c);
95  hash_v3_finalize32 (a, b, c);
96 
97  return c;
98 }
99 
100 always_inline void *
103  int proto, u8 csum_offload, u8 is_df)
104 {
105  ip4_header_t *ih;
106 
107  /* make some room */
108  ih = vlib_buffer_push_uninit (b, sizeof (ip4_header_t));
109 
110  ih->ip_version_and_header_length = 0x45;
111  ih->tos = 0;
112  ih->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b));
113 
114  /* No fragments */
115  ih->flags_and_fragment_offset = is_df ? clib_host_to_net_u16 (IP_DF) : 0;
116  ih->ttl = 255;
117  ih->protocol = proto;
118  ih->src_address.as_u32 = src->as_u32;
119  ih->dst_address.as_u32 = dst->as_u32;
120 
121  vnet_buffer (b)->l3_hdr_offset = (u8 *) ih - b->data;
122  b->flags |= VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
123 
124  /* Offload ip4 header checksum generation */
125  if (csum_offload)
126  {
127  ih->checksum = 0;
128  vnet_buffer_offload_flags_set (b, VNET_BUFFER_OFFLOAD_F_IP_CKSUM);
129  }
130  else
131  ih->checksum = ip4_header_checksum (ih);
132 
133  return ih;
134 }
135 
136 /**
137  * Push IPv4 header to buffer
138  *
139  * This does not support fragmentation.
140  *
141  * @param vm - vlib_main
142  * @param b - buffer to write the header to
143  * @param src - source IP
144  * @param dst - destination IP
145  * @param prot - payload proto
146  *
147  * @return - pointer to start of IP header
148  */
149 always_inline void *
152  u8 csum_offload)
153 {
154  return vlib_buffer_push_ip4_custom (vm, b, src, dst, proto, csum_offload,
155  1 /* is_df */ );
156 }
157 
158 #endif /* included_ip_ip4_inlines_h */
159 
160 /*
161  * fd.io coding-style-patch-verification: ON
162  *
163  * Local Variables:
164  * eval: (c-set-style "gnu")
165  * End:
166  */
tcp_header_t
struct _tcp_header tcp_header_t
vnet_buffer_offload_flags_set
static_always_inline void vnet_buffer_offload_flags_set(vlib_buffer_t *b, vnet_buffer_oflags_t oflags)
Definition: buffer.h:522
ip4_address_t::as_u32
u32 as_u32
Definition: ip4_packet.h:57
vlib_buffer_push_ip4
static void * vlib_buffer_push_ip4(vlib_main_t *vm, vlib_buffer_t *b, ip4_address_t *src, ip4_address_t *dst, int proto, u8 csum_offload)
Push IPv4 header to buffer.
Definition: ip4_inlines.h:150
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
ip_flow_hash_router_id
u32 ip_flow_hash_router_id
Definition: ip.c:19
vlib_buffer_length_in_chain
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:433
ip4_header_t
Definition: ip4_packet.h:87
ip4_header_t::tos
ip_dscp_t tos
Definition: ip4_packet.h:96
ip4_header_t::length
u16 length
Definition: ip4_packet.h:99
ip_flow_hash.h
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
IP_DF
#define IP_DF
Definition: ip4_inlines.h:46
flow_hash_config_t
enum flow_hash_config_t_ flow_hash_config_t
A flow hash configuration is a mask of the flow hash options.
c
svmdb_client_t * c
Definition: vpp_get_metrics.c:48
uword
u64 uword
Definition: types.h:112
if
if(node->flags &VLIB_NODE_FLAG_TRACE) vnet_interface_output_trace(vm
ip4_header_t::checksum
u16 checksum
Definition: ip4_packet.h:118
src
vl_api_address_t src
Definition: gre.api:54
hash_v3_finalize32
#define hash_v3_finalize32(a, b, c)
Definition: hash.h:563
ip4_address_t
Definition: ip4_packet.h:50
ip4_header_t::dst_address
ip4_address_t dst_address
Definition: ip4_packet.h:125
ip4_packet.h
vlib_buffer_push_uninit
static void * vlib_buffer_push_uninit(vlib_buffer_t *b, u8 size)
Prepend uninitialized data to buffer.
Definition: buffer.h:363
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
ip4_header_t::src_address
ip4_address_t src_address
Definition: ip4_packet.h:125
u32
unsigned int u32
Definition: types.h:88
ip4_header_t::ttl
u8 ttl
Definition: ip4_packet.h:112
dst
vl_api_ip4_address_t dst
Definition: pnat.api:41
ip4_compute_flow_hash
static u32 ip4_compute_flow_hash(const ip4_header_t *ip, flow_hash_config_t flow_hash_config)
Definition: ip4_inlines.h:51
ip4_header_t::flags_and_fragment_offset
u16 flags_and_fragment_offset
Definition: ip4_packet.h:106
vlib_main_t
Definition: main.h:102
ip4_header_t::ip_version_and_header_length
u8 ip_version_and_header_length
Definition: ip4_packet.h:93
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
a
a
Definition: bitmap.h:544
ip
vl_api_address_t ip
Definition: l2.api:558
ip4_header_checksum
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:314
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
vlib_buffer_push_ip4_custom
static void * vlib_buffer_push_ip4_custom(vlib_main_t *vm, vlib_buffer_t *b, ip4_address_t *src, ip4_address_t *dst, int proto, u8 csum_offload, u8 is_df)
Definition: ip4_inlines.h:101
proto
vl_api_ip_proto_t proto
Definition: acl_types.api:51
hash_v3_mix32
#define hash_v3_mix32(a, b, c)
Definition: hash.h:553
ip4_header_t::protocol
u8 protocol
Definition: ip4_packet.h:115
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111