blob: 764dfca4820e5aefbcebef8bde16a74abd6c7525 [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>
19#include <vnet/pg/pg.h>
20#include <vnet/vxlan/vxlan.h>
21
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
John Lo2b81eb82017-01-30 13:12:10 -050049always_inline u32
Eyal Bari7d92c092018-07-24 15:15:37 +030050buf_fib_index (vlib_buffer_t * b, u32 is_ip4)
John Lo2b81eb82017-01-30 13:12:10 -050051{
Eyal Bari0fa56782018-06-04 12:25:05 +030052 u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
53 if (sw_if_index != (u32) ~ 0)
54 return sw_if_index;
John Lo2b81eb82017-01-30 13:12:10 -050055
Eyal Bari7d92c092018-07-24 15:15:37 +030056 u32 *fib_index_by_sw_if_index = is_ip4 ?
Eyal Barifb663012017-10-19 15:27:51 +030057 ip4_main.fib_index_by_sw_if_index : ip6_main.fib_index_by_sw_if_index;
Eyal Bari0fa56782018-06-04 12:25:05 +030058 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
John Lo2b81eb82017-01-30 13:12:10 -050059
Eyal Bari0fa56782018-06-04 12:25:05 +030060 return vec_elt (fib_index_by_sw_if_index, sw_if_index);
John Lo2b81eb82017-01-30 13:12:10 -050061}
62
Eyal Baridd47eca2018-07-08 08:15:56 +030063typedef vxlan4_tunnel_key_t last_tunnel_cache4;
Eyal Bari0f4b1842018-04-12 12:39:51 +030064
Eyal Bariefd9cf32018-10-02 12:23:06 +030065static const vxlan_decap_info_t decap_not_found = {
66 .sw_if_index = ~0,
67 .next_index = VXLAN_INPUT_NEXT_DROP,
68 .error = VXLAN_ERROR_NO_SUCH_TUNNEL
69};
70
71static const vxlan_decap_info_t decap_bad_flags = {
72 .sw_if_index = ~0,
73 .next_index = VXLAN_INPUT_NEXT_DROP,
74 .error = VXLAN_ERROR_BAD_FLAGS
75};
76
77always_inline vxlan_decap_info_t
Eyal Bari0f4b1842018-04-12 12:39:51 +030078vxlan4_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache4 * cache,
Eyal Bari7d92c092018-07-24 15:15:37 +030079 u32 fib_index, ip4_header_t * ip4_0,
Eyal Bariefd9cf32018-10-02 12:23:06 +030080 vxlan_header_t * vxlan0, u32 * stats_sw_if_index)
Eyal Bari0f4b1842018-04-12 12:39:51 +030081{
Eyal Bariefd9cf32018-10-02 12:23:06 +030082 if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
83 return decap_bad_flags;
Eyal Bari0f4b1842018-04-12 12:39:51 +030084
Eyal Bariefd9cf32018-10-02 12:23:06 +030085 /* Make sure VXLAN tunnel exist according to packet S/D IP, VRF, and VNI */
86 u32 dst = ip4_0->dst_address.as_u32;
87 u32 src = ip4_0->src_address.as_u32;
88 vxlan4_tunnel_key_t key4 = {
89 .key[0] = ((u64) dst << 32) | src,
90 .key[1] = ((u64) fib_index << 32) | vxlan0->vni_reserved,
91 };
92
93 if (PREDICT_TRUE
Eyal Bari9be7c6d2018-10-17 17:13:42 +030094 (key4.key[0] == cache->key[0] && key4.key[1] == cache->key[1]))
Eyal Bari7d92c092018-07-24 15:15:37 +030095 {
Eyal Bariefd9cf32018-10-02 12:23:06 +030096 /* cache hit */
97 vxlan_decap_info_t di = {.as_u64 = cache->value };
98 *stats_sw_if_index = di.sw_if_index;
99 return di;
100 }
Eyal Bari0f4b1842018-04-12 12:39:51 +0300101
Eyal Bariefd9cf32018-10-02 12:23:06 +0300102 int rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
103 if (PREDICT_TRUE (rv == 0))
104 {
Eyal Bari7d92c092018-07-24 15:15:37 +0300105 *cache = key4;
Eyal Bariefd9cf32018-10-02 12:23:06 +0300106 vxlan_decap_info_t di = {.as_u64 = key4.value };
107 *stats_sw_if_index = di.sw_if_index;
108 return di;
Eyal Bari7d92c092018-07-24 15:15:37 +0300109 }
Eyal Bari0f4b1842018-04-12 12:39:51 +0300110
Eyal Bariefd9cf32018-10-02 12:23:06 +0300111 /* try multicast */
112 if (PREDICT_TRUE (!ip4_address_is_multicast (&ip4_0->dst_address)))
113 return decap_not_found;
114
115 /* search for mcast decap info by mcast address */
116 key4.key[0] = dst;
117 rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
118 if (rv != 0)
119 return decap_not_found;
120
121 /* search for unicast tunnel using the mcast tunnel local(src) ip */
122 vxlan_decap_info_t mdi = {.as_u64 = key4.value };
123 key4.key[0] = ((u64) mdi.local_ip.as_u32 << 32) | src;
124 rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
125 if (PREDICT_FALSE (rv != 0))
126 return decap_not_found;
127
128 /* mcast traffic does not update the cache */
129 *stats_sw_if_index = mdi.sw_if_index;
130 vxlan_decap_info_t di = {.as_u64 = key4.value };
131 return di;
Eyal Bari0f4b1842018-04-12 12:39:51 +0300132}
133
Eyal Bari0fa56782018-06-04 12:25:05 +0300134typedef vxlan6_tunnel_key_t last_tunnel_cache6;
135
Eyal Bariefd9cf32018-10-02 12:23:06 +0300136always_inline vxlan_decap_info_t
Eyal Bari0f4b1842018-04-12 12:39:51 +0300137vxlan6_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache6 * cache,
Eyal Bari7d92c092018-07-24 15:15:37 +0300138 u32 fib_index, ip6_header_t * ip6_0,
Eyal Bariefd9cf32018-10-02 12:23:06 +0300139 vxlan_header_t * vxlan0, u32 * stats_sw_if_index)
Eyal Bari0f4b1842018-04-12 12:39:51 +0300140{
Eyal Bariefd9cf32018-10-02 12:23:06 +0300141 if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
142 return decap_bad_flags;
143
Eyal Bari0f4b1842018-04-12 12:39:51 +0300144 /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
Eyal Bari0fa56782018-06-04 12:25:05 +0300145 vxlan6_tunnel_key_t key6 = {
Eyal Bariefd9cf32018-10-02 12:23:06 +0300146 .key[0] = ip6_0->src_address.as_u64[0],
147 .key[1] = ip6_0->src_address.as_u64[1],
148 .key[2] = (((u64) fib_index) << 32) | vxlan0->vni_reserved,
Eyal Bari0f4b1842018-04-12 12:39:51 +0300149 };
150
Eyal Bari7d92c092018-07-24 15:15:37 +0300151 if (PREDICT_FALSE
152 (clib_bihash_key_compare_24_8 (key6.key, cache->key) == 0))
153 {
154 int rv =
155 clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6);
156 if (PREDICT_FALSE (rv != 0))
Eyal Bariefd9cf32018-10-02 12:23:06 +0300157 return decap_not_found;
Eyal Bari0f4b1842018-04-12 12:39:51 +0300158
Eyal Bari7d92c092018-07-24 15:15:37 +0300159 *cache = key6;
160 }
161 vxlan_tunnel_t *t0 = pool_elt_at_index (vxm->tunnels, cache->value);
Eyal Bari0f4b1842018-04-12 12:39:51 +0300162
163 /* Validate VXLAN tunnel SIP against packet DIP */
164 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address, &t0->src.ip6)))
Eyal Bariefd9cf32018-10-02 12:23:06 +0300165 *stats_sw_if_index = t0->sw_if_index;
Eyal Bari0f4b1842018-04-12 12:39:51 +0300166 else
Eyal Bari7d92c092018-07-24 15:15:37 +0300167 {
168 /* try multicast */
169 if (PREDICT_TRUE (!ip6_address_is_multicast (&ip6_0->dst_address)))
Eyal Bariefd9cf32018-10-02 12:23:06 +0300170 return decap_not_found;
Eyal Bari0f4b1842018-04-12 12:39:51 +0300171
Eyal Bari7d92c092018-07-24 15:15:37 +0300172 /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */
173 key6.key[0] = ip6_0->dst_address.as_u64[0];
174 key6.key[1] = ip6_0->dst_address.as_u64[1];
175 int rv =
176 clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6);
177 if (PREDICT_FALSE (rv != 0))
Eyal Bariefd9cf32018-10-02 12:23:06 +0300178 return decap_not_found;
Eyal Bari0fa56782018-06-04 12:25:05 +0300179
Eyal Bariefd9cf32018-10-02 12:23:06 +0300180 vxlan_tunnel_t *mcast_t0 = pool_elt_at_index (vxm->tunnels, key6.value);
181 *stats_sw_if_index = mcast_t0->sw_if_index;
Eyal Bari7d92c092018-07-24 15:15:37 +0300182 }
Eyal Bari0f4b1842018-04-12 12:39:51 +0300183
Eyal Bariefd9cf32018-10-02 12:23:06 +0300184 vxlan_decap_info_t di = {
185 .sw_if_index = t0->sw_if_index,
186 .next_index = t0->decap_next_index,
187 };
188 return di;
Eyal Bari0f4b1842018-04-12 12:39:51 +0300189}
190
Chris Luke99cb3352016-04-26 10:49:53 -0400191always_inline uword
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192vxlan_input (vlib_main_t * vm,
Eyal Bari7d92c092018-07-24 15:15:37 +0300193 vlib_node_runtime_t * node,
194 vlib_frame_t * from_frame, u32 is_ip4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195{
Eyal Bari7d92c092018-07-24 15:15:37 +0300196 vxlan_main_t *vxm = &vxlan_main;
197 vnet_main_t *vnm = vxm->vnet_main;
198 vnet_interface_main_t *im = &vnm->interface_main;
199 vlib_combined_counter_main_t *rx_counter =
200 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX;
Eyal Baridd47eca2018-07-08 08:15:56 +0300201 last_tunnel_cache4 last4;
Eyal Bari0fa56782018-06-04 12:25:05 +0300202 last_tunnel_cache6 last6;
Eyal Baria5679e82018-08-26 15:20:07 +0300203 u32 pkts_dropped = 0;
Eyal Bari7d92c092018-07-24 15:15:37 +0300204 u32 thread_index = vlib_get_thread_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
Dave Barachf9c231e2016-08-05 10:10:18 -0400206 if (is_ip4)
Dave Barachb7b92992018-10-17 10:38:51 -0400207 clib_memset (&last4, 0xff, sizeof last4);
Dave Barachf9c231e2016-08-05 10:10:18 -0400208 else
Dave Barachb7b92992018-10-17 10:38:51 -0400209 clib_memset (&last6, 0xff, sizeof last6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
Eyal Bari7d92c092018-07-24 15:15:37 +0300211 u32 *from = vlib_frame_vector_args (from_frame);
Eyal Barifb663012017-10-19 15:27:51 +0300212 u32 n_left_from = from_frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213
Eyal Baria5679e82018-08-26 15:20:07 +0300214 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
215 vlib_get_buffers (vm, from, bufs, n_left_from);
216
Eyal Bariefd9cf32018-10-02 12:23:06 +0300217 u32 stats_if0 = ~0, stats_if1 = ~0;
Eyal Baria5679e82018-08-26 15:20:07 +0300218 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
219 while (n_left_from >= 4)
220 {
221 /* Prefetch next iteration. */
222 vlib_prefetch_buffer_header (b[2], LOAD);
223 vlib_prefetch_buffer_header (b[3], LOAD);
224
225 /* udp leaves current_data pointing at the vxlan header */
226 void *cur0 = vlib_buffer_get_current (b[0]);
227 void *cur1 = vlib_buffer_get_current (b[1]);
228 vxlan_header_t *vxlan0 = cur0;
229 vxlan_header_t *vxlan1 = cur1;
230
Eyal Baria5679e82018-08-26 15:20:07 +0300231
232 ip4_header_t *ip4_0, *ip4_1;
233 ip6_header_t *ip6_0, *ip6_1;
234 if (is_ip4)
235 {
236 ip4_0 = cur0 - sizeof (udp_header_t) - sizeof (ip4_header_t);
237 ip4_1 = cur1 - sizeof (udp_header_t) - sizeof (ip4_header_t);
238 }
239 else
240 {
241 ip6_0 = cur0 - sizeof (udp_header_t) - sizeof (ip6_header_t);
242 ip6_1 = cur1 - sizeof (udp_header_t) - sizeof (ip6_header_t);
243 }
244
245 /* pop vxlan */
246 vlib_buffer_advance (b[0], sizeof *vxlan0);
247 vlib_buffer_advance (b[1], sizeof *vxlan1);
248
249 u32 fi0 = buf_fib_index (b[0], is_ip4);
250 u32 fi1 = buf_fib_index (b[1], is_ip4);
251
Eyal Bariefd9cf32018-10-02 12:23:06 +0300252 vxlan_decap_info_t di0 = is_ip4 ?
253 vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_if0) :
254 vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_if0);
255 vxlan_decap_info_t di1 = is_ip4 ?
256 vxlan4_find_tunnel (vxm, &last4, fi1, ip4_1, vxlan1, &stats_if1) :
257 vxlan6_find_tunnel (vxm, &last6, fi1, ip6_1, vxlan1, &stats_if1);
Eyal Baria5679e82018-08-26 15:20:07 +0300258
259 /* Prefetch next iteration. */
260 CLIB_PREFETCH (b[2]->data, CLIB_CACHE_LINE_BYTES, LOAD);
261 CLIB_PREFETCH (b[3]->data, CLIB_CACHE_LINE_BYTES, LOAD);
262
263 u32 len0 = vlib_buffer_length_in_chain (vm, b[0]);
264 u32 len1 = vlib_buffer_length_in_chain (vm, b[1]);
265
Eyal Bariefd9cf32018-10-02 12:23:06 +0300266 next[0] = di0.next_index;
267 next[1] = di1.next_index;
Eyal Baria5679e82018-08-26 15:20:07 +0300268
Eyal Bariefd9cf32018-10-02 12:23:06 +0300269 u8 any_error = di0.error | di1.error;
270 if (PREDICT_TRUE (any_error == 0))
271 {
272 /* Required to make the l2 tag push / pop code work on l2 subifs */
273 vnet_update_l2_len (b[0]);
274 vnet_update_l2_len (b[1]);
275 /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
276 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
277 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = di1.sw_if_index;
278 vlib_increment_combined_counter (rx_counter, thread_index,
279 stats_if0, 1, len0);
280 vlib_increment_combined_counter (rx_counter, thread_index,
281 stats_if1, 1, len1);
Eyal Baria5679e82018-08-26 15:20:07 +0300282 }
283 else
284 {
Eyal Bariefd9cf32018-10-02 12:23:06 +0300285 if (di0.error == 0)
Eyal Baria5679e82018-08-26 15:20:07 +0300286 {
Eyal Bariefd9cf32018-10-02 12:23:06 +0300287 vnet_update_l2_len (b[0]);
288 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
289 vlib_increment_combined_counter (rx_counter, thread_index,
290 stats_if0, 1, len0);
Eyal Baria5679e82018-08-26 15:20:07 +0300291 }
Eyal Bariefd9cf32018-10-02 12:23:06 +0300292 else
293 {
294 b[0]->error = node->errors[di0.error];
295 pkts_dropped++;
296 }
Eyal Baria5679e82018-08-26 15:20:07 +0300297
Eyal Bariefd9cf32018-10-02 12:23:06 +0300298 if (di1.error == 0)
299 {
300 vnet_update_l2_len (b[1]);
301 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = di1.sw_if_index;
302 vlib_increment_combined_counter (rx_counter, thread_index,
303 stats_if1, 1, len1);
304 }
305 else
306 {
307 b[1]->error = node->errors[di1.error];
308 pkts_dropped++;
309 }
Eyal Baria5679e82018-08-26 15:20:07 +0300310 }
311
312 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
313 {
314 vxlan_rx_trace_t *tr =
315 vlib_add_trace (vm, node, b[0], sizeof (*tr));
316 tr->next_index = next[0];
Eyal Bariefd9cf32018-10-02 12:23:06 +0300317 tr->error = di0.error;
318 tr->tunnel_index = di0.sw_if_index == ~0 ?
319 ~0 : vxm->tunnel_index_by_sw_if_index[di0.sw_if_index];
Eyal Baria5679e82018-08-26 15:20:07 +0300320 tr->vni = vnet_get_vni (vxlan0);
321 }
322 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
323 {
324 vxlan_rx_trace_t *tr =
325 vlib_add_trace (vm, node, b[1], sizeof (*tr));
326 tr->next_index = next[1];
Eyal Bariefd9cf32018-10-02 12:23:06 +0300327 tr->error = di1.error;
328 tr->tunnel_index = di1.sw_if_index == ~0 ?
329 ~0 : vxm->tunnel_index_by_sw_if_index[di1.sw_if_index];
Eyal Baria5679e82018-08-26 15:20:07 +0300330 tr->vni = vnet_get_vni (vxlan1);
331 }
332 b += 2;
333 next += 2;
334 n_left_from -= 2;
335 }
336
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337 while (n_left_from > 0)
338 {
Eyal Baria5679e82018-08-26 15:20:07 +0300339 /* udp leaves current_data pointing at the vxlan header */
340 void *cur0 = vlib_buffer_get_current (b[0]);
341 vxlan_header_t *vxlan0 = cur0;
Eyal Baria5679e82018-08-26 15:20:07 +0300342 ip4_header_t *ip4_0;
343 ip6_header_t *ip6_0;
344 if (is_ip4)
345 ip4_0 = cur0 - sizeof (udp_header_t) - sizeof (ip4_header_t);
346 else
347 ip6_0 = cur0 - sizeof (udp_header_t) - sizeof (ip6_header_t);
Eyal Barifb663012017-10-19 15:27:51 +0300348
Eyal Baria5679e82018-08-26 15:20:07 +0300349 /* pop (ip, udp, vxlan) */
350 vlib_buffer_advance (b[0], sizeof (*vxlan0));
351
352 u32 fi0 = buf_fib_index (b[0], is_ip4);
353
Eyal Bariefd9cf32018-10-02 12:23:06 +0300354 vxlan_decap_info_t di0 = is_ip4 ?
355 vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_if0) :
356 vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_if0);
Eyal Baria5679e82018-08-26 15:20:07 +0300357
Eyal Baria5679e82018-08-26 15:20:07 +0300358 uword len0 = vlib_buffer_length_in_chain (vm, b[0]);
359
Eyal Bariefd9cf32018-10-02 12:23:06 +0300360 next[0] = di0.next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361
Eyal Bariefd9cf32018-10-02 12:23:06 +0300362 /* Validate VXLAN tunnel encap-fib index against packet */
363 if (di0.error == 0)
364 {
365 /* Required to make the l2 tag push / pop code work on l2 subifs */
366 vnet_update_l2_len (b[0]);
367
368 /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
369 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
370
371 vlib_increment_combined_counter (rx_counter, thread_index,
372 stats_if0, 1, len0);
Eyal Baria5679e82018-08-26 15:20:07 +0300373 }
374 else
375 {
Eyal Bariefd9cf32018-10-02 12:23:06 +0300376 b[0]->error = node->errors[di0.error];
377 pkts_dropped++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378 }
379
Eyal Baria5679e82018-08-26 15:20:07 +0300380 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381 {
Eyal Baria5679e82018-08-26 15:20:07 +0300382 vxlan_rx_trace_t *tr
383 = vlib_add_trace (vm, node, b[0], sizeof (*tr));
384 tr->next_index = next[0];
Eyal Bariefd9cf32018-10-02 12:23:06 +0300385 tr->error = di0.error;
386 tr->tunnel_index = di0.sw_if_index == ~0 ?
387 ~0 : vxm->tunnel_index_by_sw_if_index[di0.sw_if_index];
Eyal Baria5679e82018-08-26 15:20:07 +0300388 tr->vni = vnet_get_vni (vxlan0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389 }
Eyal Baria5679e82018-08-26 15:20:07 +0300390 b += 1;
391 next += 1;
392 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393 }
Eyal Baria5679e82018-08-26 15:20:07 +0300394 vlib_buffer_enqueue_to_next (vm, node, from, nexts, from_frame->n_vectors);
Eyal Barifb663012017-10-19 15:27:51 +0300395 /* Do we still need this now that tunnel tx stats is kept? */
396 u32 node_idx = is_ip4 ? vxlan4_input_node.index : vxlan6_input_node.index;
397 vlib_node_increment_counter (vm, node_idx, VXLAN_ERROR_DECAPSULATED,
Eyal Baria5679e82018-08-26 15:20:07 +0300398 from_frame->n_vectors - pkts_dropped);
Eyal Barifb663012017-10-19 15:27:51 +0300399
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400 return from_frame->n_vectors;
401}
402
Eyal Baria5679e82018-08-26 15:20:07 +0300403VLIB_NODE_FN (vxlan4_input_node) (vlib_main_t * vm,
404 vlib_node_runtime_t * node,
405 vlib_frame_t * from_frame)
Chris Luke99cb3352016-04-26 10:49:53 -0400406{
Eyal Bari7d92c092018-07-24 15:15:37 +0300407 return vxlan_input (vm, node, from_frame, /* is_ip4 */ 1);
Chris Luke99cb3352016-04-26 10:49:53 -0400408}
409
Eyal Baria5679e82018-08-26 15:20:07 +0300410VLIB_NODE_FN (vxlan6_input_node) (vlib_main_t * vm,
411 vlib_node_runtime_t * node,
412 vlib_frame_t * from_frame)
Chris Luke99cb3352016-04-26 10:49:53 -0400413{
Eyal Bari7d92c092018-07-24 15:15:37 +0300414 return vxlan_input (vm, node, from_frame, /* is_ip4 */ 0);
Chris Luke99cb3352016-04-26 10:49:53 -0400415}
416
Eyal Bari7d92c092018-07-24 15:15:37 +0300417static char *vxlan_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418#define vxlan_error(n,s) s,
419#include <vnet/vxlan/vxlan_error.def>
420#undef vxlan_error
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421};
422
Eyal Bari7d92c092018-07-24 15:15:37 +0300423/* *INDENT-OFF* */
424VLIB_REGISTER_NODE (vxlan4_input_node) =
425{
Chris Luke99cb3352016-04-26 10:49:53 -0400426 .name = "vxlan4-input",
Chris Luke99cb3352016-04-26 10:49:53 -0400427 .vector_size = sizeof (u32),
Chris Luke99cb3352016-04-26 10:49:53 -0400428 .n_errors = VXLAN_N_ERROR,
429 .error_strings = vxlan_error_strings,
Chris Luke99cb3352016-04-26 10:49:53 -0400430 .n_next_nodes = VXLAN_INPUT_N_NEXT,
Eyal Bari7d92c092018-07-24 15:15:37 +0300431 .format_trace = format_vxlan_rx_trace,
Chris Luke99cb3352016-04-26 10:49:53 -0400432 .next_nodes = {
433#define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
434 foreach_vxlan_input_next
435#undef _
436 },
Chris Luke99cb3352016-04-26 10:49:53 -0400437};
Damjan Marion1c80e832016-05-11 23:07:18 +0200438
Eyal Bari7d92c092018-07-24 15:15:37 +0300439VLIB_REGISTER_NODE (vxlan6_input_node) =
440{
Chris Luke99cb3352016-04-26 10:49:53 -0400441 .name = "vxlan6-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443 .n_errors = VXLAN_N_ERROR,
444 .error_strings = vxlan_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445 .n_next_nodes = VXLAN_INPUT_N_NEXT,
446 .next_nodes = {
447#define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
448 foreach_vxlan_input_next
449#undef _
450 },
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451 .format_trace = format_vxlan_rx_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452};
Eyal Bari7d92c092018-07-24 15:15:37 +0300453/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +0200454
Eyal Bari7d92c092018-07-24 15:15:37 +0300455typedef enum
456{
John Lo37682e12016-11-30 12:51:39 -0500457 IP_VXLAN_BYPASS_NEXT_DROP,
458 IP_VXLAN_BYPASS_NEXT_VXLAN,
459 IP_VXLAN_BYPASS_N_NEXT,
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700460} ip_vxlan_bypass_next_t;
John Lo37682e12016-11-30 12:51:39 -0500461
462always_inline uword
463ip_vxlan_bypass_inline (vlib_main_t * vm,
John Lo2b81eb82017-01-30 13:12:10 -0500464 vlib_node_runtime_t * node,
Eyal Bari7d92c092018-07-24 15:15:37 +0300465 vlib_frame_t * frame, u32 is_ip4)
John Lo37682e12016-11-30 12:51:39 -0500466{
Eyal Bari7d92c092018-07-24 15:15:37 +0300467 vxlan_main_t *vxm = &vxlan_main;
468 u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
469 vlib_node_runtime_t *error_node =
470 vlib_node_get_runtime (vm, ip4_input_node.index);
471 ip4_address_t addr4; /* last IPv4 address matching a local VTEP address */
472 ip6_address_t addr6; /* last IPv6 address matching a local VTEP address */
John Lo37682e12016-11-30 12:51:39 -0500473
474 from = vlib_frame_vector_args (frame);
475 n_left_from = frame->n_vectors;
476 next_index = node->cached_next_index;
477
478 if (node->flags & VLIB_NODE_FLAG_TRACE)
479 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
480
Eyal Bari7d92c092018-07-24 15:15:37 +0300481 if (is_ip4)
482 addr4.data_u32 = ~0;
483 else
484 ip6_address_set_zero (&addr6);
John Lo37682e12016-11-30 12:51:39 -0500485
486 while (n_left_from > 0)
487 {
488 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
489
490 while (n_left_from >= 4 && n_left_to_next >= 2)
Eyal Bari7d92c092018-07-24 15:15:37 +0300491 {
492 vlib_buffer_t *b0, *b1;
493 ip4_header_t *ip40, *ip41;
494 ip6_header_t *ip60, *ip61;
495 udp_header_t *udp0, *udp1;
496 u32 bi0, ip_len0, udp_len0, flags0, next0;
497 u32 bi1, ip_len1, udp_len1, flags1, next1;
498 i32 len_diff0, len_diff1;
499 u8 error0, good_udp0, proto0;
500 u8 error1, good_udp1, proto1;
John Lo37682e12016-11-30 12:51:39 -0500501
502 /* Prefetch next iteration. */
503 {
Eyal Bari7d92c092018-07-24 15:15:37 +0300504 vlib_buffer_t *p2, *p3;
John Lo37682e12016-11-30 12:51:39 -0500505
506 p2 = vlib_get_buffer (vm, from[2]);
507 p3 = vlib_get_buffer (vm, from[3]);
508
509 vlib_prefetch_buffer_header (p2, LOAD);
510 vlib_prefetch_buffer_header (p3, LOAD);
511
Eyal Bari7d92c092018-07-24 15:15:37 +0300512 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
513 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
John Lo37682e12016-11-30 12:51:39 -0500514 }
515
Eyal Bari7d92c092018-07-24 15:15:37 +0300516 bi0 = to_next[0] = from[0];
517 bi1 = to_next[1] = from[1];
518 from += 2;
519 n_left_from -= 2;
520 to_next += 2;
521 n_left_to_next -= 2;
John Lo37682e12016-11-30 12:51:39 -0500522
523 b0 = vlib_get_buffer (vm, bi0);
524 b1 = vlib_get_buffer (vm, bi1);
John Lo2b81eb82017-01-30 13:12:10 -0500525 if (is_ip4)
526 {
527 ip40 = vlib_buffer_get_current (b0);
528 ip41 = vlib_buffer_get_current (b1);
529 }
530 else
531 {
532 ip60 = vlib_buffer_get_current (b0);
533 ip61 = vlib_buffer_get_current (b1);
534 }
John Lo37682e12016-11-30 12:51:39 -0500535
536 /* Setup packet for next IP feature */
Eyal Bari7d92c092018-07-24 15:15:37 +0300537 vnet_feature_next (&next0, b0);
538 vnet_feature_next (&next1, b1);
John Lo37682e12016-11-30 12:51:39 -0500539
John Lo2b81eb82017-01-30 13:12:10 -0500540 if (is_ip4)
541 {
542 /* Treat IP frag packets as "experimental" protocol for now
Eyal Bari7d92c092018-07-24 15:15:37 +0300543 until support of IP frag reassembly is implemented */
544 proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol;
545 proto1 = ip4_is_fragment (ip41) ? 0xfe : ip41->protocol;
John Lo2b81eb82017-01-30 13:12:10 -0500546 }
547 else
548 {
549 proto0 = ip60->protocol;
550 proto1 = ip61->protocol;
551 }
John Lo37682e12016-11-30 12:51:39 -0500552
553 /* Process packet 0 */
554 if (proto0 != IP_PROTOCOL_UDP)
Eyal Bari7d92c092018-07-24 15:15:37 +0300555 goto exit0; /* not UDP packet */
John Lo37682e12016-11-30 12:51:39 -0500556
John Lo2b81eb82017-01-30 13:12:10 -0500557 if (is_ip4)
558 udp0 = ip4_next_header (ip40);
559 else
560 udp0 = ip6_next_header (ip60);
561
John Lo37682e12016-11-30 12:51:39 -0500562 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
Eyal Bari7d92c092018-07-24 15:15:37 +0300563 goto exit0; /* not VXLAN packet */
John Lo37682e12016-11-30 12:51:39 -0500564
Eyal Bari7d92c092018-07-24 15:15:37 +0300565 /* Validate DIP against VTEPs */
Eyal Bari0f4b1842018-04-12 12:39:51 +0300566 if (is_ip4)
John Lo37682e12016-11-30 12:51:39 -0500567 {
John Lo2b81eb82017-01-30 13:12:10 -0500568 if (addr4.as_u32 != ip40->dst_address.as_u32)
Eyal Bari7d92c092018-07-24 15:15:37 +0300569 {
John Lo2b81eb82017-01-30 13:12:10 -0500570 if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32))
Eyal Bari7d92c092018-07-24 15:15:37 +0300571 goto exit0; /* no local VTEP for VXLAN packet */
John Lo2b81eb82017-01-30 13:12:10 -0500572 addr4 = ip40->dst_address;
Eyal Bari7d92c092018-07-24 15:15:37 +0300573 }
John Lo37682e12016-11-30 12:51:39 -0500574 }
John Lo2b81eb82017-01-30 13:12:10 -0500575 else
576 {
577 if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
Eyal Bari7d92c092018-07-24 15:15:37 +0300578 {
John Lo2b81eb82017-01-30 13:12:10 -0500579 if (!hash_get_mem (vxm->vtep6, &ip60->dst_address))
Eyal Bari7d92c092018-07-24 15:15:37 +0300580 goto exit0; /* no local VTEP for VXLAN packet */
John Lo2b81eb82017-01-30 13:12:10 -0500581 addr6 = ip60->dst_address;
Eyal Bari7d92c092018-07-24 15:15:37 +0300582 }
John Lo2b81eb82017-01-30 13:12:10 -0500583 }
John Lo37682e12016-11-30 12:51:39 -0500584
585 flags0 = b0->flags;
Damjan Marion213b5aa2017-07-13 21:19:27 +0200586 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo37682e12016-11-30 12:51:39 -0500587
588 /* Don't verify UDP checksum for packets with explicit zero checksum. */
589 good_udp0 |= udp0->checksum == 0;
590
591 /* Verify UDP length */
John Lo2b81eb82017-01-30 13:12:10 -0500592 if (is_ip4)
593 ip_len0 = clib_net_to_host_u16 (ip40->length);
594 else
595 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
John Lo37682e12016-11-30 12:51:39 -0500596 udp_len0 = clib_net_to_host_u16 (udp0->length);
John Lo37682e12016-11-30 12:51:39 -0500597 len_diff0 = ip_len0 - udp_len0;
598
599 /* Verify UDP checksum */
600 if (PREDICT_FALSE (!good_udp0))
601 {
Damjan Marion213b5aa2017-07-13 21:19:27 +0200602 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
Eyal Bari7d92c092018-07-24 15:15:37 +0300603 {
John Lo2b81eb82017-01-30 13:12:10 -0500604 if (is_ip4)
605 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
606 else
607 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
608 good_udp0 =
Damjan Marion213b5aa2017-07-13 21:19:27 +0200609 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
Eyal Bari7d92c092018-07-24 15:15:37 +0300610 }
John Lo37682e12016-11-30 12:51:39 -0500611 }
612
John Lo2b81eb82017-01-30 13:12:10 -0500613 if (is_ip4)
614 {
615 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
616 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
617 }
618 else
619 {
620 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
621 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
622 }
John Lo37682e12016-11-30 12:51:39 -0500623
Eyal Bari0f4b1842018-04-12 12:39:51 +0300624 next0 = error0 ?
John Lo37682e12016-11-30 12:51:39 -0500625 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
626 b0->error = error0 ? error_node->errors[error0] : 0;
627
John Lo2b81eb82017-01-30 13:12:10 -0500628 /* vxlan-input node expect current at VXLAN header */
629 if (is_ip4)
Eyal Bari7d92c092018-07-24 15:15:37 +0300630 vlib_buffer_advance (b0,
631 sizeof (ip4_header_t) +
632 sizeof (udp_header_t));
John Lo2b81eb82017-01-30 13:12:10 -0500633 else
Eyal Bari7d92c092018-07-24 15:15:37 +0300634 vlib_buffer_advance (b0,
635 sizeof (ip6_header_t) +
636 sizeof (udp_header_t));
John Lo2b81eb82017-01-30 13:12:10 -0500637
John Lo37682e12016-11-30 12:51:39 -0500638 exit0:
639 /* Process packet 1 */
640 if (proto1 != IP_PROTOCOL_UDP)
Eyal Bari7d92c092018-07-24 15:15:37 +0300641 goto exit1; /* not UDP packet */
John Lo37682e12016-11-30 12:51:39 -0500642
John Lo2b81eb82017-01-30 13:12:10 -0500643 if (is_ip4)
644 udp1 = ip4_next_header (ip41);
645 else
646 udp1 = ip6_next_header (ip61);
647
John Lo37682e12016-11-30 12:51:39 -0500648 if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
Eyal Bari7d92c092018-07-24 15:15:37 +0300649 goto exit1; /* not VXLAN packet */
John Lo37682e12016-11-30 12:51:39 -0500650
Eyal Bari7d92c092018-07-24 15:15:37 +0300651 /* Validate DIP against VTEPs */
Eyal Bari0f4b1842018-04-12 12:39:51 +0300652 if (is_ip4)
John Lo37682e12016-11-30 12:51:39 -0500653 {
John Lo2b81eb82017-01-30 13:12:10 -0500654 if (addr4.as_u32 != ip41->dst_address.as_u32)
Eyal Bari7d92c092018-07-24 15:15:37 +0300655 {
John Lo2b81eb82017-01-30 13:12:10 -0500656 if (!hash_get (vxm->vtep4, ip41->dst_address.as_u32))
Eyal Bari7d92c092018-07-24 15:15:37 +0300657 goto exit1; /* no local VTEP for VXLAN packet */
John Lo2b81eb82017-01-30 13:12:10 -0500658 addr4 = ip41->dst_address;
659 }
John Lo37682e12016-11-30 12:51:39 -0500660 }
John Lo2b81eb82017-01-30 13:12:10 -0500661 else
662 {
663 if (!ip6_address_is_equal (&addr6, &ip61->dst_address))
Eyal Bari7d92c092018-07-24 15:15:37 +0300664 {
John Lo2b81eb82017-01-30 13:12:10 -0500665 if (!hash_get_mem (vxm->vtep6, &ip61->dst_address))
Eyal Bari7d92c092018-07-24 15:15:37 +0300666 goto exit1; /* no local VTEP for VXLAN packet */
John Lo2b81eb82017-01-30 13:12:10 -0500667 addr6 = ip61->dst_address;
668 }
669 }
John Lo37682e12016-11-30 12:51:39 -0500670
671 flags1 = b1->flags;
Damjan Marion213b5aa2017-07-13 21:19:27 +0200672 good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo37682e12016-11-30 12:51:39 -0500673
674 /* Don't verify UDP checksum for packets with explicit zero checksum. */
675 good_udp1 |= udp1->checksum == 0;
676
677 /* Verify UDP length */
John Lo2b81eb82017-01-30 13:12:10 -0500678 if (is_ip4)
679 ip_len1 = clib_net_to_host_u16 (ip41->length);
680 else
681 ip_len1 = clib_net_to_host_u16 (ip61->payload_length);
John Lo37682e12016-11-30 12:51:39 -0500682 udp_len1 = clib_net_to_host_u16 (udp1->length);
John Lo37682e12016-11-30 12:51:39 -0500683 len_diff1 = ip_len1 - udp_len1;
684
685 /* Verify UDP checksum */
686 if (PREDICT_FALSE (!good_udp1))
687 {
Damjan Marion213b5aa2017-07-13 21:19:27 +0200688 if ((flags1 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
Eyal Bari7d92c092018-07-24 15:15:37 +0300689 {
John Lo2b81eb82017-01-30 13:12:10 -0500690 if (is_ip4)
691 flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
692 else
693 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
694 good_udp1 =
Damjan Marion213b5aa2017-07-13 21:19:27 +0200695 (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
Eyal Bari7d92c092018-07-24 15:15:37 +0300696 }
John Lo37682e12016-11-30 12:51:39 -0500697 }
698
John Lo2b81eb82017-01-30 13:12:10 -0500699 if (is_ip4)
700 {
701 error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM;
702 error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH;
703 }
704 else
705 {
Eyal Baria93ea422017-02-01 13:36:15 +0200706 error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM;
707 error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH;
John Lo2b81eb82017-01-30 13:12:10 -0500708 }
John Lo37682e12016-11-30 12:51:39 -0500709
Eyal Bari0f4b1842018-04-12 12:39:51 +0300710 next1 = error1 ?
John Lo37682e12016-11-30 12:51:39 -0500711 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
712 b1->error = error1 ? error_node->errors[error1] : 0;
Eyal Bari0f4b1842018-04-12 12:39:51 +0300713
John Lo2b81eb82017-01-30 13:12:10 -0500714 /* vxlan-input node expect current at VXLAN header */
715 if (is_ip4)
Eyal Bari7d92c092018-07-24 15:15:37 +0300716 vlib_buffer_advance (b1,
717 sizeof (ip4_header_t) +
718 sizeof (udp_header_t));
John Lo2b81eb82017-01-30 13:12:10 -0500719 else
Eyal Bari7d92c092018-07-24 15:15:37 +0300720 vlib_buffer_advance (b1,
721 sizeof (ip6_header_t) +
722 sizeof (udp_header_t));
John Lo2b81eb82017-01-30 13:12:10 -0500723
John Lo37682e12016-11-30 12:51:39 -0500724 exit1:
725 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
726 to_next, n_left_to_next,
727 bi0, bi1, next0, next1);
728 }
729
730 while (n_left_from > 0 && n_left_to_next > 0)
731 {
Eyal Bari7d92c092018-07-24 15:15:37 +0300732 vlib_buffer_t *b0;
733 ip4_header_t *ip40;
734 ip6_header_t *ip60;
735 udp_header_t *udp0;
736 u32 bi0, ip_len0, udp_len0, flags0, next0;
John Lo37682e12016-11-30 12:51:39 -0500737 i32 len_diff0;
738 u8 error0, good_udp0, proto0;
739
740 bi0 = to_next[0] = from[0];
741 from += 1;
742 n_left_from -= 1;
743 to_next += 1;
744 n_left_to_next -= 1;
745
746 b0 = vlib_get_buffer (vm, bi0);
John Lo2b81eb82017-01-30 13:12:10 -0500747 if (is_ip4)
748 ip40 = vlib_buffer_get_current (b0);
749 else
750 ip60 = vlib_buffer_get_current (b0);
John Lo37682e12016-11-30 12:51:39 -0500751
752 /* Setup packet for next IP feature */
Eyal Bari7d92c092018-07-24 15:15:37 +0300753 vnet_feature_next (&next0, b0);
John Lo37682e12016-11-30 12:51:39 -0500754
John Lo2b81eb82017-01-30 13:12:10 -0500755 if (is_ip4)
756 /* Treat IP4 frag packets as "experimental" protocol for now
757 until support of IP frag reassembly is implemented */
Eyal Bari7d92c092018-07-24 15:15:37 +0300758 proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol;
John Lo2b81eb82017-01-30 13:12:10 -0500759 else
760 proto0 = ip60->protocol;
John Lo37682e12016-11-30 12:51:39 -0500761
762 if (proto0 != IP_PROTOCOL_UDP)
Eyal Bari7d92c092018-07-24 15:15:37 +0300763 goto exit; /* not UDP packet */
John Lo37682e12016-11-30 12:51:39 -0500764
John Lo2b81eb82017-01-30 13:12:10 -0500765 if (is_ip4)
766 udp0 = ip4_next_header (ip40);
767 else
768 udp0 = ip6_next_header (ip60);
769
John Lo37682e12016-11-30 12:51:39 -0500770 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
Eyal Bari7d92c092018-07-24 15:15:37 +0300771 goto exit; /* not VXLAN packet */
John Lo37682e12016-11-30 12:51:39 -0500772
Eyal Bari7d92c092018-07-24 15:15:37 +0300773 /* Validate DIP against VTEPs */
Eyal Bari0f4b1842018-04-12 12:39:51 +0300774 if (is_ip4)
John Lo37682e12016-11-30 12:51:39 -0500775 {
John Lo2b81eb82017-01-30 13:12:10 -0500776 if (addr4.as_u32 != ip40->dst_address.as_u32)
Eyal Bari7d92c092018-07-24 15:15:37 +0300777 {
John Lo2b81eb82017-01-30 13:12:10 -0500778 if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32))
Eyal Bari7d92c092018-07-24 15:15:37 +0300779 goto exit; /* no local VTEP for VXLAN packet */
John Lo2b81eb82017-01-30 13:12:10 -0500780 addr4 = ip40->dst_address;
781 }
John Lo37682e12016-11-30 12:51:39 -0500782 }
John Lo2b81eb82017-01-30 13:12:10 -0500783 else
784 {
785 if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
Eyal Bari7d92c092018-07-24 15:15:37 +0300786 {
John Lo2b81eb82017-01-30 13:12:10 -0500787 if (!hash_get_mem (vxm->vtep6, &ip60->dst_address))
Eyal Bari7d92c092018-07-24 15:15:37 +0300788 goto exit; /* no local VTEP for VXLAN packet */
John Lo2b81eb82017-01-30 13:12:10 -0500789 addr6 = ip60->dst_address;
790 }
791 }
John Lo37682e12016-11-30 12:51:39 -0500792
793 flags0 = b0->flags;
Damjan Marion213b5aa2017-07-13 21:19:27 +0200794 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo37682e12016-11-30 12:51:39 -0500795
796 /* Don't verify UDP checksum for packets with explicit zero checksum. */
797 good_udp0 |= udp0->checksum == 0;
798
799 /* Verify UDP length */
John Lo2b81eb82017-01-30 13:12:10 -0500800 if (is_ip4)
801 ip_len0 = clib_net_to_host_u16 (ip40->length);
802 else
803 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
John Lo37682e12016-11-30 12:51:39 -0500804 udp_len0 = clib_net_to_host_u16 (udp0->length);
John Lo37682e12016-11-30 12:51:39 -0500805 len_diff0 = ip_len0 - udp_len0;
806
807 /* Verify UDP checksum */
808 if (PREDICT_FALSE (!good_udp0))
809 {
Damjan Marion213b5aa2017-07-13 21:19:27 +0200810 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
Eyal Bari7d92c092018-07-24 15:15:37 +0300811 {
John Lo2b81eb82017-01-30 13:12:10 -0500812 if (is_ip4)
813 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
814 else
815 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
816 good_udp0 =
Damjan Marion213b5aa2017-07-13 21:19:27 +0200817 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
Eyal Bari7d92c092018-07-24 15:15:37 +0300818 }
John Lo37682e12016-11-30 12:51:39 -0500819 }
820
John Lo2b81eb82017-01-30 13:12:10 -0500821 if (is_ip4)
822 {
823 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
824 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
825 }
826 else
827 {
828 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
829 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
830 }
John Lo37682e12016-11-30 12:51:39 -0500831
Eyal Bari0f4b1842018-04-12 12:39:51 +0300832 next0 = error0 ?
John Lo37682e12016-11-30 12:51:39 -0500833 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
834 b0->error = error0 ? error_node->errors[error0] : 0;
835
John Lo2b81eb82017-01-30 13:12:10 -0500836 /* vxlan-input node expect current at VXLAN header */
837 if (is_ip4)
Eyal Bari7d92c092018-07-24 15:15:37 +0300838 vlib_buffer_advance (b0,
839 sizeof (ip4_header_t) +
840 sizeof (udp_header_t));
John Lo2b81eb82017-01-30 13:12:10 -0500841 else
Eyal Bari7d92c092018-07-24 15:15:37 +0300842 vlib_buffer_advance (b0,
843 sizeof (ip6_header_t) +
844 sizeof (udp_header_t));
John Lo2b81eb82017-01-30 13:12:10 -0500845
John Lo37682e12016-11-30 12:51:39 -0500846 exit:
847 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
848 to_next, n_left_to_next,
849 bi0, next0);
850 }
851
852 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
853 }
854
855 return frame->n_vectors;
856}
857
Eyal Baria5679e82018-08-26 15:20:07 +0300858VLIB_NODE_FN (ip4_vxlan_bypass_node) (vlib_main_t * vm,
859 vlib_node_runtime_t * node,
860 vlib_frame_t * frame)
John Lo37682e12016-11-30 12:51:39 -0500861{
862 return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 1);
863}
864
Eyal Bari7d92c092018-07-24 15:15:37 +0300865/* *INDENT-OFF* */
866VLIB_REGISTER_NODE (ip4_vxlan_bypass_node) =
867{
John Lo37682e12016-11-30 12:51:39 -0500868 .name = "ip4-vxlan-bypass",
869 .vector_size = sizeof (u32),
John Lo37682e12016-11-30 12:51:39 -0500870 .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
871 .next_nodes = {
Eyal Bari7d92c092018-07-24 15:15:37 +0300872 [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
873 [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan4-input",
John Lo37682e12016-11-30 12:51:39 -0500874 },
John Lo37682e12016-11-30 12:51:39 -0500875 .format_buffer = format_ip4_header,
876 .format_trace = format_ip4_forward_next_trace,
877};
878
Eyal Bari7d92c092018-07-24 15:15:37 +0300879/* *INDENT-ON* */
John Lo37682e12016-11-30 12:51:39 -0500880
John Lo37682e12016-11-30 12:51:39 -0500881/* Dummy init function to get us linked in. */
Eyal Baria5679e82018-08-26 15:20:07 +0300882static clib_error_t *
Eyal Bari7d92c092018-07-24 15:15:37 +0300883ip4_vxlan_bypass_init (vlib_main_t * vm)
884{
885 return 0;
886}
John Lo37682e12016-11-30 12:51:39 -0500887
888VLIB_INIT_FUNCTION (ip4_vxlan_bypass_init);
John Lo2b81eb82017-01-30 13:12:10 -0500889
Eyal Baria5679e82018-08-26 15:20:07 +0300890VLIB_NODE_FN (ip6_vxlan_bypass_node) (vlib_main_t * vm,
891 vlib_node_runtime_t * node,
892 vlib_frame_t * frame)
John Lo2b81eb82017-01-30 13:12:10 -0500893{
894 return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 0);
895}
896
Eyal Bari7d92c092018-07-24 15:15:37 +0300897/* *INDENT-OFF* */
898VLIB_REGISTER_NODE (ip6_vxlan_bypass_node) =
899{
John Lo2b81eb82017-01-30 13:12:10 -0500900 .name = "ip6-vxlan-bypass",
901 .vector_size = sizeof (u32),
John Lo2b81eb82017-01-30 13:12:10 -0500902 .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
903 .next_nodes = {
904 [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
905 [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan6-input",
906 },
John Lo2b81eb82017-01-30 13:12:10 -0500907 .format_buffer = format_ip6_header,
908 .format_trace = format_ip6_forward_next_trace,
909};
910
Eyal Bari7d92c092018-07-24 15:15:37 +0300911/* *INDENT-ON* */
John Lo2b81eb82017-01-30 13:12:10 -0500912
913/* Dummy init function to get us linked in. */
Eyal Baria5679e82018-08-26 15:20:07 +0300914static clib_error_t *
Eyal Bari7d92c092018-07-24 15:15:37 +0300915ip6_vxlan_bypass_init (vlib_main_t * vm)
916{
917 return 0;
918}
John Lo2b81eb82017-01-30 13:12:10 -0500919
920VLIB_INIT_FUNCTION (ip6_vxlan_bypass_init);
eyal bariaf86a482018-04-17 11:20:27 +0300921
922#define foreach_vxlan_flow_input_next \
923_(DROP, "error-drop") \
924_(L2_INPUT, "l2-input")
925
926typedef enum
927{
928#define _(s,n) VXLAN_FLOW_NEXT_##s,
929 foreach_vxlan_flow_input_next
930#undef _
Eyal Bari7d92c092018-07-24 15:15:37 +0300931 VXLAN_FLOW_N_NEXT,
eyal bariaf86a482018-04-17 11:20:27 +0300932} vxlan_flow_input_next_t;
933
934#define foreach_vxlan_flow_error \
935 _(NONE, "no error") \
936 _(IP_CHECKSUM_ERROR, "Rx ip checksum errors") \
937 _(IP_HEADER_ERROR, "Rx ip header errors") \
938 _(UDP_CHECKSUM_ERROR, "Rx udp checksum errors") \
939 _(UDP_LENGTH_ERROR, "Rx udp length errors")
940
941typedef enum
942{
943#define _(f,s) VXLAN_FLOW_ERROR_##f,
944 foreach_vxlan_flow_error
945#undef _
946 VXLAN_FLOW_N_ERROR,
947} vxlan_flow_error_t;
948
949static char *vxlan_flow_error_strings[] = {
950#define _(n,s) s,
951 foreach_vxlan_flow_error
952#undef _
953};
954
955
956static_always_inline u8
Eyal Bari7d92c092018-07-24 15:15:37 +0300957vxlan_validate_udp_csum (vlib_main_t * vm, vlib_buffer_t * b)
eyal bariaf86a482018-04-17 11:20:27 +0300958{
959 u32 flags = b->flags;
Eyal Bari7d92c092018-07-24 15:15:37 +0300960 enum
961 { offset =
962 sizeof (ip4_header_t) + sizeof (udp_header_t) + sizeof (vxlan_header_t),
963 };
eyal bariaf86a482018-04-17 11:20:27 +0300964
965 /* Verify UDP checksum */
966 if ((flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
Eyal Bari7d92c092018-07-24 15:15:37 +0300967 {
968 vlib_buffer_advance (b, -offset);
969 flags = ip4_tcp_udp_validate_checksum (vm, b);
970 vlib_buffer_advance (b, offset);
971 }
eyal bariaf86a482018-04-17 11:20:27 +0300972
973 return (flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
974}
975
976static_always_inline u8
Eyal Bari7d92c092018-07-24 15:15:37 +0300977vxlan_check_udp_csum (vlib_main_t * vm, vlib_buffer_t * b)
eyal bariaf86a482018-04-17 11:20:27 +0300978{
Eyal Bari7d92c092018-07-24 15:15:37 +0300979 ip4_vxlan_header_t *hdr = vlib_buffer_get_current (b) - sizeof *hdr;
980 udp_header_t *udp = &hdr->udp;
eyal bariaf86a482018-04-17 11:20:27 +0300981 /* Don't verify UDP checksum for packets with explicit zero checksum. */
982 u8 good_csum = (b->flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0 ||
983 udp->checksum == 0;
984
985 return !good_csum;
986}
987
988static_always_inline u8
Eyal Bari7d92c092018-07-24 15:15:37 +0300989vxlan_check_ip (vlib_buffer_t * b, u16 payload_len)
eyal bariaf86a482018-04-17 11:20:27 +0300990{
Eyal Bari7d92c092018-07-24 15:15:37 +0300991 ip4_vxlan_header_t *hdr = vlib_buffer_get_current (b) - sizeof *hdr;
eyal bariaf86a482018-04-17 11:20:27 +0300992 u16 ip_len = clib_net_to_host_u16 (hdr->ip4.length);
993 u16 expected = payload_len + sizeof *hdr;
Eyal Bari7d92c092018-07-24 15:15:37 +0300994 return ip_len > expected || hdr->ip4.ttl == 0
995 || hdr->ip4.ip_version_and_header_length != 0x45;
eyal bariaf86a482018-04-17 11:20:27 +0300996}
997
998static_always_inline u8
Eyal Bari7d92c092018-07-24 15:15:37 +0300999vxlan_check_ip_udp_len (vlib_buffer_t * b)
eyal bariaf86a482018-04-17 11:20:27 +03001000{
Eyal Bari7d92c092018-07-24 15:15:37 +03001001 ip4_vxlan_header_t *hdr = vlib_buffer_get_current (b) - sizeof *hdr;
eyal bariaf86a482018-04-17 11:20:27 +03001002 u16 ip_len = clib_net_to_host_u16 (hdr->ip4.length);
1003 u16 udp_len = clib_net_to_host_u16 (hdr->udp.length);
1004 return udp_len > ip_len;
1005}
1006
1007static_always_inline u8
1008vxlan_err_code (u8 ip_err0, u8 udp_err0, u8 csum_err0)
1009{
1010 u8 error0 = VXLAN_FLOW_ERROR_NONE;
1011 if (ip_err0)
Eyal Bari7d92c092018-07-24 15:15:37 +03001012 error0 = VXLAN_FLOW_ERROR_IP_HEADER_ERROR;
eyal bariaf86a482018-04-17 11:20:27 +03001013 if (udp_err0)
Eyal Bari7d92c092018-07-24 15:15:37 +03001014 error0 = VXLAN_FLOW_ERROR_UDP_LENGTH_ERROR;
eyal bariaf86a482018-04-17 11:20:27 +03001015 if (csum_err0)
Eyal Bari7d92c092018-07-24 15:15:37 +03001016 error0 = VXLAN_FLOW_ERROR_UDP_CHECKSUM_ERROR;
eyal bariaf86a482018-04-17 11:20:27 +03001017 return error0;
1018}
1019
Eyal Bari93a6f252018-06-14 08:57:39 +03001020VLIB_NODE_FN (vxlan4_flow_input_node) (vlib_main_t * vm,
Eyal Bari7d92c092018-07-24 15:15:37 +03001021 vlib_node_runtime_t * node,
1022 vlib_frame_t * f)
eyal bariaf86a482018-04-17 11:20:27 +03001023{
Eyal Bari7d92c092018-07-24 15:15:37 +03001024 enum
1025 { payload_offset = sizeof (ip4_vxlan_header_t) };
eyal bariaf86a482018-04-17 11:20:27 +03001026
Eyal Bari7d92c092018-07-24 15:15:37 +03001027 vxlan_main_t *vxm = &vxlan_main;
1028 vnet_interface_main_t *im = &vnet_main.interface_main;
1029 vlib_combined_counter_main_t *rx_counter[VXLAN_FLOW_N_NEXT] = {
1030 [VXLAN_FLOW_NEXT_DROP] =
1031 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_DROP,
1032 [VXLAN_FLOW_NEXT_L2_INPUT] =
1033 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
eyal bariaf86a482018-04-17 11:20:27 +03001034 };
Eyal Bari7d92c092018-07-24 15:15:37 +03001035 u32 thread_index = vlib_get_thread_index ();
eyal bariaf86a482018-04-17 11:20:27 +03001036
Eyal Bari7d92c092018-07-24 15:15:37 +03001037 u32 *from = vlib_frame_vector_args (f);
eyal bariaf86a482018-04-17 11:20:27 +03001038 u32 n_left_from = f->n_vectors;
1039 u32 next_index = VXLAN_FLOW_NEXT_L2_INPUT;
1040
1041 while (n_left_from > 0)
eyal bariaf86a482018-04-17 11:20:27 +03001042 {
Eyal Bari7d92c092018-07-24 15:15:37 +03001043 u32 n_left_to_next, *to_next;
eyal bariaf86a482018-04-17 11:20:27 +03001044
Eyal Bari7d92c092018-07-24 15:15:37 +03001045 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
eyal bariaf86a482018-04-17 11:20:27 +03001046
Eyal Bari7d92c092018-07-24 15:15:37 +03001047 while (n_left_from > 3 && n_left_to_next > 3)
eyal bariaf86a482018-04-17 11:20:27 +03001048 {
Eyal Bari7d92c092018-07-24 15:15:37 +03001049 u32 bi0 = to_next[0] = from[0];
1050 u32 bi1 = to_next[1] = from[1];
1051 u32 bi2 = to_next[2] = from[2];
1052 u32 bi3 = to_next[3] = from[3];
1053 from += 4;
1054 n_left_from -= 4;
1055 to_next += 4;
1056 n_left_to_next -= 4;
eyal bariaf86a482018-04-17 11:20:27 +03001057
Eyal Bari7d92c092018-07-24 15:15:37 +03001058 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
1059 vlib_buffer_t *b1 = vlib_get_buffer (vm, bi1);
1060 vlib_buffer_t *b2 = vlib_get_buffer (vm, bi2);
1061 vlib_buffer_t *b3 = vlib_get_buffer (vm, bi3);
1062
1063 vlib_buffer_advance (b0, payload_offset);
1064 vlib_buffer_advance (b1, payload_offset);
1065 vlib_buffer_advance (b2, payload_offset);
1066 vlib_buffer_advance (b3, payload_offset);
1067
1068 u16 len0 = vlib_buffer_length_in_chain (vm, b0);
1069 u16 len1 = vlib_buffer_length_in_chain (vm, b1);
1070 u16 len2 = vlib_buffer_length_in_chain (vm, b2);
1071 u16 len3 = vlib_buffer_length_in_chain (vm, b3);
1072
1073 u32 next0 = VXLAN_FLOW_NEXT_L2_INPUT, next1 =
1074 VXLAN_FLOW_NEXT_L2_INPUT, next2 =
1075 VXLAN_FLOW_NEXT_L2_INPUT, next3 = VXLAN_FLOW_NEXT_L2_INPUT;
1076
1077 u8 ip_err0 = vxlan_check_ip (b0, len0);
1078 u8 ip_err1 = vxlan_check_ip (b1, len1);
1079 u8 ip_err2 = vxlan_check_ip (b2, len2);
1080 u8 ip_err3 = vxlan_check_ip (b3, len3);
1081 u8 ip_err = ip_err0 | ip_err1 | ip_err2 | ip_err3;
1082
1083 u8 udp_err0 = vxlan_check_ip_udp_len (b0);
1084 u8 udp_err1 = vxlan_check_ip_udp_len (b1);
1085 u8 udp_err2 = vxlan_check_ip_udp_len (b2);
1086 u8 udp_err3 = vxlan_check_ip_udp_len (b3);
1087 u8 udp_err = udp_err0 | udp_err1 | udp_err2 | udp_err3;
1088
1089 u8 csum_err0 = vxlan_check_udp_csum (vm, b0);
1090 u8 csum_err1 = vxlan_check_udp_csum (vm, b1);
1091 u8 csum_err2 = vxlan_check_udp_csum (vm, b2);
1092 u8 csum_err3 = vxlan_check_udp_csum (vm, b3);
1093 u8 csum_err = csum_err0 | csum_err1 | csum_err2 | csum_err3;
1094
1095 if (PREDICT_FALSE (csum_err))
1096 {
1097 if (csum_err0)
1098 csum_err0 = !vxlan_validate_udp_csum (vm, b0);
1099 if (csum_err1)
1100 csum_err1 = !vxlan_validate_udp_csum (vm, b1);
1101 if (csum_err2)
1102 csum_err2 = !vxlan_validate_udp_csum (vm, b2);
1103 if (csum_err3)
1104 csum_err3 = !vxlan_validate_udp_csum (vm, b3);
1105 csum_err = csum_err0 | csum_err1 | csum_err2 | csum_err3;
1106 }
1107
1108 if (PREDICT_FALSE (ip_err || udp_err || csum_err))
1109 {
1110 if (ip_err0 || udp_err0 || csum_err0)
1111 {
1112 next0 = VXLAN_FLOW_NEXT_DROP;
1113 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1114 b0->error = node->errors[error0];
1115 }
1116 if (ip_err1 || udp_err1 || csum_err1)
1117 {
1118 next1 = VXLAN_FLOW_NEXT_DROP;
1119 u8 error1 = vxlan_err_code (ip_err1, udp_err1, csum_err1);
1120 b1->error = node->errors[error1];
1121 }
1122 if (ip_err2 || udp_err2 || csum_err2)
1123 {
1124 next2 = VXLAN_FLOW_NEXT_DROP;
1125 u8 error2 = vxlan_err_code (ip_err2, udp_err2, csum_err2);
1126 b2->error = node->errors[error2];
1127 }
1128 if (ip_err3 || udp_err3 || csum_err3)
1129 {
1130 next3 = VXLAN_FLOW_NEXT_DROP;
1131 u8 error3 = vxlan_err_code (ip_err3, udp_err3, csum_err3);
1132 b3->error = node->errors[error3];
1133 }
1134 }
1135
1136 vnet_update_l2_len (b0);
1137 vnet_update_l2_len (b1);
1138 vnet_update_l2_len (b2);
1139 vnet_update_l2_len (b3);
1140
1141 ASSERT (b0->flow_id != 0);
1142 ASSERT (b1->flow_id != 0);
1143 ASSERT (b2->flow_id != 0);
1144 ASSERT (b3->flow_id != 0);
1145
1146 u32 t_index0 = b0->flow_id - vxm->flow_id_start;
1147 u32 t_index1 = b1->flow_id - vxm->flow_id_start;
1148 u32 t_index2 = b2->flow_id - vxm->flow_id_start;
1149 u32 t_index3 = b3->flow_id - vxm->flow_id_start;
1150
1151 vxlan_tunnel_t *t0 = &vxm->tunnels[t_index0];
1152 vxlan_tunnel_t *t1 = &vxm->tunnels[t_index1];
1153 vxlan_tunnel_t *t2 = &vxm->tunnels[t_index2];
1154 vxlan_tunnel_t *t3 = &vxm->tunnels[t_index3];
1155
1156 /* flow id consumed */
1157 b0->flow_id = 0;
1158 b1->flow_id = 0;
1159 b2->flow_id = 0;
1160 b3->flow_id = 0;
1161
1162 u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1163 t0->sw_if_index;
1164 u32 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX] =
1165 t1->sw_if_index;
1166 u32 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_RX] =
1167 t2->sw_if_index;
1168 u32 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_RX] =
1169 t3->sw_if_index;
1170
1171 vlib_increment_combined_counter (rx_counter[next0], thread_index,
1172 sw_if_index0, 1, len0);
1173 vlib_increment_combined_counter (rx_counter[next1], thread_index,
1174 sw_if_index1, 1, len1);
1175 vlib_increment_combined_counter (rx_counter[next2], thread_index,
1176 sw_if_index2, 1, len2);
1177 vlib_increment_combined_counter (rx_counter[next3], thread_index,
1178 sw_if_index3, 1, len3);
1179
1180 u32 flags = b0->flags | b1->flags | b2->flags | b3->flags;
1181
1182 if (PREDICT_FALSE (flags & VLIB_BUFFER_IS_TRACED))
1183 {
1184 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1185 {
1186 vxlan_rx_trace_t *tr =
1187 vlib_add_trace (vm, node, b0, sizeof *tr);
1188 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1189 tr->next_index = next0;
1190 tr->error = error0;
1191 tr->tunnel_index = t_index0;
1192 tr->vni = t0->vni;
1193 }
1194 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1195 {
1196 vxlan_rx_trace_t *tr =
1197 vlib_add_trace (vm, node, b1, sizeof *tr);
1198 u8 error1 = vxlan_err_code (ip_err1, udp_err1, csum_err1);
1199 tr->next_index = next1;
1200 tr->error = error1;
1201 tr->tunnel_index = t_index1;
1202 tr->vni = t1->vni;
1203 }
1204 if (b2->flags & VLIB_BUFFER_IS_TRACED)
1205 {
1206 vxlan_rx_trace_t *tr =
1207 vlib_add_trace (vm, node, b2, sizeof *tr);
1208 u8 error2 = vxlan_err_code (ip_err2, udp_err2, csum_err2);
1209 tr->next_index = next2;
1210 tr->error = error2;
1211 tr->tunnel_index = t_index2;
1212 tr->vni = t2->vni;
1213 }
1214 if (b3->flags & VLIB_BUFFER_IS_TRACED)
1215 {
1216 vxlan_rx_trace_t *tr =
1217 vlib_add_trace (vm, node, b3, sizeof *tr);
1218 u8 error3 = vxlan_err_code (ip_err3, udp_err3, csum_err3);
1219 tr->next_index = next3;
1220 tr->error = error3;
1221 tr->tunnel_index = t_index3;
1222 tr->vni = t3->vni;
1223 }
1224 }
1225 vlib_validate_buffer_enqueue_x4
1226 (vm, node, next_index, to_next, n_left_to_next,
1227 bi0, bi1, bi2, bi3, next0, next1, next2, next3);
1228 }
1229 while (n_left_from > 0 && n_left_to_next > 0)
1230 {
1231 u32 bi0 = to_next[0] = from[0];
1232 from++;
1233 n_left_from--;
1234 to_next++;
1235 n_left_to_next--;
1236
1237 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
1238 vlib_buffer_advance (b0, payload_offset);
1239
1240 u16 len0 = vlib_buffer_length_in_chain (vm, b0);
1241 u32 next0 = VXLAN_FLOW_NEXT_L2_INPUT;
1242
1243 u8 ip_err0 = vxlan_check_ip (b0, len0);
1244 u8 udp_err0 = vxlan_check_ip_udp_len (b0);
1245 u8 csum_err0 = vxlan_check_udp_csum (vm, b0);
1246
1247 if (csum_err0)
1248 csum_err0 = !vxlan_validate_udp_csum (vm, b0);
1249 if (ip_err0 || udp_err0 || csum_err0)
1250 {
1251 next0 = VXLAN_FLOW_NEXT_DROP;
1252 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1253 b0->error = node->errors[error0];
1254 }
1255
1256 vnet_update_l2_len (b0);
1257
1258 ASSERT (b0->flow_id != 0);
1259 u32 t_index0 = b0->flow_id - vxm->flow_id_start;
1260 vxlan_tunnel_t *t0 = &vxm->tunnels[t_index0];
1261 b0->flow_id = 0;
1262
1263 u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1264 t0->sw_if_index;
1265 vlib_increment_combined_counter (rx_counter[next0], thread_index,
1266 sw_if_index0, 1, len0);
1267
1268 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1269 {
1270 vxlan_rx_trace_t *tr =
1271 vlib_add_trace (vm, node, b0, sizeof *tr);
1272 u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0);
1273 tr->next_index = next0;
1274 tr->error = error0;
1275 tr->tunnel_index = t_index0;
1276 tr->vni = t0->vni;
1277 }
1278 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1279 to_next, n_left_to_next,
1280 bi0, next0);
1281 }
1282
1283 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1284 }
eyal bariaf86a482018-04-17 11:20:27 +03001285
1286 return f->n_vectors;
1287}
1288
1289/* *INDENT-OFF* */
1290#ifndef CLIB_MULTIARCH_VARIANT
1291VLIB_REGISTER_NODE (vxlan4_flow_input_node) = {
1292 .name = "vxlan-flow-input",
eyal bariaf86a482018-04-17 11:20:27 +03001293 .type = VLIB_NODE_TYPE_INTERNAL,
1294 .vector_size = sizeof (u32),
1295
1296 .format_trace = format_vxlan_rx_trace,
1297
1298 .n_errors = VXLAN_FLOW_N_ERROR,
1299 .error_strings = vxlan_flow_error_strings,
1300
1301 .n_next_nodes = VXLAN_FLOW_N_NEXT,
1302 .next_nodes = {
1303#define _(s,n) [VXLAN_FLOW_NEXT_##s] = n,
1304 foreach_vxlan_flow_input_next
1305#undef _
1306 },
1307};
1308#endif
1309/* *INDENT-ON* */
Eyal Bari7d92c092018-07-24 15:15:37 +03001310
1311/*
1312 * fd.io coding-style-patch-verification: ON
1313 *
1314 * Local Variables:
1315 * eval: (c-set-style "gnu")
1316 * End:
1317 */