Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 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> |
Neale Ranns | f7040f0 | 2022-02-15 09:02:27 +0000 | [diff] [blame] | 45 | #include <vnet/tcp/tcp_packet.h> |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 46 | |
| 47 | #define IP_DF 0x4000 /* don't fragment */ |
| 48 | |
| 49 | /* Compute flow hash. We'll use it to select which adjacency to use for this |
| 50 | flow. And other things. */ |
| 51 | always_inline u32 |
| 52 | ip4_compute_flow_hash (const ip4_header_t * ip, |
| 53 | flow_hash_config_t flow_hash_config) |
| 54 | { |
| 55 | tcp_header_t *tcp = (void *) (ip + 1); |
| 56 | u32 a, b, c, t1, t2; |
| 57 | uword is_tcp_udp = (ip->protocol == IP_PROTOCOL_TCP |
| 58 | || ip->protocol == IP_PROTOCOL_UDP); |
| 59 | |
| 60 | t1 = (flow_hash_config & IP_FLOW_HASH_SRC_ADDR) |
| 61 | ? ip->src_address.data_u32 : 0; |
| 62 | t2 = (flow_hash_config & IP_FLOW_HASH_DST_ADDR) |
| 63 | ? ip->dst_address.data_u32 : 0; |
| 64 | |
| 65 | a = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t2 : t1; |
| 66 | b = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t1 : t2; |
| 67 | |
| 68 | t1 = is_tcp_udp ? tcp->src : 0; |
| 69 | t2 = is_tcp_udp ? tcp->dst : 0; |
| 70 | |
| 71 | t1 = (flow_hash_config & IP_FLOW_HASH_SRC_PORT) ? t1 : 0; |
| 72 | t2 = (flow_hash_config & IP_FLOW_HASH_DST_PORT) ? t2 : 0; |
| 73 | |
| 74 | if (flow_hash_config & IP_FLOW_HASH_SYMMETRIC) |
| 75 | { |
| 76 | if (b < a) |
| 77 | { |
| 78 | c = a; |
| 79 | a = b; |
| 80 | b = c; |
| 81 | } |
| 82 | if (t2 < t1) |
| 83 | { |
| 84 | t2 += t1; |
| 85 | t1 = t2 - t1; |
| 86 | t2 = t2 - t1; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | b ^= (flow_hash_config & IP_FLOW_HASH_PROTO) ? ip->protocol : 0; |
| 91 | c = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? |
| 92 | (t1 << 16) | t2 : (t2 << 16) | t1; |
Neale Ranns | 3d5f08a | 2021-01-22 16:12:38 +0000 | [diff] [blame] | 93 | a ^= ip_flow_hash_router_id; |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 94 | |
| 95 | hash_v3_mix32 (a, b, c); |
| 96 | hash_v3_finalize32 (a, b, c); |
| 97 | |
| 98 | return c; |
| 99 | } |
| 100 | |
| 101 | always_inline void * |
Filip Tehlar | 3ef8bf3 | 2021-10-21 14:07:31 +0000 | [diff] [blame] | 102 | vlib_buffer_push_ip4_custom (vlib_main_t *vm, vlib_buffer_t *b, |
| 103 | ip4_address_t *src, ip4_address_t *dst, int proto, |
| 104 | u8 csum_offload, u8 is_df, u8 dscp) |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 105 | { |
| 106 | ip4_header_t *ih; |
| 107 | |
| 108 | /* make some room */ |
| 109 | ih = vlib_buffer_push_uninit (b, sizeof (ip4_header_t)); |
| 110 | |
| 111 | ih->ip_version_and_header_length = 0x45; |
Filip Tehlar | 3ef8bf3 | 2021-10-21 14:07:31 +0000 | [diff] [blame] | 112 | ip4_header_set_dscp (ih, dscp); |
| 113 | ip4_header_set_ecn (ih, 0); |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 114 | ih->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b)); |
| 115 | |
| 116 | /* No fragments */ |
| 117 | ih->flags_and_fragment_offset = is_df ? clib_host_to_net_u16 (IP_DF) : 0; |
| 118 | ih->ttl = 255; |
| 119 | ih->protocol = proto; |
| 120 | ih->src_address.as_u32 = src->as_u32; |
| 121 | ih->dst_address.as_u32 = dst->as_u32; |
| 122 | |
| 123 | vnet_buffer (b)->l3_hdr_offset = (u8 *) ih - b->data; |
| 124 | b->flags |= VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID; |
| 125 | |
| 126 | /* Offload ip4 header checksum generation */ |
| 127 | if (csum_offload) |
| 128 | { |
| 129 | ih->checksum = 0; |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 130 | vnet_buffer_offload_flags_set (b, VNET_BUFFER_OFFLOAD_F_IP_CKSUM); |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 131 | } |
| 132 | else |
| 133 | ih->checksum = ip4_header_checksum (ih); |
| 134 | |
| 135 | return ih; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Push IPv4 header to buffer |
| 140 | * |
| 141 | * This does not support fragmentation. |
| 142 | * |
| 143 | * @param vm - vlib_main |
| 144 | * @param b - buffer to write the header to |
| 145 | * @param src - source IP |
| 146 | * @param dst - destination IP |
| 147 | * @param prot - payload proto |
| 148 | * |
| 149 | * @return - pointer to start of IP header |
| 150 | */ |
| 151 | always_inline void * |
| 152 | vlib_buffer_push_ip4 (vlib_main_t * vm, vlib_buffer_t * b, |
| 153 | ip4_address_t * src, ip4_address_t * dst, int proto, |
| 154 | u8 csum_offload) |
| 155 | { |
| 156 | return vlib_buffer_push_ip4_custom (vm, b, src, dst, proto, csum_offload, |
Filip Tehlar | 3ef8bf3 | 2021-10-21 14:07:31 +0000 | [diff] [blame] | 157 | 1 /* is_df */, 0); |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | #endif /* included_ip_ip4_inlines_h */ |
| 161 | |
| 162 | /* |
| 163 | * fd.io coding-style-patch-verification: ON |
| 164 | * |
| 165 | * Local Variables: |
| 166 | * eval: (c-set-style "gnu") |
| 167 | * End: |
| 168 | */ |