blob: 4678aa3121913585d0f50f511d5c4c7fa742c3b7 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * decap.c: vxlan tunnel decap packet processing
3 *
4 * Copyright (c) 2013 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vlib/vlib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070019#include <vnet/vxlan/vxlan.h>
Florin Corasb040f982020-10-20 14:59:43 -070020#include <vnet/udp/udp_local.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070021
Eyal Baria5679e82018-08-26 15:20:07 +030022#ifndef CLIB_MARCH_VARIANT
John Lo13e3d452016-08-09 19:20:51 -040023vlib_node_registration_t vxlan4_input_node;
24vlib_node_registration_t vxlan6_input_node;
Eyal Baria5679e82018-08-26 15:20:07 +030025#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
Eyal Bari7d92c092018-07-24 15:15:37 +030027typedef struct
28{
Ed Warnickecb9cada2015-12-08 15:45:58 -070029 u32 next_index;
30 u32 tunnel_index;
31 u32 error;
32 u32 vni;
33} vxlan_rx_trace_t;
34
Eyal Bari7d92c092018-07-24 15:15:37 +030035static u8 *
36format_vxlan_rx_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070037{
38 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
39 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Eyal Bari7d92c092018-07-24 15:15:37 +030040 vxlan_rx_trace_t *t = va_arg (*args, vxlan_rx_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070041
Eyal Bari7d92c092018-07-24 15:15:37 +030042 if (t->tunnel_index == ~0)
43 return format (s, "VXLAN decap error - tunnel for vni %d does not exist",
44 t->vni);
45 return format (s, "VXLAN decap from vxlan_tunnel%d vni %d next %d error %d",
46 t->tunnel_index, t->vni, t->next_index, t->error);
Ed Warnickecb9cada2015-12-08 15:45:58 -070047}
48
Eyal Baridd47eca2018-07-08 08:15:56 +030049typedef vxlan4_tunnel_key_t last_tunnel_cache4;
Eyal Bari0f4b1842018-04-12 12:39:51 +030050
Eyal Bariefd9cf32018-10-02 12:23:06 +030051static const vxlan_decap_info_t decap_not_found = {
52 .sw_if_index = ~0,
53 .next_index = VXLAN_INPUT_NEXT_DROP,
54 .error = VXLAN_ERROR_NO_SUCH_TUNNEL
55};
56
57static const vxlan_decap_info_t decap_bad_flags = {
58 .sw_if_index = ~0,
59 .next_index = VXLAN_INPUT_NEXT_DROP,
60 .error = VXLAN_ERROR_BAD_FLAGS
61};
62
63always_inline vxlan_decap_info_t
Eyal Bari0f4b1842018-04-12 12:39:51 +030064vxlan4_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache4 * cache,
Eyal Bari7d92c092018-07-24 15:15:37 +030065 u32 fib_index, ip4_header_t * ip4_0,
Eyal Bariefd9cf32018-10-02 12:23:06 +030066 vxlan_header_t * vxlan0, u32 * stats_sw_if_index)
Eyal Bari0f4b1842018-04-12 12:39:51 +030067{
Eyal Bariefd9cf32018-10-02 12:23:06 +030068 if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
69 return decap_bad_flags;
Eyal Bari0f4b1842018-04-12 12:39:51 +030070
Artem Glazychev839dcc02020-12-01 02:39:21 +070071 /* Make sure VXLAN tunnel exist according to packet S/D IP, UDP port, VRF,
72 * and VNI */
Eyal Bariefd9cf32018-10-02 12:23:06 +030073 u32 dst = ip4_0->dst_address.as_u32;
74 u32 src = ip4_0->src_address.as_u32;
Artem Glazychev839dcc02020-12-01 02:39:21 +070075 udp_header_t *udp = ip4_next_header (ip4_0);
Eyal Bariefd9cf32018-10-02 12:23:06 +030076 vxlan4_tunnel_key_t key4 = {
77 .key[0] = ((u64) dst << 32) | src,
Artem Glazychev839dcc02020-12-01 02:39:21 +070078 .key[1] = ((u64) udp->dst_port << 48) | ((u64) fib_index << 32) |
79 vxlan0->vni_reserved,
Eyal Bariefd9cf32018-10-02 12:23:06 +030080 };
81
82 if (PREDICT_TRUE
Eyal Bari9be7c6d2018-10-17 17:13:42 +030083 (key4.key[0] == cache->key[0] && key4.key[1] == cache->key[1]))
Eyal Bari7d92c092018-07-24 15:15:37 +030084 {
Eyal Bariefd9cf32018-10-02 12:23:06 +030085 /* cache hit */
86 vxlan_decap_info_t di = {.as_u64 = cache->value };
87 *stats_sw_if_index = di.sw_if_index;
88 return di;
89 }
Eyal Bari0f4b1842018-04-12 12:39:51 +030090
Eyal Bariefd9cf32018-10-02 12:23:06 +030091 int rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
92 if (PREDICT_TRUE (rv == 0))
93 {
Eyal Bari7d92c092018-07-24 15:15:37 +030094 *cache = key4;
Eyal Bariefd9cf32018-10-02 12:23:06 +030095 vxlan_decap_info_t di = {.as_u64 = key4.value };
96 *stats_sw_if_index = di.sw_if_index;
97 return di;
Eyal Bari7d92c092018-07-24 15:15:37 +030098 }
Eyal Bari0f4b1842018-04-12 12:39:51 +030099
Eyal Bariefd9cf32018-10-02 12:23:06 +0300100 /* try multicast */
101 if (PREDICT_TRUE (!ip4_address_is_multicast (&ip4_0->dst_address)))
102 return decap_not_found;
103
104 /* search for mcast decap info by mcast address */
105 key4.key[0] = dst;
106 rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
107 if (rv != 0)
108 return decap_not_found;
109
110 /* search for unicast tunnel using the mcast tunnel local(src) ip */
111 vxlan_decap_info_t mdi = {.as_u64 = key4.value };
112 key4.key[0] = ((u64) mdi.local_ip.as_u32 << 32) | src;
113 rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
114 if (PREDICT_FALSE (rv != 0))
115 return decap_not_found;
116
117 /* mcast traffic does not update the cache */
118 *stats_sw_if_index = mdi.sw_if_index;
119 vxlan_decap_info_t di = {.as_u64 = key4.value };
120 return di;
Eyal Bari0f4b1842018-04-12 12:39:51 +0300121}
122
Eyal Bari0fa56782018-06-04 12:25:05 +0300123typedef vxlan6_tunnel_key_t last_tunnel_cache6;
124
Eyal Bariefd9cf32018-10-02 12:23:06 +0300125always_inline vxlan_decap_info_t
Eyal Bari0f4b1842018-04-12 12:39:51 +0300126vxlan6_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache6 * cache,
Eyal Bari7d92c092018-07-24 15:15:37 +0300127 u32 fib_index, ip6_header_t * ip6_0,
Eyal Bariefd9cf32018-10-02 12:23:06 +0300128 vxlan_header_t * vxlan0, u32 * stats_sw_if_index)
Eyal Bari0f4b1842018-04-12 12:39:51 +0300129{
Eyal Bariefd9cf32018-10-02 12:23:06 +0300130 if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
131 return decap_bad_flags;
132
Artem Glazychev839dcc02020-12-01 02:39:21 +0700133 /* Make sure VXLAN tunnel exist according to packet SIP, UDP port, VRF, and
134 * VNI */
135 udp_header_t *udp = ip6_next_header (ip6_0);
Eyal Bari0fa56782018-06-04 12:25:05 +0300136 vxlan6_tunnel_key_t key6 = {
Eyal Bariefd9cf32018-10-02 12:23:06 +0300137 .key[0] = ip6_0->src_address.as_u64[0],
138 .key[1] = ip6_0->src_address.as_u64[1],
Artem Glazychev839dcc02020-12-01 02:39:21 +0700139 .key[2] = ((u64) udp->dst_port << 48) | ((u64) fib_index << 32) |
140 vxlan0->vni_reserved,
Eyal Bari0f4b1842018-04-12 12:39:51 +0300141 };
142
Eyal Bari7d92c092018-07-24 15:15:37 +0300143 if (PREDICT_FALSE
144 (clib_bihash_key_compare_24_8 (key6.key, cache->key) == 0))
145 {
146 int rv =
147 clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6);
148 if (PREDICT_FALSE (rv != 0))
Eyal Bariefd9cf32018-10-02 12:23:06 +0300149 return decap_not_found;
Eyal Bari0f4b1842018-04-12 12:39:51 +0300150
Eyal Bari7d92c092018-07-24 15:15:37 +0300151 *cache = key6;
152 }
153 vxlan_tunnel_t *t0 = pool_elt_at_index (vxm->tunnels, cache->value);
Eyal Bari0f4b1842018-04-12 12:39:51 +0300154
155 /* Validate VXLAN tunnel SIP against packet DIP */
156 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address, &t0->src.ip6)))
Eyal Bariefd9cf32018-10-02 12:23:06 +0300157 *stats_sw_if_index = t0->sw_if_index;
Eyal Bari0f4b1842018-04-12 12:39:51 +0300158 else
Eyal Bari7d92c092018-07-24 15:15:37 +0300159 {
160 /* try multicast */
161 if (PREDICT_TRUE (!ip6_address_is_multicast (&ip6_0->dst_address)))
Eyal Bariefd9cf32018-10-02 12:23:06 +0300162 return decap_not_found;
Eyal Bari0f4b1842018-04-12 12:39:51 +0300163
Eyal Bari7d92c092018-07-24 15:15:37 +0300164 /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */
165 key6.key[0] = ip6_0->dst_address.as_u64[0];
166 key6.key[1] = ip6_0->dst_address.as_u64[1];
167 int rv =
168 clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6);
169 if (PREDICT_FALSE (rv != 0))
Eyal Bariefd9cf32018-10-02 12:23:06 +0300170 return decap_not_found;
Eyal Bari0fa56782018-06-04 12:25:05 +0300171
Eyal Bariefd9cf32018-10-02 12:23:06 +0300172 vxlan_tunnel_t *mcast_t0 = pool_elt_at_index (vxm->tunnels, key6.value);
173 *stats_sw_if_index = mcast_t0->sw_if_index;
Eyal Bari7d92c092018-07-24 15:15:37 +0300174 }
Eyal Bari0f4b1842018-04-12 12:39:51 +0300175
Eyal Bariefd9cf32018-10-02 12:23:06 +0300176 vxlan_decap_info_t di = {
177 .sw_if_index = t0->sw_if_index,
178 .next_index = t0->decap_next_index,
179 };
180 return di;
Eyal Bari0f4b1842018-04-12 12:39:51 +0300181}
182
Chris Luke99cb3352016-04-26 10:49:53 -0400183always_inline uword
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184vxlan_input (vlib_main_t * vm,
Eyal Bari7d92c092018-07-24 15:15:37 +0300185 vlib_node_runtime_t * node,
186 vlib_frame_t * from_frame, u32 is_ip4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187{
Eyal Bari7d92c092018-07-24 15:15:37 +0300188 vxlan_main_t *vxm = &vxlan_main;
189 vnet_main_t *vnm = vxm->vnet_main;
190 vnet_interface_main_t *im = &vnm->interface_main;
191 vlib_combined_counter_main_t *rx_counter =
192 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX;
Eyal Baridd47eca2018-07-08 08:15:56 +0300193 last_tunnel_cache4 last4;
Eyal Bari0fa56782018-06-04 12:25:05 +0300194 last_tunnel_cache6 last6;
Eyal Baria5679e82018-08-26 15:20:07 +0300195 u32 pkts_dropped = 0;
Eyal Bari7d92c092018-07-24 15:15:37 +0300196 u32 thread_index = vlib_get_thread_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197
Dave Barachf9c231e2016-08-05 10:10:18 -0400198 if (is_ip4)
Dave Barachb7b92992018-10-17 10:38:51 -0400199 clib_memset (&last4, 0xff, sizeof last4);
Dave Barachf9c231e2016-08-05 10:10:18 -0400200 else
Dave Barachb7b92992018-10-17 10:38:51 -0400201 clib_memset (&last6, 0xff, sizeof last6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Eyal Bari7d92c092018-07-24 15:15:37 +0300203 u32 *from = vlib_frame_vector_args (from_frame);
Eyal Barifb663012017-10-19 15:27:51 +0300204 u32 n_left_from = from_frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
Eyal Baria5679e82018-08-26 15:20:07 +0300206 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
207 vlib_get_buffers (vm, from, bufs, n_left_from);
208
Eyal Bariefd9cf32018-10-02 12:23:06 +0300209 u32 stats_if0 = ~0, stats_if1 = ~0;
Eyal Baria5679e82018-08-26 15:20:07 +0300210 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
211 while (n_left_from >= 4)
212 {
213 /* Prefetch next iteration. */
214 vlib_prefetch_buffer_header (b[2], LOAD);
215 vlib_prefetch_buffer_header (b[3], LOAD);
216
217 /* udp leaves current_data pointing at the vxlan header */
218 void *cur0 = vlib_buffer_get_current (b[0]);
219 void *cur1 = vlib_buffer_get_current (b[1]);
220 vxlan_header_t *vxlan0 = cur0;
221 vxlan_header_t *vxlan1 = cur1;
222
Eyal Baria5679e82018-08-26 15:20:07 +0300223
224 ip4_header_t *ip4_0, *ip4_1;
225 ip6_header_t *ip6_0, *ip6_1;
226 if (is_ip4)
227 {
228 ip4_0 = cur0 - sizeof (udp_header_t) - sizeof (ip4_header_t);
229 ip4_1 = cur1 - sizeof (udp_header_t) - sizeof (ip4_header_t);
230 }
231 else
232 {
233 ip6_0 = cur0 - sizeof (udp_header_t) - sizeof (ip6_header_t);
234 ip6_1 = cur1 - sizeof (udp_header_t) - sizeof (ip6_header_t);
235 }
236
237 /* pop vxlan */
238 vlib_buffer_advance (b[0], sizeof *vxlan0);
239 vlib_buffer_advance (b[1], sizeof *vxlan1);
240
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000241 u32 fi0 = vlib_buffer_get_ip_fib_index (b[0], is_ip4);
242 u32 fi1 = vlib_buffer_get_ip_fib_index (b[1], is_ip4);
Eyal Baria5679e82018-08-26 15:20:07 +0300243
Eyal Bariefd9cf32018-10-02 12:23:06 +0300244 vxlan_decap_info_t di0 = is_ip4 ?
245 vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_if0) :
246 vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_if0);
247 vxlan_decap_info_t di1 = is_ip4 ?
248 vxlan4_find_tunnel (vxm, &last4, fi1, ip4_1, vxlan1, &stats_if1) :
249 vxlan6_find_tunnel (vxm, &last6, fi1, ip6_1, vxlan1, &stats_if1);
Eyal Baria5679e82018-08-26 15:20:07 +0300250
251 /* Prefetch next iteration. */
252 CLIB_PREFETCH (b[2]->data, CLIB_CACHE_LINE_BYTES, LOAD);
253 CLIB_PREFETCH (b[3]->data, CLIB_CACHE_LINE_BYTES, LOAD);
254
255 u32 len0 = vlib_buffer_length_in_chain (vm, b[0]);
256 u32 len1 = vlib_buffer_length_in_chain (vm, b[1]);
257
Eyal Bariefd9cf32018-10-02 12:23:06 +0300258 next[0] = di0.next_index;
259 next[1] = di1.next_index;
Eyal Baria5679e82018-08-26 15:20:07 +0300260
Eyal Bariefd9cf32018-10-02 12:23:06 +0300261 u8 any_error = di0.error | di1.error;
262 if (PREDICT_TRUE (any_error == 0))
263 {
264 /* Required to make the l2 tag push / pop code work on l2 subifs */
265 vnet_update_l2_len (b[0]);
266 vnet_update_l2_len (b[1]);
267 /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
268 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
269 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = di1.sw_if_index;
270 vlib_increment_combined_counter (rx_counter, thread_index,
271 stats_if0, 1, len0);
272 vlib_increment_combined_counter (rx_counter, thread_index,
273 stats_if1, 1, len1);
Eyal Baria5679e82018-08-26 15:20:07 +0300274 }
275 else
276 {
Eyal Bariefd9cf32018-10-02 12:23:06 +0300277 if (di0.error == 0)
Eyal Baria5679e82018-08-26 15:20:07 +0300278 {
Eyal Bariefd9cf32018-10-02 12:23:06 +0300279 vnet_update_l2_len (b[0]);
280 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
281 vlib_increment_combined_counter (rx_counter, thread_index,
282 stats_if0, 1, len0);
Eyal Baria5679e82018-08-26 15:20:07 +0300283 }
Eyal Bariefd9cf32018-10-02 12:23:06 +0300284 else
285 {
286 b[0]->error = node->errors[di0.error];
287 pkts_dropped++;
288 }
Eyal Baria5679e82018-08-26 15:20:07 +0300289
Eyal Bariefd9cf32018-10-02 12:23:06 +0300290 if (di1.error == 0)
291 {
292 vnet_update_l2_len (b[1]);
293 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = di1.sw_if_index;
294 vlib_increment_combined_counter (rx_counter, thread_index,
295 stats_if1, 1, len1);
296 }
297 else
298 {
299 b[1]->error = node->errors[di1.error];
300 pkts_dropped++;
301 }
Eyal Baria5679e82018-08-26 15:20:07 +0300302 }
303
304 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
305 {
306 vxlan_rx_trace_t *tr =
307 vlib_add_trace (vm, node, b[0], sizeof (*tr));
308 tr->next_index = next[0];
Eyal Bariefd9cf32018-10-02 12:23:06 +0300309 tr->error = di0.error;
310 tr->tunnel_index = di0.sw_if_index == ~0 ?
311 ~0 : vxm->tunnel_index_by_sw_if_index[di0.sw_if_index];
Eyal Baria5679e82018-08-26 15:20:07 +0300312 tr->vni = vnet_get_vni (vxlan0);
313 }
314 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
315 {
316 vxlan_rx_trace_t *tr =
317 vlib_add_trace (vm, node, b[1], sizeof (*tr));
318 tr->next_index = next[1];
Eyal Bariefd9cf32018-10-02 12:23:06 +0300319 tr->error = di1.error;
320 tr->tunnel_index = di1.sw_if_index == ~0 ?
321 ~0 : vxm->tunnel_index_by_sw_if_index[di1.sw_if_index];
Eyal Baria5679e82018-08-26 15:20:07 +0300322 tr->vni = vnet_get_vni (vxlan1);
323 }
324 b += 2;
325 next += 2;
326 n_left_from -= 2;
327 }
328
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 while (n_left_from > 0)
330 {
Eyal Baria5679e82018-08-26 15:20:07 +0300331 /* udp leaves current_data pointing at the vxlan header */
332 void *cur0 = vlib_buffer_get_current (b[0]);
333 vxlan_header_t *vxlan0 = cur0;
Eyal Baria5679e82018-08-26 15:20:07 +0300334 ip4_header_t *ip4_0;
335 ip6_header_t *ip6_0;
336 if (is_ip4)
337 ip4_0 = cur0 - sizeof (udp_header_t) - sizeof (ip4_header_t);
338 else
339 ip6_0 = cur0 - sizeof (udp_header_t) - sizeof (ip6_header_t);
Eyal Barifb663012017-10-19 15:27:51 +0300340
Eyal Baria5679e82018-08-26 15:20:07 +0300341 /* pop (ip, udp, vxlan) */
342 vlib_buffer_advance (b[0], sizeof (*vxlan0));
343
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000344 u32 fi0 = vlib_buffer_get_ip_fib_index (b[0], is_ip4);
Eyal Baria5679e82018-08-26 15:20:07 +0300345
Eyal Bariefd9cf32018-10-02 12:23:06 +0300346 vxlan_decap_info_t di0 = is_ip4 ?
347 vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_if0) :
348 vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_if0);
Eyal Baria5679e82018-08-26 15:20:07 +0300349
Eyal Baria5679e82018-08-26 15:20:07 +0300350 uword len0 = vlib_buffer_length_in_chain (vm, b[0]);
351
Eyal Bariefd9cf32018-10-02 12:23:06 +0300352 next[0] = di0.next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353
Eyal Bariefd9cf32018-10-02 12:23:06 +0300354 /* Validate VXLAN tunnel encap-fib index against packet */
355 if (di0.error == 0)
356 {
357 /* Required to make the l2 tag push / pop code work on l2 subifs */
358 vnet_update_l2_len (b[0]);
359
360 /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
361 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
362
363 vlib_increment_combined_counter (rx_counter, thread_index,
364 stats_if0, 1, len0);
Eyal Baria5679e82018-08-26 15:20:07 +0300365 }
366 else
367 {
Eyal Bariefd9cf32018-10-02 12:23:06 +0300368 b[0]->error = node->errors[di0.error];
369 pkts_dropped++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 }
371
Eyal Baria5679e82018-08-26 15:20:07 +0300372 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373 {
Eyal Baria5679e82018-08-26 15:20:07 +0300374 vxlan_rx_trace_t *tr
375 = vlib_add_trace (vm, node, b[0], sizeof (*tr));
376 tr->next_index = next[0];
Eyal Bariefd9cf32018-10-02 12:23:06 +0300377 tr->error = di0.error;
378 tr->tunnel_index = di0.sw_if_index == ~0 ?
379 ~0 : vxm->tunnel_index_by_sw_if_index[di0.sw_if_index];
Eyal Baria5679e82018-08-26 15:20:07 +0300380 tr->vni = vnet_get_vni (vxlan0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381 }
Eyal Baria5679e82018-08-26 15:20:07 +0300382 b += 1;
383 next += 1;
384 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385 }
Eyal Baria5679e82018-08-26 15:20:07 +0300386 vlib_buffer_enqueue_to_next (vm, node, from, nexts, from_frame->n_vectors);
Eyal Barifb663012017-10-19 15:27:51 +0300387 /* Do we still need this now that tunnel tx stats is kept? */
388 u32 node_idx = is_ip4 ? vxlan4_input_node.index : vxlan6_input_node.index;
389 vlib_node_increment_counter (vm, node_idx, VXLAN_ERROR_DECAPSULATED,
Eyal Baria5679e82018-08-26 15:20:07 +0300390 from_frame->n_vectors - pkts_dropped);
Eyal Barifb663012017-10-19 15:27:51 +0300391
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392 return from_frame->n_vectors;
393}
394
Eyal Baria5679e82018-08-26 15:20:07 +0300395VLIB_NODE_FN (vxlan4_input_node) (vlib_main_t * vm,
396 vlib_node_runtime_t * node,
397 vlib_frame_t * from_frame)
Chris Luke99cb3352016-04-26 10:49:53 -0400398{
Eyal Bari7d92c092018-07-24 15:15:37 +0300399 return vxlan_input (vm, node, from_frame, /* is_ip4 */ 1);
Chris Luke99cb3352016-04-26 10:49:53 -0400400}
401
Eyal Baria5679e82018-08-26 15:20:07 +0300402VLIB_NODE_FN (vxlan6_input_node) (vlib_main_t * vm,
403 vlib_node_runtime_t * node,
404 vlib_frame_t * from_frame)
Chris Luke99cb3352016-04-26 10:49:53 -0400405{
Eyal Bari7d92c092018-07-24 15:15:37 +0300406 return vxlan_input (vm, node, from_frame, /* is_ip4 */ 0);
Chris Luke99cb3352016-04-26 10:49:53 -0400407}
408
Eyal Bari7d92c092018-07-24 15:15:37 +0300409static char *vxlan_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410#define vxlan_error(n,s) s,
411#include <vnet/vxlan/vxlan_error.def>
412#undef vxlan_error
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413};
414
Eyal Bari7d92c092018-07-24 15:15:37 +0300415/* *INDENT-OFF* */
416VLIB_REGISTER_NODE (vxlan4_input_node) =
417{
Chris Luke99cb3352016-04-26 10:49:53 -0400418 .name = "vxlan4-input",
Chris Luke99cb3352016-04-26 10:49:53 -0400419 .vector_size = sizeof (u32),
Chris Luke99cb3352016-04-26 10:49:53 -0400420 .n_errors = VXLAN_N_ERROR,
421 .error_strings = vxlan_error_strings,
Chris Luke99cb3352016-04-26 10:49:53 -0400422 .n_next_nodes = VXLAN_INPUT_N_NEXT,
Eyal Bari7d92c092018-07-24 15:15:37 +0300423 .format_trace = format_vxlan_rx_trace,
Chris Luke99cb3352016-04-26 10:49:53 -0400424 .next_nodes = {
425#define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
426 foreach_vxlan_input_next
427#undef _
428 },
Chris Luke99cb3352016-04-26 10:49:53 -0400429};
Damjan Marion1c80e832016-05-11 23:07:18 +0200430
Eyal Bari7d92c092018-07-24 15:15:37 +0300431VLIB_REGISTER_NODE (vxlan6_input_node) =
432{
Chris Luke99cb3352016-04-26 10:49:53 -0400433 .name = "vxlan6-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435 .n_errors = VXLAN_N_ERROR,
436 .error_strings = vxlan_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437 .n_next_nodes = VXLAN_INPUT_N_NEXT,
438 .next_nodes = {
439#define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
440 foreach_vxlan_input_next
441#undef _
442 },
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443 .format_trace = format_vxlan_rx_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444};
Eyal Bari7d92c092018-07-24 15:15:37 +0300445/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +0200446
Eyal Bari7d92c092018-07-24 15:15:37 +0300447typedef enum
448{
John Lo37682e12016-11-30 12:51:39 -0500449 IP_VXLAN_BYPASS_NEXT_DROP,
450 IP_VXLAN_BYPASS_NEXT_VXLAN,
451 IP_VXLAN_BYPASS_N_NEXT,
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700452} ip_vxlan_bypass_next_t;
John Lo37682e12016-11-30 12:51:39 -0500453
454always_inline uword
455ip_vxlan_bypass_inline (vlib_main_t * vm,
John Lo2b81eb82017-01-30 13:12:10 -0500456 vlib_node_runtime_t * node,
Eyal Bari7d92c092018-07-24 15:15:37 +0300457 vlib_frame_t * frame, u32 is_ip4)
John Lo37682e12016-11-30 12:51:39 -0500458{
Eyal Bari7d92c092018-07-24 15:15:37 +0300459 vxlan_main_t *vxm = &vxlan_main;
460 u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
461 vlib_node_runtime_t *error_node =
462 vlib_node_get_runtime (vm, ip4_input_node.index);
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000463 vtep4_key_t last_vtep4; /* last IPv4 address / fib index
464 matching a local VTEP address */
465 vtep6_key_t last_vtep6; /* last IPv6 address / fib index
466 matching a local VTEP address */
Zhiyong Yang5e524172020-07-08 20:28:36 +0000467 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
468
Artem Glazychev839dcc02020-12-01 02:39:21 +0700469 last_tunnel_cache4 last4;
470 last_tunnel_cache6 last6;
471
Zhiyong Yang5e524172020-07-08 20:28:36 +0000472#ifdef CLIB_HAVE_VEC512
473 vtep4_cache_t vtep4_u512;
474 clib_memset (&vtep4_u512, 0, sizeof (vtep4_u512));
475#endif
John Lo37682e12016-11-30 12:51:39 -0500476
477 from = vlib_frame_vector_args (frame);
478 n_left_from = frame->n_vectors;
479 next_index = node->cached_next_index;
480
Zhiyong Yang5e524172020-07-08 20:28:36 +0000481 vlib_get_buffers (vm, from, bufs, n_left_from);
482
John Lo37682e12016-11-30 12:51:39 -0500483 if (node->flags & VLIB_NODE_FLAG_TRACE)
484 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
485
Eyal Bari7d92c092018-07-24 15:15:37 +0300486 if (is_ip4)
Artem Glazychev839dcc02020-12-01 02:39:21 +0700487 {
488 vtep4_key_init (&last_vtep4);
489 clib_memset (&last4, 0xff, sizeof last4);
490 }
Eyal Bari7d92c092018-07-24 15:15:37 +0300491 else
Artem Glazychev839dcc02020-12-01 02:39:21 +0700492 {
493 vtep6_key_init (&last_vtep6);
494 clib_memset (&last6, 0xff, sizeof last6);
495 }
John Lo37682e12016-11-30 12:51:39 -0500496
497 while (n_left_from > 0)
498 {
499 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
500
501 while (n_left_from >= 4 && n_left_to_next >= 2)
Eyal Bari7d92c092018-07-24 15:15:37 +0300502 {
503 vlib_buffer_t *b0, *b1;
504 ip4_header_t *ip40, *ip41;
505 ip6_header_t *ip60, *ip61;
506 udp_header_t *udp0, *udp1;
Artem Glazychev839dcc02020-12-01 02:39:21 +0700507 vxlan_header_t *vxlan0, *vxlan1;
Eyal Bari7d92c092018-07-24 15:15:37 +0300508 u32 bi0, ip_len0, udp_len0, flags0, next0;
509 u32 bi1, ip_len1, udp_len1, flags1, next1;
510 i32 len_diff0, len_diff1;
511 u8 error0, good_udp0, proto0;
512 u8 error1, good_udp1, proto1;
Artem Glazychev839dcc02020-12-01 02:39:21 +0700513 u32 stats_if0 = ~0, stats_if1 = ~0;
John Lo37682e12016-11-30 12:51:39 -0500514
515 /* Prefetch next iteration. */
516 {
Zhiyong Yang5e524172020-07-08 20:28:36 +0000517 vlib_prefetch_buffer_header (b[2], LOAD);
518 vlib_prefetch_buffer_header (b[3], LOAD);
John Lo37682e12016-11-30 12:51:39 -0500519
Zhiyong Yang5e524172020-07-08 20:28:36 +0000520 CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
521 CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
John Lo37682e12016-11-30 12:51:39 -0500522 }
523
Eyal Bari7d92c092018-07-24 15:15:37 +0300524 bi0 = to_next[0] = from[0];
525 bi1 = to_next[1] = from[1];
526 from += 2;
527 n_left_from -= 2;
528 to_next += 2;
529 n_left_to_next -= 2;
John Lo37682e12016-11-30 12:51:39 -0500530
Zhiyong Yang5e524172020-07-08 20:28:36 +0000531 b0 = b[0];
532 b1 = b[1];
533 b += 2;
John Lo2b81eb82017-01-30 13:12:10 -0500534 if (is_ip4)
535 {
536 ip40 = vlib_buffer_get_current (b0);
537 ip41 = vlib_buffer_get_current (b1);
538 }
539 else
540 {
541 ip60 = vlib_buffer_get_current (b0);
542 ip61 = vlib_buffer_get_current (b1);
543 }
John Lo37682e12016-11-30 12:51:39 -0500544
545 /* Setup packet for next IP feature */
Eyal Bari7d92c092018-07-24 15:15:37 +0300546 vnet_feature_next (&next0, b0);
547 vnet_feature_next (&next1, b1);
John Lo37682e12016-11-30 12:51:39 -0500548
John Lo2b81eb82017-01-30 13:12:10 -0500549 if (is_ip4)
550 {
551 /* Treat IP frag packets as "experimental" protocol for now
Eyal Bari7d92c092018-07-24 15:15:37 +0300552 until support of IP frag reassembly is implemented */
553 proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol;
554 proto1 = ip4_is_fragment (ip41) ? 0xfe : ip41->protocol;
John Lo2b81eb82017-01-30 13:12:10 -0500555 }
556 else
557 {
558 proto0 = ip60->protocol;
559 proto1 = ip61->protocol;
560 }
John Lo37682e12016-11-30 12:51:39 -0500561
562 /* Process packet 0 */
563 if (proto0 != IP_PROTOCOL_UDP)
Eyal Bari7d92c092018-07-24 15:15:37 +0300564 goto exit0; /* not UDP packet */
John Lo37682e12016-11-30 12:51:39 -0500565
John Lo2b81eb82017-01-30 13:12:10 -0500566 if (is_ip4)
567 udp0 = ip4_next_header (ip40);
568 else
569 udp0 = ip6_next_header (ip60);
570
Artem Glazychev839dcc02020-12-01 02:39:21 +0700571 u32 fi0 = vlib_buffer_get_ip_fib_index (b0, is_ip4);
572 vxlan0 = vlib_buffer_get_current (b0) + sizeof (udp_header_t) +
573 sizeof (ip4_header_t);
574
575 vxlan_decap_info_t di0 =
576 is_ip4 ?
577 vxlan4_find_tunnel (vxm, &last4, fi0, ip40, vxlan0, &stats_if0) :
578 vxlan6_find_tunnel (vxm, &last6, fi0, ip60, vxlan0, &stats_if0);
579
580 if (PREDICT_FALSE (di0.sw_if_index == ~0))
581 goto exit0; /* unknown interface */
John Lo37682e12016-11-30 12:51:39 -0500582
Eyal Bari7d92c092018-07-24 15:15:37 +0300583 /* Validate DIP against VTEPs */
Eyal Bari0f4b1842018-04-12 12:39:51 +0300584 if (is_ip4)
John Lo37682e12016-11-30 12:51:39 -0500585 {
Zhiyong Yang5e524172020-07-08 20:28:36 +0000586#ifdef CLIB_HAVE_VEC512
587 if (!vtep4_check_vector
588 (&vxm->vtep_table, b0, ip40, &last_vtep4, &vtep4_u512))
589#else
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000590 if (!vtep4_check (&vxm->vtep_table, b0, ip40, &last_vtep4))
Zhiyong Yang5e524172020-07-08 20:28:36 +0000591#endif
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000592 goto exit0; /* no local VTEP for VXLAN packet */
John Lo37682e12016-11-30 12:51:39 -0500593 }
John Lo2b81eb82017-01-30 13:12:10 -0500594 else
595 {
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000596 if (!vtep6_check (&vxm->vtep_table, b0, ip60, &last_vtep6))
597 goto exit0; /* no local VTEP for VXLAN packet */
John Lo2b81eb82017-01-30 13:12:10 -0500598 }
John Lo37682e12016-11-30 12:51:39 -0500599
600 flags0 = b0->flags;
Damjan Marion213b5aa2017-07-13 21:19:27 +0200601 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo37682e12016-11-30 12:51:39 -0500602
603 /* Don't verify UDP checksum for packets with explicit zero checksum. */
604 good_udp0 |= udp0->checksum == 0;
605
606 /* Verify UDP length */
John Lo2b81eb82017-01-30 13:12:10 -0500607 if (is_ip4)
608 ip_len0 = clib_net_to_host_u16 (ip40->length);
609 else
610 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
John Lo37682e12016-11-30 12:51:39 -0500611 udp_len0 = clib_net_to_host_u16 (udp0->length);
John Lo37682e12016-11-30 12:51:39 -0500612 len_diff0 = ip_len0 - udp_len0;
613
614 /* Verify UDP checksum */
615 if (PREDICT_FALSE (!good_udp0))
616 {
Zhiyong Yang47ea4c32020-06-10 13:23:50 +0000617 if (is_ip4)
618 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
619 else
620 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
621 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo37682e12016-11-30 12:51:39 -0500622 }
623
John Lo2b81eb82017-01-30 13:12:10 -0500624 if (is_ip4)
625 {
626 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
627 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
628 }
629 else
630 {
631 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
632 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
633 }
John Lo37682e12016-11-30 12:51:39 -0500634
Eyal Bari0f4b1842018-04-12 12:39:51 +0300635 next0 = error0 ?
John Lo37682e12016-11-30 12:51:39 -0500636 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
637 b0->error = error0 ? error_node->errors[error0] : 0;
638
John Lo2b81eb82017-01-30 13:12:10 -0500639 /* vxlan-input node expect current at VXLAN header */
640 if (is_ip4)
Eyal Bari7d92c092018-07-24 15:15:37 +0300641 vlib_buffer_advance (b0,
642 sizeof (ip4_header_t) +
643 sizeof (udp_header_t));
John Lo2b81eb82017-01-30 13:12:10 -0500644 else
Eyal Bari7d92c092018-07-24 15:15:37 +0300645 vlib_buffer_advance (b0,
646 sizeof (ip6_header_t) +
647 sizeof (udp_header_t));
John Lo2b81eb82017-01-30 13:12:10 -0500648
John Lo37682e12016-11-30 12:51:39 -0500649 exit0:
650 /* Process packet 1 */
651 if (proto1 != IP_PROTOCOL_UDP)
Eyal Bari7d92c092018-07-24 15:15:37 +0300652 goto exit1; /* not UDP packet */
John Lo37682e12016-11-30 12:51:39 -0500653
John Lo2b81eb82017-01-30 13:12:10 -0500654 if (is_ip4)
655 udp1 = ip4_next_header (ip41);
656 else
657 udp1 = ip6_next_header (ip61);
658
Artem Glazychev839dcc02020-12-01 02:39:21 +0700659 u32 fi1 = vlib_buffer_get_ip_fib_index (b1, is_ip4);
660 vxlan1 = vlib_buffer_get_current (b1) + sizeof (udp_header_t) +
661 sizeof (ip4_header_t);
662
663 vxlan_decap_info_t di1 =
664 is_ip4 ?
665 vxlan4_find_tunnel (vxm, &last4, fi1, ip41, vxlan1, &stats_if1) :
666 vxlan6_find_tunnel (vxm, &last6, fi1, ip61, vxlan1, &stats_if1);
667
668 if (PREDICT_FALSE (di1.sw_if_index == ~0))
669 goto exit1; /* unknown interface */
John Lo37682e12016-11-30 12:51:39 -0500670
Eyal Bari7d92c092018-07-24 15:15:37 +0300671 /* Validate DIP against VTEPs */
Eyal Bari0f4b1842018-04-12 12:39:51 +0300672 if (is_ip4)
John Lo37682e12016-11-30 12:51:39 -0500673 {
Zhiyong Yang5e524172020-07-08 20:28:36 +0000674#ifdef CLIB_HAVE_VEC512
675 if (!vtep4_check_vector
676 (&vxm->vtep_table, b1, ip41, &last_vtep4, &vtep4_u512))
677#else
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000678 if (!vtep4_check (&vxm->vtep_table, b1, ip41, &last_vtep4))
Zhiyong Yang5e524172020-07-08 20:28:36 +0000679#endif
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000680 goto exit1; /* no local VTEP for VXLAN packet */
John Lo37682e12016-11-30 12:51:39 -0500681 }
John Lo2b81eb82017-01-30 13:12:10 -0500682 else
683 {
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000684 if (!vtep6_check (&vxm->vtep_table, b1, ip61, &last_vtep6))
685 goto exit1; /* no local VTEP for VXLAN packet */
John Lo2b81eb82017-01-30 13:12:10 -0500686 }
John Lo37682e12016-11-30 12:51:39 -0500687
688 flags1 = b1->flags;
Damjan Marion213b5aa2017-07-13 21:19:27 +0200689 good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo37682e12016-11-30 12:51:39 -0500690
691 /* Don't verify UDP checksum for packets with explicit zero checksum. */
692 good_udp1 |= udp1->checksum == 0;
693
694 /* Verify UDP length */
John Lo2b81eb82017-01-30 13:12:10 -0500695 if (is_ip4)
696 ip_len1 = clib_net_to_host_u16 (ip41->length);
697 else
698 ip_len1 = clib_net_to_host_u16 (ip61->payload_length);
John Lo37682e12016-11-30 12:51:39 -0500699 udp_len1 = clib_net_to_host_u16 (udp1->length);
John Lo37682e12016-11-30 12:51:39 -0500700 len_diff1 = ip_len1 - udp_len1;
701
702 /* Verify UDP checksum */
703 if (PREDICT_FALSE (!good_udp1))
704 {
Zhiyong Yang47ea4c32020-06-10 13:23:50 +0000705 if (is_ip4)
706 flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
707 else
708 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
709 good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo37682e12016-11-30 12:51:39 -0500710 }
711
John Lo2b81eb82017-01-30 13:12:10 -0500712 if (is_ip4)
713 {
714 error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM;
715 error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH;
716 }
717 else
718 {
Eyal Baria93ea422017-02-01 13:36:15 +0200719 error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM;
720 error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH;
John Lo2b81eb82017-01-30 13:12:10 -0500721 }
John Lo37682e12016-11-30 12:51:39 -0500722
Eyal Bari0f4b1842018-04-12 12:39:51 +0300723 next1 = error1 ?
John Lo37682e12016-11-30 12:51:39 -0500724 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
725 b1->error = error1 ? error_node->errors[error1] : 0;
Eyal Bari0f4b1842018-04-12 12:39:51 +0300726
John Lo2b81eb82017-01-30 13:12:10 -0500727 /* vxlan-input node expect current at VXLAN header */
728 if (is_ip4)
Eyal Bari7d92c092018-07-24 15:15:37 +0300729 vlib_buffer_advance (b1,
730 sizeof (ip4_header_t) +
731 sizeof (udp_header_t));
John Lo2b81eb82017-01-30 13:12:10 -0500732 else
Eyal Bari7d92c092018-07-24 15:15:37 +0300733 vlib_buffer_advance (b1,
734 sizeof (ip6_header_t) +
735 sizeof (udp_header_t));
John Lo2b81eb82017-01-30 13:12:10 -0500736
John Lo37682e12016-11-30 12:51:39 -0500737 exit1:
738 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
739 to_next, n_left_to_next,
740 bi0, bi1, next0, next1);
741 }
742
743 while (n_left_from > 0 && n_left_to_next > 0)
744 {
Eyal Bari7d92c092018-07-24 15:15:37 +0300745 vlib_buffer_t *b0;
746 ip4_header_t *ip40;
747 ip6_header_t *ip60;
748 udp_header_t *udp0;
Artem Glazychev839dcc02020-12-01 02:39:21 +0700749 vxlan_header_t *vxlan0;
Eyal Bari7d92c092018-07-24 15:15:37 +0300750 u32 bi0, ip_len0, udp_len0, flags0, next0;
John Lo37682e12016-11-30 12:51:39 -0500751 i32 len_diff0;
752 u8 error0, good_udp0, proto0;
Artem Glazychev839dcc02020-12-01 02:39:21 +0700753 u32 stats_if0 = ~0;
John Lo37682e12016-11-30 12:51:39 -0500754
755 bi0 = to_next[0] = from[0];
756 from += 1;
757 n_left_from -= 1;
758 to_next += 1;
759 n_left_to_next -= 1;
760
Zhiyong Yang5e524172020-07-08 20:28:36 +0000761 b0 = b[0];
762 b++;
John Lo2b81eb82017-01-30 13:12:10 -0500763 if (is_ip4)
764 ip40 = vlib_buffer_get_current (b0);
765 else
766 ip60 = vlib_buffer_get_current (b0);
John Lo37682e12016-11-30 12:51:39 -0500767
768 /* Setup packet for next IP feature */
Eyal Bari7d92c092018-07-24 15:15:37 +0300769 vnet_feature_next (&next0, b0);
John Lo37682e12016-11-30 12:51:39 -0500770
John Lo2b81eb82017-01-30 13:12:10 -0500771 if (is_ip4)
772 /* Treat IP4 frag packets as "experimental" protocol for now
773 until support of IP frag reassembly is implemented */
Eyal Bari7d92c092018-07-24 15:15:37 +0300774 proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol;
John Lo2b81eb82017-01-30 13:12:10 -0500775 else
776 proto0 = ip60->protocol;
John Lo37682e12016-11-30 12:51:39 -0500777
778 if (proto0 != IP_PROTOCOL_UDP)
Eyal Bari7d92c092018-07-24 15:15:37 +0300779 goto exit; /* not UDP packet */
John Lo37682e12016-11-30 12:51:39 -0500780
John Lo2b81eb82017-01-30 13:12:10 -0500781 if (is_ip4)
782 udp0 = ip4_next_header (ip40);
783 else
784 udp0 = ip6_next_header (ip60);
785
Artem Glazychev839dcc02020-12-01 02:39:21 +0700786 u32 fi0 = vlib_buffer_get_ip_fib_index (b0, is_ip4);
787 vxlan0 = vlib_buffer_get_current (b0) + sizeof (udp_header_t) +
788 sizeof (ip4_header_t);
789
790 vxlan_decap_info_t di0 =
791 is_ip4 ?
792 vxlan4_find_tunnel (vxm, &last4, fi0, ip40, vxlan0, &stats_if0) :
793 vxlan6_find_tunnel (vxm, &last6, fi0, ip60, vxlan0, &stats_if0);
794
795 if (PREDICT_FALSE (di0.sw_if_index == ~0))
796 goto exit; /* unknown interface */
John Lo37682e12016-11-30 12:51:39 -0500797
Eyal Bari7d92c092018-07-24 15:15:37 +0300798 /* Validate DIP against VTEPs */
Eyal Bari0f4b1842018-04-12 12:39:51 +0300799 if (is_ip4)
John Lo37682e12016-11-30 12:51:39 -0500800 {
Zhiyong Yang5e524172020-07-08 20:28:36 +0000801#ifdef CLIB_HAVE_VEC512
802 if (!vtep4_check_vector
803 (&vxm->vtep_table, b0, ip40, &last_vtep4, &vtep4_u512))
804#else
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000805 if (!vtep4_check (&vxm->vtep_table, b0, ip40, &last_vtep4))
Zhiyong Yang5e524172020-07-08 20:28:36 +0000806#endif
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000807 goto exit; /* no local VTEP for VXLAN packet */
John Lo37682e12016-11-30 12:51:39 -0500808 }
John Lo2b81eb82017-01-30 13:12:10 -0500809 else
810 {
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000811 if (!vtep6_check (&vxm->vtep_table, b0, ip60, &last_vtep6))
812 goto exit; /* no local VTEP for VXLAN packet */
John Lo2b81eb82017-01-30 13:12:10 -0500813 }
John Lo37682e12016-11-30 12:51:39 -0500814
815 flags0 = b0->flags;
Damjan Marion213b5aa2017-07-13 21:19:27 +0200816 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo37682e12016-11-30 12:51:39 -0500817
818 /* Don't verify UDP checksum for packets with explicit zero checksum. */
819 good_udp0 |= udp0->checksum == 0;
820
821 /* Verify UDP length */
John Lo2b81eb82017-01-30 13:12:10 -0500822 if (is_ip4)
823 ip_len0 = clib_net_to_host_u16 (ip40->length);
824 else
825 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
John Lo37682e12016-11-30 12:51:39 -0500826 udp_len0 = clib_net_to_host_u16 (udp0->length);
John Lo37682e12016-11-30 12:51:39 -0500827 len_diff0 = ip_len0 - udp_len0;
828
829 /* Verify UDP checksum */
830 if (PREDICT_FALSE (!good_udp0))
831 {
Zhiyong Yang47ea4c32020-06-10 13:23:50 +0000832 if (is_ip4)
833 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
834 else
835 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
836 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo37682e12016-11-30 12:51:39 -0500837 }
838
John Lo2b81eb82017-01-30 13:12:10 -0500839 if (is_ip4)
840 {
841 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
842 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
843 }
844 else
845 {
846 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
847 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
848 }
John Lo37682e12016-11-30 12:51:39 -0500849
Eyal Bari0f4b1842018-04-12 12:39:51 +0300850 next0 = error0 ?
John Lo37682e12016-11-30 12:51:39 -0500851 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
852 b0->error = error0 ? error_node->errors[error0] : 0;
853
John Lo2b81eb82017-01-30 13:12:10 -0500854 /* vxlan-input node expect current at VXLAN header */
855 if (is_ip4)
Eyal Bari7d92c092018-07-24 15:15:37 +0300856 vlib_buffer_advance (b0,
857 sizeof (ip4_header_t) +
858 sizeof (udp_header_t));
John Lo2b81eb82017-01-30 13:12:10 -0500859 else
Eyal Bari7d92c092018-07-24 15:15:37 +0300860 vlib_buffer_advance (b0,
861 sizeof (ip6_header_t) +
862 sizeof (udp_header_t));
John Lo2b81eb82017-01-30 13:12:10 -0500863
John Lo37682e12016-11-30 12:51:39 -0500864 exit:
865 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
866 to_next, n_left_to_next,
867 bi0, next0);
868 }
869
870 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
871 }
872
873 return frame->n_vectors;
874}
875
Eyal Baria5679e82018-08-26 15:20:07 +0300876VLIB_NODE_FN (ip4_vxlan_bypass_node) (vlib_main_t * vm,
877 vlib_node_runtime_t * node,
878 vlib_frame_t * frame)
John Lo37682e12016-11-30 12:51:39 -0500879{
880 return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 1);
881}
882
Eyal Bari7d92c092018-07-24 15:15:37 +0300883/* *INDENT-OFF* */
884VLIB_REGISTER_NODE (ip4_vxlan_bypass_node) =
885{
John Lo37682e12016-11-30 12:51:39 -0500886 .name = "ip4-vxlan-bypass",
887 .vector_size = sizeof (u32),
John Lo37682e12016-11-30 12:51:39 -0500888 .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
889 .next_nodes = {
Eyal Bari7d92c092018-07-24 15:15:37 +0300890 [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
891 [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan4-input",
John Lo37682e12016-11-30 12:51:39 -0500892 },
John Lo37682e12016-11-30 12:51:39 -0500893 .format_buffer = format_ip4_header,
894 .format_trace = format_ip4_forward_next_trace,
895};
896
Eyal Bari7d92c092018-07-24 15:15:37 +0300897/* *INDENT-ON* */
John Lo37682e12016-11-30 12:51:39 -0500898
John Lo37682e12016-11-30 12:51:39 -0500899/* Dummy init function to get us linked in. */
Eyal Baria5679e82018-08-26 15:20:07 +0300900static clib_error_t *
Eyal Bari7d92c092018-07-24 15:15:37 +0300901ip4_vxlan_bypass_init (vlib_main_t * vm)
902{
903 return 0;
904}
John Lo37682e12016-11-30 12:51:39 -0500905
906VLIB_INIT_FUNCTION (ip4_vxlan_bypass_init);
John Lo2b81eb82017-01-30 13:12:10 -0500907
Eyal Baria5679e82018-08-26 15:20:07 +0300908VLIB_NODE_FN (ip6_vxlan_bypass_node) (vlib_main_t * vm,
909 vlib_node_runtime_t * node,
910 vlib_frame_t * frame)
John Lo2b81eb82017-01-30 13:12:10 -0500911{
912 return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 0);
913}
914
Eyal Bari7d92c092018-07-24 15:15:37 +0300915/* *INDENT-OFF* */
916VLIB_REGISTER_NODE (ip6_vxlan_bypass_node) =
917{
John Lo2b81eb82017-01-30 13:12:10 -0500918 .name = "ip6-vxlan-bypass",
919 .vector_size = sizeof (u32),
John Lo2b81eb82017-01-30 13:12:10 -0500920 .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
921 .next_nodes = {
922 [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
923 [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan6-input",
924 },
John Lo2b81eb82017-01-30 13:12:10 -0500925 .format_buffer = format_ip6_header,
926 .format_trace = format_ip6_forward_next_trace,
927};
928
Eyal Bari7d92c092018-07-24 15:15:37 +0300929/* *INDENT-ON* */
John Lo2b81eb82017-01-30 13:12:10 -0500930
931/* Dummy init function to get us linked in. */
Eyal Baria5679e82018-08-26 15:20:07 +0300932static clib_error_t *
Eyal Bari7d92c092018-07-24 15:15:37 +0300933ip6_vxlan_bypass_init (vlib_main_t * vm)
934{
935 return 0;
936}
John Lo2b81eb82017-01-30 13:12:10 -0500937
938VLIB_INIT_FUNCTION (ip6_vxlan_bypass_init);
eyal bariaf86a482018-04-17 11:20:27 +0300939
940#define foreach_vxlan_flow_input_next \
941_(DROP, "error-drop") \
942_(L2_INPUT, "l2-input")
943
944typedef enum
945{
946#define _(s,n) VXLAN_FLOW_NEXT_##s,
947 foreach_vxlan_flow_input_next
948#undef _
Eyal Bari7d92c092018-07-24 15:15:37 +0300949 VXLAN_FLOW_N_NEXT,
eyal bariaf86a482018-04-17 11:20:27 +0300950} vxlan_flow_input_next_t;
951
952#define foreach_vxlan_flow_error \
953 _(NONE, "no error") \
954 _(IP_CHECKSUM_ERROR, "Rx ip checksum errors") \
955 _(IP_HEADER_ERROR, "Rx ip header errors") \
956 _(UDP_CHECKSUM_ERROR, "Rx udp checksum errors") \
957 _(UDP_LENGTH_ERROR, "Rx udp length errors")
958
959typedef enum
960{
961#define _(f,s) VXLAN_FLOW_ERROR_##f,
962 foreach_vxlan_flow_error
963#undef _
964 VXLAN_FLOW_N_ERROR,
965} vxlan_flow_error_t;
966
967static char *vxlan_flow_error_strings[] = {
968#define _(n,s) s,
969 foreach_vxlan_flow_error
970#undef _
971};
972
973
974static_always_inline u8
Eyal Bari7d92c092018-07-24 15:15:37 +0300975vxlan_validate_udp_csum (vlib_main_t * vm, vlib_buffer_t * b)
eyal bariaf86a482018-04-17 11:20:27 +0300976{
977 u32 flags = b->flags;
Eyal Bari7d92c092018-07-24 15:15:37 +0300978 enum
979 { offset =
980 sizeof (ip4_header_t) + sizeof (udp_header_t) + sizeof (vxlan_header_t),
981 };
eyal bariaf86a482018-04-17 11:20:27 +0300982
983 /* Verify UDP checksum */
984 if ((flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
Eyal Bari7d92c092018-07-24 15:15:37 +0300985 {
986 vlib_buffer_advance (b, -offset);
987 flags = ip4_tcp_udp_validate_checksum (vm, b);
988 vlib_buffer_advance (b, offset);
989 }
eyal bariaf86a482018-04-17 11:20:27 +0300990
991 return (flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
992}
993
994static_always_inline u8
Eyal Bari7d92c092018-07-24 15:15:37 +0300995vxlan_check_udp_csum (vlib_main_t * vm, vlib_buffer_t * b)
eyal bariaf86a482018-04-17 11:20:27 +0300996{
Eyal Bari7d92c092018-07-24 15:15:37 +0300997 ip4_vxlan_header_t *hdr = vlib_buffer_get_current (b) - sizeof *hdr;
998 udp_header_t *udp = &hdr->udp;
eyal bariaf86a482018-04-17 11:20:27 +0300999 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1000 u8 good_csum = (b->flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0 ||
1001 udp->checksum == 0;
1002
1003 return !good_csum;
1004}
1005
1006static_always_inline u8
Eyal Bari7d92c092018-07-24 15:15:37 +03001007vxlan_check_ip (vlib_buffer_t * b, u16 payload_len)
eyal bariaf86a482018-04-17 11:20:27 +03001008{
Eyal Bari7d92c092018-07-24 15:15:37 +03001009 ip4_vxlan_header_t *hdr = vlib_buffer_get_current (b) - sizeof *hdr;
eyal bariaf86a482018-04-17 11:20:27 +03001010 u16 ip_len = clib_net_to_host_u16 (hdr->ip4.length);
1011 u16 expected = payload_len + sizeof *hdr;
Eyal Bari7d92c092018-07-24 15:15:37 +03001012 return ip_len > expected || hdr->ip4.ttl == 0
1013 || hdr->ip4.ip_version_and_header_length != 0x45;
eyal bariaf86a482018-04-17 11:20:27 +03001014}
1015
1016static_always_inline u8
Eyal Bari7d92c092018-07-24 15:15:37 +03001017vxlan_check_ip_udp_len (vlib_buffer_t * b)
eyal bariaf86a482018-04-17 11:20:27 +03001018{
Eyal Bari7d92c092018-07-24 15:15:37 +03001019 ip4_vxlan_header_t *hdr = vlib_buffer_get_current (b) - sizeof *hdr;
eyal bariaf86a482018-04-17 11:20:27 +03001020 u16 ip_len = clib_net_to_host_u16 (hdr->ip4.length);
1021 u16 udp_len = clib_net_to_host_u16 (hdr->udp.length);
1022 return udp_len > ip_len;
1023}
1024
1025static_always_inline u8
1026vxlan_err_code (u8 ip_err0, u8 udp_err0, u8 csum_err0)
1027{
1028 u8 error0 = VXLAN_FLOW_ERROR_NONE;
1029 if (ip_err0)
Eyal Bari7d92c092018-07-24 15:15:37 +03001030 error0 = VXLAN_FLOW_ERROR_IP_HEADER_ERROR;
eyal bariaf86a482018-04-17 11:20:27 +03001031 if (udp_err0)
Eyal Bari7d92c092018-07-24 15:15:37 +03001032 error0 = VXLAN_FLOW_ERROR_UDP_LENGTH_ERROR;
eyal bariaf86a482018-04-17 11:20:27 +03001033 if (csum_err0)
Eyal Bari7d92c092018-07-24 15:15:37 +03001034 error0 = VXLAN_FLOW_ERROR_UDP_CHECKSUM_ERROR;
eyal bariaf86a482018-04-17 11:20:27 +03001035 return error0;
1036}
1037
Eyal Bari93a6f252018-06-14 08:57:39 +03001038VLIB_NODE_FN (vxlan4_flow_input_node) (vlib_main_t * vm,
Eyal Bari7d92c092018-07-24 15:15:37 +03001039 vlib_node_runtime_t * node,
1040 vlib_frame_t * f)
eyal bariaf86a482018-04-17 11:20:27 +03001041{
Eyal Bari7d92c092018-07-24 15:15:37 +03001042 enum
1043 { payload_offset = sizeof (ip4_vxlan_header_t) };
eyal bariaf86a482018-04-17 11:20:27 +03001044
Eyal Bari7d92c092018-07-24 15:15:37 +03001045 vxlan_main_t *vxm = &vxlan_main;
1046 vnet_interface_main_t *im = &vnet_main.interface_main;
1047 vlib_combined_counter_main_t *rx_counter[VXLAN_FLOW_N_NEXT] = {
1048 [VXLAN_FLOW_NEXT_DROP] =
1049 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_DROP,
1050 [VXLAN_FLOW_NEXT_L2_INPUT] =
1051 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
eyal bariaf86a482018-04-17 11:20:27 +03001052 };
Eyal Bari7d92c092018-07-24 15:15:37 +03001053 u32 thread_index = vlib_get_thread_index ();
eyal bariaf86a482018-04-17 11:20:27 +03001054
Eyal Bari7d92c092018-07-24 15:15:37 +03001055 u32 *from = vlib_frame_vector_args (f);
eyal bariaf86a482018-04-17 11:20:27 +03001056 u32 n_left_from = f->n_vectors;
1057 u32 next_index = VXLAN_FLOW_NEXT_L2_INPUT;
1058
1059 while (n_left_from > 0)
eyal bariaf86a482018-04-17 11:20:27 +03001060 {
Eyal Bari7d92c092018-07-24 15:15:37 +03001061 u32 n_left_to_next, *to_next;
eyal bariaf86a482018-04-17 11:20:27 +03001062
Eyal Bari7d92c092018-07-24 15:15:37 +03001063 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
eyal bariaf86a482018-04-17 11:20:27 +03001064
Eyal Bari7d92c092018-07-24 15:15:37 +03001065 while (n_left_from > 3 && n_left_to_next > 3)
eyal bariaf86a482018-04-17 11:20:27 +03001066 {
Eyal Bari7d92c092018-07-24 15:15:37 +03001067 u32 bi0 = to_next[0] = from[0];
1068 u32 bi1 = to_next[1] = from[1];
1069 u32 bi2 = to_next[2] = from[2];
1070 u32 bi3 = to_next[3] = from[3];
1071 from += 4;
1072 n_left_from -= 4;
1073 to_next += 4;
1074 n_left_to_next -= 4;
eyal bariaf86a482018-04-17 11:20:27 +03001075
Eyal Bari7d92c092018-07-24 15:15:37 +03001076 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
1077 vlib_buffer_t *b1 = vlib_get_buffer (vm, bi1);
1078 vlib_buffer_t *b2 = vlib_get_buffer (vm, bi2);
1079 vlib_buffer_t *b3 = vlib_get_buffer (vm, bi3);
1080
1081 vlib_buffer_advance (b0, payload_offset);
1082 vlib_buffer_advance (b1, payload_offset);
1083 vlib_buffer_advance (b2, payload_offset);
1084 vlib_buffer_advance (b3, payload_offset);
1085
1086 u16 len0 = vlib_buffer_length_in_chain (vm, b0);
1087 u16 len1 = vlib_buffer_length_in_chain (vm, b1);
1088 u16 len2 = vlib_buffer_length_in_chain (vm, b2);
1089 u16 len3 = vlib_buffer_length_in_chain (vm, b3);
1090
1091 u32 next0 = VXLAN_FLOW_NEXT_L2_INPUT, next1 =
1092 VXLAN_FLOW_NEXT_L2_INPUT, next2 =
1093 VXLAN_FLOW_NEXT_L2_INPUT, next3 = VXLAN_FLOW_NEXT_L2_INPUT;
1094
1095 u8 ip_err0 = vxlan_check_ip (b0, len0);
1096 u8 ip_err1 = vxlan_check_ip (b1, len1);
1097 u8 ip_err2 = vxlan_check_ip (b2, len2);
1098 u8 ip_err3 = vxlan_check_ip (b3, len3);
1099 u8 ip_err = ip_err0 | ip_err1 | ip_err2 | ip_err3;
1100
1101 u8 udp_err0 = vxlan_check_ip_udp_len (b0);
1102 u8 udp_err1 = vxlan_check_ip_udp_len (b1);
1103 u8 udp_err2 = vxlan_check_ip_udp_len (b2);
1104 u8 udp_err3 = vxlan_check_ip_udp_len (b3);
1105 u8 udp_err = udp_err0 | udp_err1 | udp_err2 | udp_err3;
1106
1107 u8 csum_err0 = vxlan_check_udp_csum (vm, b0);
1108 u8 csum_err1 = vxlan_check_udp_csum (vm, b1);
1109 u8 csum_err2 = vxlan_check_udp_csum (vm, b2);
1110 u8 csum_err3 = vxlan_check_udp_csum (vm, b3);
1111 u8 csum_err = csum_err0 | csum_err1 | csum_err2 | csum_err3;
1112
1113 if (PREDICT_FALSE (csum_err))
1114 {
1115 if (csum_err0)
1116 csum_err0 = !vxlan_validate_udp_csum (vm, b0);
1117 if (csum_err1)
1118 csum_err1 = !vxlan_validate_udp_csum (vm, b1);
1119 if (csum_err2)
1120 csum_err2 = !vxlan_validate_udp_csum (vm, b2);
1121 if (csum_err3)
1122 csum_err3 = !vxlan_validate_udp_csum (vm, b3);
1123 csum_err = csum_err0 | csum_err1 | csum_err2 | csum_err3;
1124 }
1125
1126 if (PREDICT_FALSE (ip_err || udp_err || csum_err))
1127 {
1128 if (ip_err0 || udp_err0 || csum_err0)
1129 {
1130 next0 = VXLAN_FLOW_NEXT_DROP;
1131 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1132 b0->error = node->errors[error0];
1133 }
1134 if (ip_err1 || udp_err1 || csum_err1)
1135 {
1136 next1 = VXLAN_FLOW_NEXT_DROP;
1137 u8 error1 = vxlan_err_code (ip_err1, udp_err1, csum_err1);
1138 b1->error = node->errors[error1];
1139 }
1140 if (ip_err2 || udp_err2 || csum_err2)
1141 {
1142 next2 = VXLAN_FLOW_NEXT_DROP;
1143 u8 error2 = vxlan_err_code (ip_err2, udp_err2, csum_err2);
1144 b2->error = node->errors[error2];
1145 }
1146 if (ip_err3 || udp_err3 || csum_err3)
1147 {
1148 next3 = VXLAN_FLOW_NEXT_DROP;
1149 u8 error3 = vxlan_err_code (ip_err3, udp_err3, csum_err3);
1150 b3->error = node->errors[error3];
1151 }
1152 }
1153
1154 vnet_update_l2_len (b0);
1155 vnet_update_l2_len (b1);
1156 vnet_update_l2_len (b2);
1157 vnet_update_l2_len (b3);
1158
1159 ASSERT (b0->flow_id != 0);
1160 ASSERT (b1->flow_id != 0);
1161 ASSERT (b2->flow_id != 0);
1162 ASSERT (b3->flow_id != 0);
1163
1164 u32 t_index0 = b0->flow_id - vxm->flow_id_start;
1165 u32 t_index1 = b1->flow_id - vxm->flow_id_start;
1166 u32 t_index2 = b2->flow_id - vxm->flow_id_start;
1167 u32 t_index3 = b3->flow_id - vxm->flow_id_start;
1168
1169 vxlan_tunnel_t *t0 = &vxm->tunnels[t_index0];
1170 vxlan_tunnel_t *t1 = &vxm->tunnels[t_index1];
1171 vxlan_tunnel_t *t2 = &vxm->tunnels[t_index2];
1172 vxlan_tunnel_t *t3 = &vxm->tunnels[t_index3];
1173
1174 /* flow id consumed */
1175 b0->flow_id = 0;
1176 b1->flow_id = 0;
1177 b2->flow_id = 0;
1178 b3->flow_id = 0;
1179
1180 u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1181 t0->sw_if_index;
1182 u32 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX] =
1183 t1->sw_if_index;
1184 u32 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_RX] =
1185 t2->sw_if_index;
1186 u32 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_RX] =
1187 t3->sw_if_index;
1188
1189 vlib_increment_combined_counter (rx_counter[next0], thread_index,
1190 sw_if_index0, 1, len0);
1191 vlib_increment_combined_counter (rx_counter[next1], thread_index,
1192 sw_if_index1, 1, len1);
1193 vlib_increment_combined_counter (rx_counter[next2], thread_index,
1194 sw_if_index2, 1, len2);
1195 vlib_increment_combined_counter (rx_counter[next3], thread_index,
1196 sw_if_index3, 1, len3);
1197
1198 u32 flags = b0->flags | b1->flags | b2->flags | b3->flags;
1199
1200 if (PREDICT_FALSE (flags & VLIB_BUFFER_IS_TRACED))
1201 {
1202 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1203 {
1204 vxlan_rx_trace_t *tr =
1205 vlib_add_trace (vm, node, b0, sizeof *tr);
1206 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1207 tr->next_index = next0;
1208 tr->error = error0;
1209 tr->tunnel_index = t_index0;
1210 tr->vni = t0->vni;
1211 }
1212 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1213 {
1214 vxlan_rx_trace_t *tr =
1215 vlib_add_trace (vm, node, b1, sizeof *tr);
1216 u8 error1 = vxlan_err_code (ip_err1, udp_err1, csum_err1);
1217 tr->next_index = next1;
1218 tr->error = error1;
1219 tr->tunnel_index = t_index1;
1220 tr->vni = t1->vni;
1221 }
1222 if (b2->flags & VLIB_BUFFER_IS_TRACED)
1223 {
1224 vxlan_rx_trace_t *tr =
1225 vlib_add_trace (vm, node, b2, sizeof *tr);
1226 u8 error2 = vxlan_err_code (ip_err2, udp_err2, csum_err2);
1227 tr->next_index = next2;
1228 tr->error = error2;
1229 tr->tunnel_index = t_index2;
1230 tr->vni = t2->vni;
1231 }
1232 if (b3->flags & VLIB_BUFFER_IS_TRACED)
1233 {
1234 vxlan_rx_trace_t *tr =
1235 vlib_add_trace (vm, node, b3, sizeof *tr);
1236 u8 error3 = vxlan_err_code (ip_err3, udp_err3, csum_err3);
1237 tr->next_index = next3;
1238 tr->error = error3;
1239 tr->tunnel_index = t_index3;
1240 tr->vni = t3->vni;
1241 }
1242 }
1243 vlib_validate_buffer_enqueue_x4
1244 (vm, node, next_index, to_next, n_left_to_next,
1245 bi0, bi1, bi2, bi3, next0, next1, next2, next3);
1246 }
1247 while (n_left_from > 0 && n_left_to_next > 0)
1248 {
1249 u32 bi0 = to_next[0] = from[0];
1250 from++;
1251 n_left_from--;
1252 to_next++;
1253 n_left_to_next--;
1254
1255 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
1256 vlib_buffer_advance (b0, payload_offset);
1257
1258 u16 len0 = vlib_buffer_length_in_chain (vm, b0);
1259 u32 next0 = VXLAN_FLOW_NEXT_L2_INPUT;
1260
1261 u8 ip_err0 = vxlan_check_ip (b0, len0);
1262 u8 udp_err0 = vxlan_check_ip_udp_len (b0);
1263 u8 csum_err0 = vxlan_check_udp_csum (vm, b0);
1264
1265 if (csum_err0)
1266 csum_err0 = !vxlan_validate_udp_csum (vm, b0);
1267 if (ip_err0 || udp_err0 || csum_err0)
1268 {
1269 next0 = VXLAN_FLOW_NEXT_DROP;
1270 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1271 b0->error = node->errors[error0];
1272 }
1273
1274 vnet_update_l2_len (b0);
1275
1276 ASSERT (b0->flow_id != 0);
1277 u32 t_index0 = b0->flow_id - vxm->flow_id_start;
1278 vxlan_tunnel_t *t0 = &vxm->tunnels[t_index0];
1279 b0->flow_id = 0;
1280
1281 u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1282 t0->sw_if_index;
1283 vlib_increment_combined_counter (rx_counter[next0], thread_index,
1284 sw_if_index0, 1, len0);
1285
1286 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1287 {
1288 vxlan_rx_trace_t *tr =
1289 vlib_add_trace (vm, node, b0, sizeof *tr);
1290 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1291 tr->next_index = next0;
1292 tr->error = error0;
1293 tr->tunnel_index = t_index0;
1294 tr->vni = t0->vni;
1295 }
1296 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1297 to_next, n_left_to_next,
1298 bi0, next0);
1299 }
1300
1301 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1302 }
eyal bariaf86a482018-04-17 11:20:27 +03001303
1304 return f->n_vectors;
1305}
1306
1307/* *INDENT-OFF* */
1308#ifndef CLIB_MULTIARCH_VARIANT
1309VLIB_REGISTER_NODE (vxlan4_flow_input_node) = {
1310 .name = "vxlan-flow-input",
eyal bariaf86a482018-04-17 11:20:27 +03001311 .type = VLIB_NODE_TYPE_INTERNAL,
1312 .vector_size = sizeof (u32),
1313
1314 .format_trace = format_vxlan_rx_trace,
1315
1316 .n_errors = VXLAN_FLOW_N_ERROR,
1317 .error_strings = vxlan_flow_error_strings,
1318
1319 .n_next_nodes = VXLAN_FLOW_N_NEXT,
1320 .next_nodes = {
1321#define _(s,n) [VXLAN_FLOW_NEXT_##s] = n,
1322 foreach_vxlan_flow_input_next
1323#undef _
1324 },
1325};
1326#endif
1327/* *INDENT-ON* */
Eyal Bari7d92c092018-07-24 15:15:37 +03001328
1329/*
1330 * fd.io coding-style-patch-verification: ON
1331 *
1332 * Local Variables:
1333 * eval: (c-set-style "gnu")
1334 * End:
1335 */