blob: 787758a94f8466fc15ae0164c06a1abe87d4767b [file] [log] [blame]
Eyal Bari3ce0b082018-01-17 12:06:32 +02001
Ed Warnickecb9cada2015-12-08 15:45:58 -07002/*
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>
Mohsin Kazmidbd5fb32020-06-02 15:21:03 +020021#include <vnet/interface_output.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022#include <vnet/vxlan/vxlan.h>
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -070023#include <vnet/qos/qos_types.h>
eyal bari82e21d72018-04-26 13:14:55 +030024#include <vnet/adj/rewrite.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025
26/* Statistics (not all errors) */
27#define foreach_vxlan_encap_error \
John Lo3ef822e2016-06-07 09:14:07 -040028_(ENCAPSULATED, "good packets encapsulated")
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020030static char *vxlan_encap_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070031#define _(sym,string) string,
32 foreach_vxlan_encap_error
33#undef _
34};
35
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020036typedef enum
37{
Ed Warnickecb9cada2015-12-08 15:45:58 -070038#define _(sym,str) VXLAN_ENCAP_ERROR_##sym,
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020039 foreach_vxlan_encap_error
Ed Warnickecb9cada2015-12-08 15:45:58 -070040#undef _
41 VXLAN_ENCAP_N_ERROR,
42} vxlan_encap_error_t;
43
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020044typedef enum
45{
46 VXLAN_ENCAP_NEXT_DROP,
47 VXLAN_ENCAP_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070048} vxlan_encap_next_t;
49
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020050typedef struct
51{
Ed Warnickecb9cada2015-12-08 15:45:58 -070052 u32 tunnel_index;
53 u32 vni;
54} vxlan_encap_trace_t;
55
Filip Tehlare1714d32019-03-05 03:01:43 -080056#ifndef CLIB_MARCH_VARIANT
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020057u8 *
58format_vxlan_encap_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
60 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
61 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020062 vxlan_encap_trace_t *t = va_arg (*args, vxlan_encap_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020064 s = format (s, "VXLAN encap to vxlan_tunnel%d vni %d",
John Loc42912d2016-11-07 18:30:47 -050065 t->tunnel_index, t->vni);
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 return s;
67}
Filip Tehlare1714d32019-03-05 03:01:43 -080068#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -070069
John Loc42912d2016-11-07 18:30:47 -050070always_inline uword
71vxlan_encap_inline (vlib_main_t * vm,
72 vlib_node_runtime_t * node,
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020073 vlib_frame_t * from_frame, u8 is_ip4, u8 csum_offload)
Ed Warnickecb9cada2015-12-08 15:45:58 -070074{
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020075 u32 n_left_from, next_index, *from, *to_next;
76 vxlan_main_t *vxm = &vxlan_main;
77 vnet_main_t *vnm = vxm->vnet_main;
78 vnet_interface_main_t *im = &vnm->interface_main;
79 vlib_combined_counter_main_t *tx_counter =
80 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 u32 pkts_encapsulated = 0;
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020082 u32 thread_index = vlib_get_thread_index ();
John Loc42912d2016-11-07 18:30:47 -050083 u32 sw_if_index0 = 0, sw_if_index1 = 0;
84 u32 next0 = 0, next1 = 0;
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020085 vxlan_tunnel_t *t0 = NULL, *t1 = NULL;
John Lo25d417f2018-02-15 15:47:53 -050086 index_t dpoi_idx0 = INDEX_INVALID, dpoi_idx1 = INDEX_INVALID;
Zhiyong Yang102dd1c2020-03-27 13:04:38 +000087 vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
88 vlib_buffer_t **b = bufs;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
90 from = vlib_frame_vector_args (from_frame);
91 n_left_from = from_frame->n_vectors;
92
93 next_index = node->cached_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020095 STATIC_ASSERT_SIZEOF (ip6_vxlan_header_t, 56);
96 STATIC_ASSERT_SIZEOF (ip4_vxlan_header_t, 36);
Eyal Bari3ce0b082018-01-17 12:06:32 +020097
eyal bari82e21d72018-04-26 13:14:55 +030098 u8 const underlay_hdr_len = is_ip4 ?
Mohsin Kazmi21a1de42020-06-02 15:12:30 +020099 sizeof (ip4_vxlan_header_t) : sizeof (ip6_vxlan_header_t);
100 u16 const l3_len = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
John Lob1b98f52019-08-03 18:25:49 -0400101 u32 const csum_flags = is_ip4 ? VNET_BUFFER_F_OFFLOAD_IP_CKSUM |
Mohsin Kazmidbd5fb32020-06-02 15:21:03 +0200102 VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
103 VNET_BUFFER_F_L3_HDR_OFFSET_VALID | VNET_BUFFER_F_L4_HDR_OFFSET_VALID :
104 VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
105 VNET_BUFFER_F_L3_HDR_OFFSET_VALID | VNET_BUFFER_F_L4_HDR_OFFSET_VALID;
106 u32 const inner_packet_csum_offload_flags =
107 VNET_BUFFER_F_OFFLOAD_IP_CKSUM | VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
108 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
109 u32 const inner_packet_removed_flags =
110 VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_IS_IP6 |
111 VNET_BUFFER_F_L2_HDR_OFFSET_VALID | VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
112 VNET_BUFFER_F_L4_HDR_OFFSET_VALID;
113
Zhiyong Yang102dd1c2020-03-27 13:04:38 +0000114 vlib_get_buffers (vm, from, bufs, n_left_from);
Eyal Bari3ce0b082018-01-17 12:06:32 +0200115
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 while (n_left_from > 0)
117 {
118 u32 n_left_to_next;
119
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200120 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
122 while (n_left_from >= 4 && n_left_to_next >= 2)
123 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 /* Prefetch next iteration. */
125 {
Zhiyong Yang102dd1c2020-03-27 13:04:38 +0000126 vlib_prefetch_buffer_header (b[2], LOAD);
127 vlib_prefetch_buffer_header (b[3], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200129 CLIB_PREFETCH (b[2]->data - CLIB_CACHE_LINE_BYTES,
130 2 * CLIB_CACHE_LINE_BYTES, LOAD);
131 CLIB_PREFETCH (b[3]->data - CLIB_CACHE_LINE_BYTES,
132 2 * CLIB_CACHE_LINE_BYTES, LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 }
134
eyal bari82e21d72018-04-26 13:14:55 +0300135 u32 bi0 = to_next[0] = from[0];
136 u32 bi1 = to_next[1] = from[1];
137 from += 2;
138 to_next += 2;
139 n_left_to_next -= 2;
140 n_left_from -= 2;
Eyal Bari3ce0b082018-01-17 12:06:32 +0200141
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200142 vlib_buffer_t *b0 = b[0];
143 vlib_buffer_t *b1 = b[1];
Zhiyong Yang102dd1c2020-03-27 13:04:38 +0000144 b += 2;
145
Mohsin Kazmidbd5fb32020-06-02 15:21:03 +0200146 u32 or_flags = b0->flags | b1->flags;
147 if (csum_offload && (or_flags & inner_packet_csum_offload_flags))
148 {
149 /* Only calculate the non-GSO packet csum offload */
150 if ((b0->flags & VNET_BUFFER_F_GSO) == 0)
151 {
152 vnet_calc_checksums_inline (vm, b0,
153 b0->flags &
154 VNET_BUFFER_F_IS_IP4,
155 b0->flags &
156 VNET_BUFFER_F_IS_IP6);
157 b0->flags &= ~inner_packet_removed_flags;
158 }
159 if ((b1->flags & VNET_BUFFER_F_GSO) == 0)
160 {
161 vnet_calc_checksums_inline (vm, b1,
162 b1->flags &
163 VNET_BUFFER_F_IS_IP4,
164 b1->flags &
165 VNET_BUFFER_F_IS_IP6);
166 b1->flags &= ~inner_packet_removed_flags;
167 }
168 }
169
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200170 u32 flow_hash0 = vnet_l2_compute_flow_hash (b0);
171 u32 flow_hash1 = vnet_l2_compute_flow_hash (b1);
Eyal Bari3ce0b082018-01-17 12:06:32 +0200172
John Loc42912d2016-11-07 18:30:47 -0500173 /* Get next node index and adj index from tunnel next_dpo */
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200174 if (sw_if_index0 != vnet_buffer (b0)->sw_if_index[VLIB_TX])
John Loc42912d2016-11-07 18:30:47 -0500175 {
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200176 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
177 vnet_hw_interface_t *hi0 =
178 vnet_get_sup_hw_interface (vnm, sw_if_index0);
John Loc42912d2016-11-07 18:30:47 -0500179 t0 = &vxm->tunnels[hi0->dev_instance];
John Lo25d417f2018-02-15 15:47:53 -0500180 /* Note: change to always set next0 if it may set to drop */
John Loc42912d2016-11-07 18:30:47 -0500181 next0 = t0->next_dpo.dpoi_next_node;
John Lo25d417f2018-02-15 15:47:53 -0500182 dpoi_idx0 = t0->next_dpo.dpoi_index;
John Loc42912d2016-11-07 18:30:47 -0500183 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184
John Loc42912d2016-11-07 18:30:47 -0500185 /* Get next node index and adj index from tunnel next_dpo */
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200186 if (sw_if_index1 != vnet_buffer (b1)->sw_if_index[VLIB_TX])
John Loc42912d2016-11-07 18:30:47 -0500187 {
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200188 if (sw_if_index0 == vnet_buffer (b1)->sw_if_index[VLIB_TX])
189 {
John Lo25d417f2018-02-15 15:47:53 -0500190 sw_if_index1 = sw_if_index0;
191 t1 = t0;
192 next1 = next0;
193 dpoi_idx1 = dpoi_idx0;
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200194 }
John Lo25d417f2018-02-15 15:47:53 -0500195 else
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200196 {
197 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
198 vnet_hw_interface_t *hi1 =
199 vnet_get_sup_hw_interface (vnm, sw_if_index1);
John Lo25d417f2018-02-15 15:47:53 -0500200 t1 = &vxm->tunnels[hi1->dev_instance];
201 /* Note: change to always set next1 if it may set to drop */
202 next1 = t1->next_dpo.dpoi_next_node;
203 dpoi_idx1 = t1->next_dpo.dpoi_index;
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200204 }
John Loc42912d2016-11-07 18:30:47 -0500205 }
John Lo25d417f2018-02-15 15:47:53 -0500206
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200207 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpoi_idx0;
208 vnet_buffer (b1)->ip.adj_index[VLIB_TX] = dpoi_idx1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200210 ASSERT (t0->rewrite_header.data_bytes == underlay_hdr_len);
211 ASSERT (t1->rewrite_header.data_bytes == underlay_hdr_len);
212 vnet_rewrite_two_headers (*t0, *t1, vlib_buffer_get_current (b0),
213 vlib_buffer_get_current (b1),
214 underlay_hdr_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200216 vlib_buffer_advance (b0, -underlay_hdr_len);
217 vlib_buffer_advance (b1, -underlay_hdr_len);
Eyal Bari3ce0b082018-01-17 12:06:32 +0200218
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200219 u32 len0 = vlib_buffer_length_in_chain (vm, b0);
220 u32 len1 = vlib_buffer_length_in_chain (vm, b1);
221 u16 payload_l0 = clib_host_to_net_u16 (len0 - l3_len);
222 u16 payload_l1 = clib_host_to_net_u16 (len1 - l3_len);
Eyal Bari3ce0b082018-01-17 12:06:32 +0200223
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200224 void *underlay0 = vlib_buffer_get_current (b0);
225 void *underlay1 = vlib_buffer_get_current (b1);
eyal bari82e21d72018-04-26 13:14:55 +0300226
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200227 ip4_header_t *ip4_0, *ip4_1;
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700228 qos_bits_t ip4_0_tos = 0, ip4_1_tos = 0;
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200229 ip6_header_t *ip6_0, *ip6_1;
230 udp_header_t *udp0, *udp1;
231 u8 *l3_0, *l3_1;
John Loc42912d2016-11-07 18:30:47 -0500232 if (is_ip4)
233 {
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200234 ip4_vxlan_header_t *hdr0 = underlay0;
235 ip4_vxlan_header_t *hdr1 = underlay1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
John Loc42912d2016-11-07 18:30:47 -0500237 /* Fix the IP4 checksum and length */
Eyal Bari3ce0b082018-01-17 12:06:32 +0200238 ip4_0 = &hdr0->ip4;
239 ip4_1 = &hdr1->ip4;
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200240 ip4_0->length = clib_host_to_net_u16 (len0);
241 ip4_1->length = clib_host_to_net_u16 (len1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700243 if (PREDICT_FALSE (b0->flags & VNET_BUFFER_F_QOS_DATA_VALID))
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200244 {
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700245 ip4_0_tos = vnet_buffer2 (b0)->qos.bits;
246 ip4_0->tos = ip4_0_tos;
247 }
248 if (PREDICT_FALSE (b1->flags & VNET_BUFFER_F_QOS_DATA_VALID))
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200249 {
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700250 ip4_1_tos = vnet_buffer2 (b1)->qos.bits;
251 ip4_1->tos = ip4_1_tos;
252 }
253
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200254 l3_0 = (u8 *) ip4_0;
255 l3_1 = (u8 *) ip4_1;
Eyal Bari3ce0b082018-01-17 12:06:32 +0200256 udp0 = &hdr0->udp;
257 udp1 = &hdr1->udp;
John Loc42912d2016-11-07 18:30:47 -0500258 }
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200259 else /* ipv6 */
John Loc42912d2016-11-07 18:30:47 -0500260 {
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200261 ip6_vxlan_header_t *hdr0 = underlay0;
262 ip6_vxlan_header_t *hdr1 = underlay1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263
John Loc42912d2016-11-07 18:30:47 -0500264 /* Fix IP6 payload length */
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200265 ip6_0 = &hdr0->ip6;
266 ip6_1 = &hdr1->ip6;
Eyal Bari3ce0b082018-01-17 12:06:32 +0200267 ip6_0->payload_length = payload_l0;
268 ip6_1->payload_length = payload_l1;
John Loc42912d2016-11-07 18:30:47 -0500269
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200270 l3_0 = (u8 *) ip6_0;
271 l3_1 = (u8 *) ip6_1;
272 udp0 = &hdr0->udp;
273 udp1 = &hdr1->udp;
John Loc42912d2016-11-07 18:30:47 -0500274 }
Chris Luke99cb3352016-04-26 10:49:53 -0400275
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200276 /* Fix UDP length and set source port */
277 udp0->length = payload_l0;
278 udp0->src_port = flow_hash0;
279 udp1->length = payload_l1;
280 udp1->src_port = flow_hash1;
Eyal Bari3ce0b082018-01-17 12:06:32 +0200281
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200282 if (csum_offload)
283 {
284 b0->flags |= csum_flags;
285 vnet_buffer (b0)->l3_hdr_offset = l3_0 - b0->data;
286 vnet_buffer (b0)->l4_hdr_offset = (u8 *) udp0 - b0->data;
287 b1->flags |= csum_flags;
288 vnet_buffer (b1)->l3_hdr_offset = l3_1 - b1->data;
289 vnet_buffer (b1)->l4_hdr_offset = (u8 *) udp1 - b1->data;
290 }
291 /* IPv4 UDP checksum only if checksum offload is used */
292 else if (is_ip4)
293 {
294 ip_csum_t sum0 = ip4_0->checksum;
295 sum0 = ip_csum_update (sum0, 0, ip4_0->length, ip4_header_t,
296 length /* changed member */ );
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700297 if (PREDICT_FALSE (ip4_0_tos))
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200298 {
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700299 sum0 = ip_csum_update (sum0, 0, ip4_0_tos, ip4_header_t,
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200300 tos /* changed member */ );
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700301 }
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200302 ip4_0->checksum = ip_csum_fold (sum0);
303 ip_csum_t sum1 = ip4_1->checksum;
304 sum1 = ip_csum_update (sum1, 0, ip4_1->length, ip4_header_t,
305 length /* changed member */ );
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700306 if (PREDICT_FALSE (ip4_1_tos))
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200307 {
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700308 sum1 = ip_csum_update (sum1, 0, ip4_1_tos, ip4_header_t,
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200309 tos /* changed member */ );
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700310 }
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200311 ip4_1->checksum = ip_csum_fold (sum1);
312 }
313 /* IPv6 UDP checksum is mandatory */
314 else
315 {
316 int bogus = 0;
Eyal Bari3ce0b082018-01-17 12:06:32 +0200317
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200318 udp0->checksum = ip6_tcp_udp_icmp_compute_checksum
319 (vm, b0, ip6_0, &bogus);
320 ASSERT (bogus == 0);
321 if (udp0->checksum == 0)
322 udp0->checksum = 0xffff;
323 udp1->checksum = ip6_tcp_udp_icmp_compute_checksum
324 (vm, b1, ip6_1, &bogus);
325 ASSERT (bogus == 0);
326 if (udp1->checksum == 0)
327 udp1->checksum = 0xffff;
328 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200330 /* save inner packet flow_hash for load-balance node */
331 vnet_buffer (b0)->ip.flow_hash = flow_hash0;
332 vnet_buffer (b1)->ip.flow_hash = flow_hash1;
Shawn Ji623b4f82019-12-18 10:10:54 +0800333
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200334 if (sw_if_index0 == sw_if_index1)
335 {
336 vlib_increment_combined_counter (tx_counter, thread_index,
337 sw_if_index0, 2, len0 + len1);
338 }
339 else
340 {
341 vlib_increment_combined_counter (tx_counter, thread_index,
342 sw_if_index0, 1, len0);
343 vlib_increment_combined_counter (tx_counter, thread_index,
344 sw_if_index1, 1, len1);
345 }
346 pkts_encapsulated += 2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200348 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
349 {
350 vxlan_encap_trace_t *tr =
351 vlib_add_trace (vm, node, b0, sizeof (*tr));
352 tr->tunnel_index = t0 - vxm->tunnels;
353 tr->vni = t0->vni;
354 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200356 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
357 {
358 vxlan_encap_trace_t *tr =
359 vlib_add_trace (vm, node, b1, sizeof (*tr));
360 tr->tunnel_index = t1 - vxm->tunnels;
361 tr->vni = t1->vni;
362 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363
364 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
365 to_next, n_left_to_next,
366 bi0, bi1, next0, next1);
367 }
368
369 while (n_left_from > 0 && n_left_to_next > 0)
370 {
eyal bari82e21d72018-04-26 13:14:55 +0300371 u32 bi0 = to_next[0] = from[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 from += 1;
373 to_next += 1;
374 n_left_from -= 1;
375 n_left_to_next -= 1;
376
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200377 vlib_buffer_t *b0 = b[0];
Zhiyong Yang102dd1c2020-03-27 13:04:38 +0000378 b += 1;
379
Mohsin Kazmidbd5fb32020-06-02 15:21:03 +0200380 if (csum_offload && (b0->flags & inner_packet_csum_offload_flags))
381 {
382 /* Only calculate the non-GSO packet csum offload */
383 if ((b0->flags & VNET_BUFFER_F_GSO) == 0)
384 {
385 vnet_calc_checksums_inline (vm, b0,
386 b0->flags &
387 VNET_BUFFER_F_IS_IP4,
388 b0->flags &
389 VNET_BUFFER_F_IS_IP6);
390 b0->flags &= ~inner_packet_removed_flags;
391 }
392 }
393
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200394 u32 flow_hash0 = vnet_l2_compute_flow_hash (b0);
eyal bari82e21d72018-04-26 13:14:55 +0300395
John Loc42912d2016-11-07 18:30:47 -0500396 /* Get next node index and adj index from tunnel next_dpo */
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200397 if (sw_if_index0 != vnet_buffer (b0)->sw_if_index[VLIB_TX])
John Loc42912d2016-11-07 18:30:47 -0500398 {
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200399 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
400 vnet_hw_interface_t *hi0 =
401 vnet_get_sup_hw_interface (vnm, sw_if_index0);
John Loc42912d2016-11-07 18:30:47 -0500402 t0 = &vxm->tunnels[hi0->dev_instance];
403 /* Note: change to always set next0 if it may be set to drop */
404 next0 = t0->next_dpo.dpoi_next_node;
John Lo25d417f2018-02-15 15:47:53 -0500405 dpoi_idx0 = t0->next_dpo.dpoi_index;
John Loc42912d2016-11-07 18:30:47 -0500406 }
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200407 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpoi_idx0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200409 ASSERT (t0->rewrite_header.data_bytes == underlay_hdr_len);
410 vnet_rewrite_one_header (*t0, vlib_buffer_get_current (b0),
411 underlay_hdr_len);
eyal bari82e21d72018-04-26 13:14:55 +0300412
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200413 vlib_buffer_advance (b0, -underlay_hdr_len);
414 void *underlay0 = vlib_buffer_get_current (b0);
eyal bari82e21d72018-04-26 13:14:55 +0300415
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200416 u32 len0 = vlib_buffer_length_in_chain (vm, b0);
417 u16 payload_l0 = clib_host_to_net_u16 (len0 - l3_len);
Eyal Bari3ce0b082018-01-17 12:06:32 +0200418
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200419 udp_header_t *udp0;
420 ip4_header_t *ip4_0;
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700421 qos_bits_t ip4_0_tos = 0;
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200422 ip6_header_t *ip6_0;
423 u8 *l3_0;
John Loc42912d2016-11-07 18:30:47 -0500424 if (is_ip4)
425 {
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200426 ip4_vxlan_header_t *hdr = underlay0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
John Loc42912d2016-11-07 18:30:47 -0500428 /* Fix the IP4 checksum and length */
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200429 ip4_0 = &hdr->ip4;
430 ip4_0->length = clib_host_to_net_u16 (len0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700432 if (PREDICT_FALSE (b0->flags & VNET_BUFFER_F_QOS_DATA_VALID))
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200433 {
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700434 ip4_0_tos = vnet_buffer2 (b0)->qos.bits;
435 ip4_0->tos = ip4_0_tos;
436 }
437
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200438 l3_0 = (u8 *) ip4_0;
Eyal Bari3ce0b082018-01-17 12:06:32 +0200439 udp0 = &hdr->udp;
John Loc42912d2016-11-07 18:30:47 -0500440 }
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200441 else /* ip6 path */
John Loc42912d2016-11-07 18:30:47 -0500442 {
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200443 ip6_vxlan_header_t *hdr = underlay0;
Chris Luke99cb3352016-04-26 10:49:53 -0400444
John Loc42912d2016-11-07 18:30:47 -0500445 /* Fix IP6 payload length */
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200446 ip6_0 = &hdr->ip6;
Eyal Bari3ce0b082018-01-17 12:06:32 +0200447 ip6_0->payload_length = payload_l0;
Chris Luke99cb3352016-04-26 10:49:53 -0400448
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200449 l3_0 = (u8 *) ip6_0;
450 udp0 = &hdr->udp;
John Loc42912d2016-11-07 18:30:47 -0500451 }
452
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200453 /* Fix UDP length and set source port */
454 udp0->length = payload_l0;
455 udp0->src_port = flow_hash0;
Eyal Bari3ce0b082018-01-17 12:06:32 +0200456
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200457 if (csum_offload)
458 {
459 b0->flags |= csum_flags;
460 vnet_buffer (b0)->l3_hdr_offset = l3_0 - b0->data;
461 vnet_buffer (b0)->l4_hdr_offset = (u8 *) udp0 - b0->data;
462 }
463 /* IPv4 UDP checksum only if checksum offload is used */
464 else if (is_ip4)
465 {
466 ip_csum_t sum0 = ip4_0->checksum;
467 sum0 = ip_csum_update (sum0, 0, ip4_0->length, ip4_header_t,
468 length /* changed member */ );
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700469 if (PREDICT_FALSE (ip4_0_tos))
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200470 {
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700471 sum0 = ip_csum_update (sum0, 0, ip4_0_tos, ip4_header_t,
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200472 tos /* changed member */ );
Igor Mikhailov (imichail)66364402018-04-24 21:53:00 -0700473 }
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200474 ip4_0->checksum = ip_csum_fold (sum0);
475 }
476 /* IPv6 UDP checksum is mandatory */
477 else
478 {
479 int bogus = 0;
Eyal Bari3ce0b082018-01-17 12:06:32 +0200480
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200481 udp0->checksum = ip6_tcp_udp_icmp_compute_checksum
482 (vm, b0, ip6_0, &bogus);
483 ASSERT (bogus == 0);
484 if (udp0->checksum == 0)
485 udp0->checksum = 0xffff;
486 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200488 /* reuse inner packet flow_hash for load-balance node */
489 vnet_buffer (b0)->ip.flow_hash = flow_hash0;
Shawn Ji623b4f82019-12-18 10:10:54 +0800490
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200491 vlib_increment_combined_counter (tx_counter, thread_index,
492 sw_if_index0, 1, len0);
493 pkts_encapsulated++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200495 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
496 {
497 vxlan_encap_trace_t *tr =
498 vlib_add_trace (vm, node, b0, sizeof (*tr));
499 tr->tunnel_index = t0 - vxm->tunnels;
500 tr->vni = t0->vni;
501 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
503 to_next, n_left_to_next,
504 bi0, next0);
505 }
506
507 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
508 }
509
510 /* Do we still need this now that tunnel tx stats is kept? */
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200511 vlib_node_increment_counter (vm, node->node_index,
512 VXLAN_ENCAP_ERROR_ENCAPSULATED,
513 pkts_encapsulated);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 return from_frame->n_vectors;
516}
517
Filip Tehlare1714d32019-03-05 03:01:43 -0800518VLIB_NODE_FN (vxlan4_encap_node) (vlib_main_t * vm,
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200519 vlib_node_runtime_t * node,
520 vlib_frame_t * from_frame)
John Loc42912d2016-11-07 18:30:47 -0500521{
John Lof4215a62017-09-18 00:20:05 -0400522 /* Disable chksum offload as setup overhead in tx node is not worthwhile
523 for ip4 header checksum only, unless udp checksum is also required */
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200524 return vxlan_encap_inline (vm, node, from_frame, /* is_ip4 */ 1,
John Lof4215a62017-09-18 00:20:05 -0400525 /* csum_offload */ 0);
John Loc42912d2016-11-07 18:30:47 -0500526}
527
Filip Tehlare1714d32019-03-05 03:01:43 -0800528VLIB_NODE_FN (vxlan6_encap_node) (vlib_main_t * vm,
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200529 vlib_node_runtime_t * node,
530 vlib_frame_t * from_frame)
John Loc42912d2016-11-07 18:30:47 -0500531{
John Lof4215a62017-09-18 00:20:05 -0400532 /* Enable checksum offload for ip6 as udp checksum is mandatory, */
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200533 return vxlan_encap_inline (vm, node, from_frame, /* is_ip4 */ 0,
John Lof4215a62017-09-18 00:20:05 -0400534 /* csum_offload */ 1);
John Loc42912d2016-11-07 18:30:47 -0500535}
536
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200537/* *INDENT-OFF* */
John Loc42912d2016-11-07 18:30:47 -0500538VLIB_REGISTER_NODE (vxlan4_encap_node) = {
John Loc42912d2016-11-07 18:30:47 -0500539 .name = "vxlan4-encap",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540 .vector_size = sizeof (u32),
541 .format_trace = format_vxlan_encap_trace,
542 .type = VLIB_NODE_TYPE_INTERNAL,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 .n_errors = ARRAY_LEN(vxlan_encap_error_strings),
544 .error_strings = vxlan_encap_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 .n_next_nodes = VXLAN_ENCAP_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546 .next_nodes = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 [VXLAN_ENCAP_NEXT_DROP] = "error-drop",
548 },
549};
Damjan Marion1c80e832016-05-11 23:07:18 +0200550
John Loc42912d2016-11-07 18:30:47 -0500551VLIB_REGISTER_NODE (vxlan6_encap_node) = {
John Loc42912d2016-11-07 18:30:47 -0500552 .name = "vxlan6-encap",
553 .vector_size = sizeof (u32),
554 .format_trace = format_vxlan_encap_trace,
555 .type = VLIB_NODE_TYPE_INTERNAL,
556 .n_errors = ARRAY_LEN(vxlan_encap_error_strings),
557 .error_strings = vxlan_encap_error_strings,
558 .n_next_nodes = VXLAN_ENCAP_N_NEXT,
559 .next_nodes = {
560 [VXLAN_ENCAP_NEXT_DROP] = "error-drop",
561 },
562};
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200563/* *INDENT-ON* */
John Loc42912d2016-11-07 18:30:47 -0500564
Mohsin Kazmi21a1de42020-06-02 15:12:30 +0200565/*
566 * fd.io coding-style-patch-verification: ON
567 *
568 * Local Variables:
569 * eval: (c-set-style "gnu")
570 * End:
571 */