Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 1 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include <vppinfra/error.h> |
| 17 | #include <vppinfra/hash.h> |
| 18 | #include <vnet/vnet.h> |
| 19 | #include <vnet/ip/ip.h> |
| 20 | #include <vnet/ethernet/ethernet.h> |
| 21 | #include <vnet/vxlan/vxlan.h> |
| 22 | |
| 23 | /* Statistics (not all errors) */ |
| 24 | #define foreach_vxlan_encap_error \ |
John Lo | 3ef822e | 2016-06-07 09:14:07 -0400 | [diff] [blame] | 25 | _(ENCAPSULATED, "good packets encapsulated") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | |
| 27 | static char * vxlan_encap_error_strings[] = { |
| 28 | #define _(sym,string) string, |
| 29 | foreach_vxlan_encap_error |
| 30 | #undef _ |
| 31 | }; |
| 32 | |
| 33 | typedef enum { |
| 34 | #define _(sym,str) VXLAN_ENCAP_ERROR_##sym, |
| 35 | foreach_vxlan_encap_error |
| 36 | #undef _ |
| 37 | VXLAN_ENCAP_N_ERROR, |
| 38 | } vxlan_encap_error_t; |
| 39 | |
| 40 | typedef enum { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | VXLAN_ENCAP_NEXT_DROP, |
| 42 | VXLAN_ENCAP_N_NEXT, |
| 43 | } vxlan_encap_next_t; |
| 44 | |
| 45 | typedef struct { |
| 46 | u32 tunnel_index; |
| 47 | u32 vni; |
| 48 | } vxlan_encap_trace_t; |
| 49 | |
| 50 | u8 * format_vxlan_encap_trace (u8 * s, va_list * args) |
| 51 | { |
| 52 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 53 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 54 | vxlan_encap_trace_t * t |
| 55 | = va_arg (*args, vxlan_encap_trace_t *); |
| 56 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 57 | s = format (s, "VXLAN encap to vxlan_tunnel%d vni %d", |
| 58 | t->tunnel_index, t->vni); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | return s; |
| 60 | } |
| 61 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 62 | always_inline uword |
| 63 | vxlan_encap_inline (vlib_main_t * vm, |
| 64 | vlib_node_runtime_t * node, |
| 65 | vlib_frame_t * from_frame, |
John Lo | f4215a6 | 2017-09-18 00:20:05 -0400 | [diff] [blame] | 66 | u8 is_ip4, u8 csum_offload) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | { |
| 68 | u32 n_left_from, next_index, * from, * to_next; |
| 69 | vxlan_main_t * vxm = &vxlan_main; |
| 70 | vnet_main_t * vnm = vxm->vnet_main; |
| 71 | vnet_interface_main_t * im = &vnm->interface_main; |
John Lo | 25d417f | 2018-02-15 15:47:53 -0500 | [diff] [blame] | 72 | vlib_combined_counter_main_t * tx_counter = |
| 73 | im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | u32 pkts_encapsulated = 0; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 75 | u32 thread_index = vlib_get_thread_index(); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 76 | u32 sw_if_index0 = 0, sw_if_index1 = 0; |
| 77 | u32 next0 = 0, next1 = 0; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 78 | vxlan_tunnel_t * t0 = NULL, * t1 = NULL; |
John Lo | 25d417f | 2018-02-15 15:47:53 -0500 | [diff] [blame] | 79 | index_t dpoi_idx0 = INDEX_INVALID, dpoi_idx1 = INDEX_INVALID; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | |
| 81 | from = vlib_frame_vector_args (from_frame); |
| 82 | n_left_from = from_frame->n_vectors; |
| 83 | |
| 84 | next_index = node->cached_next_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 86 | STATIC_ASSERT_SIZEOF(ip6_vxlan_header_t, 56); |
| 87 | STATIC_ASSERT_SIZEOF(ip4_vxlan_header_t, 36); |
| 88 | |
| 89 | word const underlay_hdr_len = is_ip4 ? |
| 90 | sizeof(ip4_vxlan_header_t) : sizeof(ip6_vxlan_header_t); |
| 91 | u16 const l3_len = is_ip4 ? sizeof(ip4_header_t) : sizeof(ip6_header_t); |
| 92 | u32 const csum_flags = is_ip4 ? |
| 93 | VNET_BUFFER_F_OFFLOAD_IP_CKSUM | VNET_BUFFER_F_IS_IP4 | |
| 94 | VNET_BUFFER_F_OFFLOAD_UDP_CKSUM : |
| 95 | VNET_BUFFER_F_OFFLOAD_UDP_CKSUM; |
| 96 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | while (n_left_from > 0) |
| 98 | { |
| 99 | u32 n_left_to_next; |
| 100 | |
| 101 | vlib_get_next_frame (vm, node, next_index, |
| 102 | to_next, n_left_to_next); |
| 103 | |
| 104 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 105 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | /* Prefetch next iteration. */ |
| 107 | { |
| 108 | vlib_buffer_t * p2, * p3; |
| 109 | |
| 110 | p2 = vlib_get_buffer (vm, from[2]); |
| 111 | p3 = vlib_get_buffer (vm, from[3]); |
| 112 | |
| 113 | vlib_prefetch_buffer_header (p2, LOAD); |
| 114 | vlib_prefetch_buffer_header (p3, LOAD); |
| 115 | |
| 116 | CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); |
| 117 | CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); |
| 118 | } |
| 119 | |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 120 | u32 bi0 = from[0]; |
| 121 | u32 bi1 = from[1]; |
| 122 | |
| 123 | vlib_buffer_t * b0 = vlib_get_buffer (vm, bi0); |
| 124 | vlib_buffer_t * b1 = vlib_get_buffer (vm, bi1); |
| 125 | u32 flow_hash0 = vnet_l2_compute_flow_hash (b0); |
| 126 | u32 flow_hash1 = vnet_l2_compute_flow_hash (b1); |
| 127 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | to_next[0] = bi0; |
| 129 | to_next[1] = bi1; |
| 130 | from += 2; |
| 131 | to_next += 2; |
| 132 | n_left_to_next -= 2; |
| 133 | n_left_from -= 2; |
| 134 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 135 | /* Get next node index and adj index from tunnel next_dpo */ |
| 136 | if (sw_if_index0 != vnet_buffer(b0)->sw_if_index[VLIB_TX]) |
| 137 | { |
| 138 | sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX]; |
John Lo | 25d417f | 2018-02-15 15:47:53 -0500 | [diff] [blame] | 139 | vnet_hw_interface_t *hi0 = |
| 140 | vnet_get_sup_hw_interface (vnm, sw_if_index0); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 141 | t0 = &vxm->tunnels[hi0->dev_instance]; |
John Lo | 25d417f | 2018-02-15 15:47:53 -0500 | [diff] [blame] | 142 | /* Note: change to always set next0 if it may set to drop */ |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 143 | next0 = t0->next_dpo.dpoi_next_node; |
John Lo | 25d417f | 2018-02-15 15:47:53 -0500 | [diff] [blame] | 144 | dpoi_idx0 = t0->next_dpo.dpoi_index; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 145 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 147 | /* Get next node index and adj index from tunnel next_dpo */ |
| 148 | if (sw_if_index1 != vnet_buffer(b1)->sw_if_index[VLIB_TX]) |
| 149 | { |
John Lo | 25d417f | 2018-02-15 15:47:53 -0500 | [diff] [blame] | 150 | if (sw_if_index0 == vnet_buffer(b1)->sw_if_index[VLIB_TX]) |
| 151 | { |
| 152 | sw_if_index1 = sw_if_index0; |
| 153 | t1 = t0; |
| 154 | next1 = next0; |
| 155 | dpoi_idx1 = dpoi_idx0; |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_TX]; |
| 160 | vnet_hw_interface_t *hi1 = |
| 161 | vnet_get_sup_hw_interface (vnm, sw_if_index1); |
| 162 | t1 = &vxm->tunnels[hi1->dev_instance]; |
| 163 | /* Note: change to always set next1 if it may set to drop */ |
| 164 | next1 = t1->next_dpo.dpoi_next_node; |
| 165 | dpoi_idx1 = t1->next_dpo.dpoi_index; |
| 166 | } |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 167 | } |
John Lo | 25d417f | 2018-02-15 15:47:53 -0500 | [diff] [blame] | 168 | |
| 169 | vnet_buffer(b0)->ip.adj_index[VLIB_TX] = dpoi_idx0; |
| 170 | vnet_buffer(b1)->ip.adj_index[VLIB_TX] = dpoi_idx1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 172 | ASSERT(vec_len(t0->rewrite) == underlay_hdr_len); |
| 173 | ASSERT(vec_len(t1->rewrite) == underlay_hdr_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 175 | vlib_buffer_advance (b0, -underlay_hdr_len); |
| 176 | vlib_buffer_advance (b1, -underlay_hdr_len); |
| 177 | |
| 178 | u32 len0 = vlib_buffer_length_in_chain (vm, b0); |
| 179 | u32 len1 = vlib_buffer_length_in_chain (vm, b1); |
| 180 | u16 payload_l0 = clib_host_to_net_u16 (len0 - l3_len); |
| 181 | u16 payload_l1 = clib_host_to_net_u16 (len1 - l3_len); |
| 182 | |
| 183 | ip4_header_t * ip4_0, * ip4_1; |
| 184 | ip6_header_t * ip6_0, * ip6_1; |
| 185 | udp_header_t * udp0, * udp1; |
| 186 | u8 * l3_0, * l3_1; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 187 | if (is_ip4) |
| 188 | { |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 189 | ip4_vxlan_header_t * hdr0 = vlib_buffer_get_current(b0); |
| 190 | ip4_vxlan_header_t * rewrite0 = (void *)t0->rewrite; |
| 191 | ip4_vxlan_header_t * hdr1 = vlib_buffer_get_current(b1); |
| 192 | ip4_vxlan_header_t * rewrite1 = (void *)t1->rewrite; |
| 193 | *hdr0 = *rewrite0; |
| 194 | *hdr1 = *rewrite1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 196 | /* Fix the IP4 checksum and length */ |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 197 | ip4_0 = &hdr0->ip4; |
| 198 | ip4_1 = &hdr1->ip4; |
| 199 | ip4_0->length = clib_host_to_net_u16 (len0); |
| 200 | ip4_1->length = clib_host_to_net_u16 (len1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 202 | l3_0 = (u8 *)ip4_0; |
| 203 | l3_1 = (u8 *)ip4_1; |
| 204 | udp0 = &hdr0->udp; |
| 205 | udp1 = &hdr1->udp; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 206 | } |
| 207 | else /* ipv6 */ |
| 208 | { |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 209 | ip6_vxlan_header_t * hdr0 = vlib_buffer_get_current(b0); |
| 210 | ip6_vxlan_header_t * rewrite0 = (void *) t0->rewrite; |
| 211 | ip6_vxlan_header_t * hdr1 = vlib_buffer_get_current(b0); |
| 212 | ip6_vxlan_header_t * rewrite1 = (void *) t1->rewrite; |
| 213 | *hdr0 = *rewrite0; |
| 214 | *hdr1 = *rewrite1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 216 | /* Fix IP6 payload length */ |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 217 | ip6_0 = &hdr0->ip6; |
| 218 | ip6_1 = &hdr1->ip6; |
| 219 | ip6_0->payload_length = payload_l0; |
| 220 | ip6_1->payload_length = payload_l1; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 221 | |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 222 | l3_0 = (u8 *)ip6_0; |
| 223 | l3_1 = (u8 *)ip6_1; |
| 224 | udp0 = &hdr0->udp; |
| 225 | udp1 = &hdr1->udp; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 226 | } |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 227 | |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 228 | /* Fix UDP length and set source port */ |
| 229 | udp0->length = payload_l0; |
| 230 | udp0->src_port = flow_hash0; |
| 231 | udp1->length = payload_l1; |
| 232 | udp1->src_port = flow_hash1; |
| 233 | |
| 234 | if (csum_offload) |
| 235 | { |
| 236 | b0->flags |= csum_flags; |
| 237 | vnet_buffer (b0)->l3_hdr_offset = l3_0 - b0->data; |
| 238 | vnet_buffer (b0)->l4_hdr_offset = (u8 *) udp0 - b0->data; |
| 239 | b1->flags |= csum_flags; |
| 240 | vnet_buffer (b1)->l3_hdr_offset = l3_1 - b1->data; |
| 241 | vnet_buffer (b1)->l4_hdr_offset = (u8 *) udp1 - b1->data; |
| 242 | } |
| 243 | /* IPv4 UDP checksum only if checksum offload is used */ |
| 244 | else if (is_ip4) |
| 245 | { |
| 246 | ip_csum_t sum0 = ip4_0->checksum; |
| 247 | sum0 = ip_csum_update (sum0, 0, ip4_0->length, ip4_header_t, |
| 248 | length /* changed member */); |
| 249 | ip4_0->checksum = ip_csum_fold (sum0); |
| 250 | ip_csum_t sum1 = ip4_1->checksum; |
| 251 | sum1 = ip_csum_update (sum1, 0, ip4_1->length, ip4_header_t, |
| 252 | length /* changed member */); |
| 253 | ip4_1->checksum = ip_csum_fold (sum1); |
| 254 | } |
| 255 | /* IPv6 UDP checksum is mandatory */ |
| 256 | else |
| 257 | { |
| 258 | int bogus = 0; |
| 259 | |
| 260 | udp0->checksum = ip6_tcp_udp_icmp_compute_checksum |
| 261 | (vm, b0, ip6_0, &bogus); |
| 262 | ASSERT(bogus == 0); |
| 263 | if (udp0->checksum == 0) |
| 264 | udp0->checksum = 0xffff; |
| 265 | udp1->checksum = ip6_tcp_udp_icmp_compute_checksum |
| 266 | (vm, b1, ip6_1, &bogus); |
| 267 | ASSERT(bogus == 0); |
| 268 | if (udp1->checksum == 0) |
| 269 | udp1->checksum = 0xffff; |
| 270 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 272 | vlib_increment_combined_counter (tx_counter, thread_index, |
| 273 | sw_if_index0, 1, len0); |
| 274 | vlib_increment_combined_counter (tx_counter, thread_index, |
| 275 | sw_if_index1, 1, len1); |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 276 | pkts_encapsulated += 2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | |
| 278 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 279 | { |
| 280 | vxlan_encap_trace_t *tr = |
| 281 | vlib_add_trace (vm, node, b0, sizeof (*tr)); |
| 282 | tr->tunnel_index = t0 - vxm->tunnels; |
| 283 | tr->vni = t0->vni; |
| 284 | } |
| 285 | |
| 286 | if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) |
| 287 | { |
| 288 | vxlan_encap_trace_t *tr = |
| 289 | vlib_add_trace (vm, node, b1, sizeof (*tr)); |
| 290 | tr->tunnel_index = t1 - vxm->tunnels; |
| 291 | tr->vni = t1->vni; |
| 292 | } |
| 293 | |
| 294 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 295 | to_next, n_left_to_next, |
| 296 | bi0, bi1, next0, next1); |
| 297 | } |
| 298 | |
| 299 | while (n_left_from > 0 && n_left_to_next > 0) |
| 300 | { |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 301 | u32 bi0 = from[0]; |
| 302 | vlib_buffer_t * b0 = vlib_get_buffer (vm, bi0); |
| 303 | u32 flow_hash0 = vnet_l2_compute_flow_hash(b0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 304 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | to_next[0] = bi0; |
| 306 | from += 1; |
| 307 | to_next += 1; |
| 308 | n_left_from -= 1; |
| 309 | n_left_to_next -= 1; |
| 310 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 311 | /* Get next node index and adj index from tunnel next_dpo */ |
| 312 | if (sw_if_index0 != vnet_buffer(b0)->sw_if_index[VLIB_TX]) |
| 313 | { |
| 314 | sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX]; |
John Lo | 25d417f | 2018-02-15 15:47:53 -0500 | [diff] [blame] | 315 | vnet_hw_interface_t *hi0 = |
| 316 | vnet_get_sup_hw_interface (vnm, sw_if_index0); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 317 | t0 = &vxm->tunnels[hi0->dev_instance]; |
| 318 | /* Note: change to always set next0 if it may be set to drop */ |
| 319 | next0 = t0->next_dpo.dpoi_next_node; |
John Lo | 25d417f | 2018-02-15 15:47:53 -0500 | [diff] [blame] | 320 | dpoi_idx0 = t0->next_dpo.dpoi_index; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 321 | } |
John Lo | 25d417f | 2018-02-15 15:47:53 -0500 | [diff] [blame] | 322 | vnet_buffer(b0)->ip.adj_index[VLIB_TX] = dpoi_idx0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 324 | ASSERT(vec_len(t0->rewrite) == underlay_hdr_len); |
| 325 | vlib_buffer_advance (b0, -underlay_hdr_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 326 | |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 327 | u32 len0 = vlib_buffer_length_in_chain (vm, b0); |
| 328 | u16 payload_l0 = clib_host_to_net_u16 (len0 - l3_len); |
| 329 | |
| 330 | udp_header_t * udp0; |
| 331 | ip4_header_t * ip4_0; |
| 332 | ip6_header_t * ip6_0; |
| 333 | u8 * l3_0; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 334 | if (is_ip4) |
| 335 | { |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 336 | ip4_vxlan_header_t * rewrite = (void *)t0->rewrite; |
| 337 | ip4_vxlan_header_t * hdr = vlib_buffer_get_current(b0); |
| 338 | *hdr = *rewrite; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 340 | /* Fix the IP4 checksum and length */ |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 341 | ip4_0 = &hdr->ip4; |
| 342 | ip4_0->length = clib_host_to_net_u16 (len0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 343 | |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 344 | l3_0 = (u8*)ip4_0; |
| 345 | udp0 = &hdr->udp; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 346 | } |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 347 | else /* ip6 path */ |
| 348 | { |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 349 | ip6_vxlan_header_t * hdr = vlib_buffer_get_current(b0); |
| 350 | ip6_vxlan_header_t * rewrite = (void *) t0->rewrite; |
| 351 | *hdr = *rewrite; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 352 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 353 | /* Fix IP6 payload length */ |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 354 | ip6_0 = &hdr->ip6; |
| 355 | ip6_0->payload_length = payload_l0; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 356 | |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 357 | l3_0 = (u8 *)ip6_0; |
| 358 | udp0 = &hdr->udp; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 359 | } |
| 360 | |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 361 | /* Fix UDP length and set source port */ |
| 362 | udp0->length = payload_l0; |
| 363 | udp0->src_port = flow_hash0; |
| 364 | |
| 365 | if (csum_offload) |
| 366 | { |
| 367 | b0->flags |= csum_flags; |
| 368 | vnet_buffer (b0)->l3_hdr_offset = l3_0 - b0->data; |
| 369 | vnet_buffer (b0)->l4_hdr_offset = (u8 *) udp0 - b0->data; |
| 370 | } |
| 371 | /* IPv4 UDP checksum only if checksum offload is used */ |
| 372 | else if (is_ip4) |
| 373 | { |
| 374 | ip_csum_t sum0 = ip4_0->checksum; |
| 375 | sum0 = ip_csum_update (sum0, 0, ip4_0->length, ip4_header_t, |
| 376 | length /* changed member */); |
| 377 | ip4_0->checksum = ip_csum_fold (sum0); |
| 378 | } |
| 379 | /* IPv6 UDP checksum is mandatory */ |
| 380 | else |
| 381 | { |
| 382 | int bogus = 0; |
| 383 | |
| 384 | udp0->checksum = ip6_tcp_udp_icmp_compute_checksum |
| 385 | (vm, b0, ip6_0, &bogus); |
| 386 | ASSERT(bogus == 0); |
| 387 | if (udp0->checksum == 0) |
| 388 | udp0->checksum = 0xffff; |
| 389 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 391 | vlib_increment_combined_counter (tx_counter, thread_index, |
| 392 | sw_if_index0, 1, len0); |
Eyal Bari | 3ce0b08 | 2018-01-17 12:06:32 +0200 | [diff] [blame] | 393 | pkts_encapsulated ++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | |
| 395 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 396 | { |
| 397 | vxlan_encap_trace_t *tr = |
| 398 | vlib_add_trace (vm, node, b0, sizeof (*tr)); |
| 399 | tr->tunnel_index = t0 - vxm->tunnels; |
| 400 | tr->vni = t0->vni; |
| 401 | } |
| 402 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 403 | to_next, n_left_to_next, |
| 404 | bi0, next0); |
| 405 | } |
| 406 | |
| 407 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 408 | } |
| 409 | |
| 410 | /* Do we still need this now that tunnel tx stats is kept? */ |
| 411 | vlib_node_increment_counter (vm, node->node_index, |
| 412 | VXLAN_ENCAP_ERROR_ENCAPSULATED, |
| 413 | pkts_encapsulated); |
| 414 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | return from_frame->n_vectors; |
| 416 | } |
| 417 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 418 | static uword |
| 419 | vxlan4_encap (vlib_main_t * vm, |
| 420 | vlib_node_runtime_t * node, |
| 421 | vlib_frame_t * from_frame) |
| 422 | { |
John Lo | f4215a6 | 2017-09-18 00:20:05 -0400 | [diff] [blame] | 423 | /* Disable chksum offload as setup overhead in tx node is not worthwhile |
| 424 | for ip4 header checksum only, unless udp checksum is also required */ |
| 425 | return vxlan_encap_inline (vm, node, from_frame, /* is_ip4 */ 1, |
| 426 | /* csum_offload */ 0); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static uword |
| 430 | vxlan6_encap (vlib_main_t * vm, |
| 431 | vlib_node_runtime_t * node, |
| 432 | vlib_frame_t * from_frame) |
| 433 | { |
John Lo | f4215a6 | 2017-09-18 00:20:05 -0400 | [diff] [blame] | 434 | /* Enable checksum offload for ip6 as udp checksum is mandatory, */ |
| 435 | return vxlan_encap_inline (vm, node, from_frame, /* is_ip4 */ 0, |
| 436 | /* csum_offload */ 1); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | VLIB_REGISTER_NODE (vxlan4_encap_node) = { |
| 440 | .function = vxlan4_encap, |
| 441 | .name = "vxlan4-encap", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 442 | .vector_size = sizeof (u32), |
| 443 | .format_trace = format_vxlan_encap_trace, |
| 444 | .type = VLIB_NODE_TYPE_INTERNAL, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 445 | .n_errors = ARRAY_LEN(vxlan_encap_error_strings), |
| 446 | .error_strings = vxlan_encap_error_strings, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 447 | .n_next_nodes = VXLAN_ENCAP_N_NEXT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | .next_nodes = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | [VXLAN_ENCAP_NEXT_DROP] = "error-drop", |
| 450 | }, |
| 451 | }; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 452 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 453 | VLIB_NODE_FUNCTION_MULTIARCH (vxlan4_encap_node, vxlan4_encap) |
| 454 | |
| 455 | VLIB_REGISTER_NODE (vxlan6_encap_node) = { |
| 456 | .function = vxlan6_encap, |
| 457 | .name = "vxlan6-encap", |
| 458 | .vector_size = sizeof (u32), |
| 459 | .format_trace = format_vxlan_encap_trace, |
| 460 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 461 | .n_errors = ARRAY_LEN(vxlan_encap_error_strings), |
| 462 | .error_strings = vxlan_encap_error_strings, |
| 463 | .n_next_nodes = VXLAN_ENCAP_N_NEXT, |
| 464 | .next_nodes = { |
| 465 | [VXLAN_ENCAP_NEXT_DROP] = "error-drop", |
| 466 | }, |
| 467 | }; |
| 468 | |
| 469 | VLIB_NODE_FUNCTION_MULTIARCH (vxlan6_encap_node, vxlan6_encap) |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 470 | |