blob: e85e25f43ee03c52f0eaf51fddbaa85578bb3774 [file] [log] [blame]
Marco Varleseb598f1d2017-09-19 14:25:28 +02001/*
2 * Copyright (c) 2017 SUSE LLC.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vlib/vlib.h>
17#include <vnet/pg/pg.h>
18#include <vnet/geneve/geneve.h>
19
Marco Varleseb598f1d2017-09-19 14:25:28 +020020typedef struct
21{
22 u32 next_index;
23 u32 tunnel_index;
24 u32 error;
Marco Varlese60d48bb2017-11-20 09:20:38 +010025 u32 vni_rsvd;
Marco Varleseb598f1d2017-09-19 14:25:28 +020026} geneve_rx_trace_t;
27
28static u8 *
29format_geneve_rx_trace (u8 * s, va_list * args)
30{
31 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33 geneve_rx_trace_t *t = va_arg (*args, geneve_rx_trace_t *);
34
35 if (t->tunnel_index != ~0)
36 {
37 s =
38 format (s,
39 "GENEVE decap from geneve_tunnel%d vni %d next %d error %d",
Marco Varlese60d48bb2017-11-20 09:20:38 +010040 t->tunnel_index, t->vni_rsvd, t->next_index, t->error);
Marco Varleseb598f1d2017-09-19 14:25:28 +020041 }
42 else
43 {
44 s = format (s, "GENEVE decap error - tunnel for vni %d does not exist",
Marco Varlese60d48bb2017-11-20 09:20:38 +010045 t->vni_rsvd);
Marco Varleseb598f1d2017-09-19 14:25:28 +020046 }
47 return s;
48}
49
50always_inline u32
51validate_geneve_fib (vlib_buffer_t * b, geneve_tunnel_t * t, u32 is_ip4)
52{
53 u32 fib_index, sw_if_index;
54
55 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
56
57 if (is_ip4)
58 fib_index = (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
59 vec_elt (ip4_main.fib_index_by_sw_if_index, sw_if_index) :
60 vnet_buffer (b)->sw_if_index[VLIB_TX];
61 else
62 fib_index = (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
63 vec_elt (ip6_main.fib_index_by_sw_if_index, sw_if_index) :
64 vnet_buffer (b)->sw_if_index[VLIB_TX];
65
66 return (fib_index == t->encap_fib_index);
67}
68
69always_inline uword
70geneve_input (vlib_main_t * vm,
71 vlib_node_runtime_t * node,
72 vlib_frame_t * from_frame, u32 is_ip4)
73{
74 u32 n_left_from, next_index, *from, *to_next;
75 geneve_main_t *vxm = &geneve_main;
76 vnet_main_t *vnm = vxm->vnet_main;
77 vnet_interface_main_t *im = &vnm->interface_main;
78 u32 last_tunnel_index = ~0;
79 geneve4_tunnel_key_t last_key4;
80 geneve6_tunnel_key_t last_key6;
81 u32 pkts_decapsulated = 0;
Damjan Marion067cd622018-07-11 12:47:43 +020082 u32 thread_index = vm->thread_index;
Marco Varleseb598f1d2017-09-19 14:25:28 +020083 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
Zhiyong Yangd3ebc832019-05-21 01:46:35 -040084 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Marco Varleseb598f1d2017-09-19 14:25:28 +020085
86 if (is_ip4)
87 last_key4.as_u64 = ~0;
88 else
Dave Barachb7b92992018-10-17 10:38:51 -040089 clib_memset (&last_key6, 0xff, sizeof (last_key6));
Marco Varleseb598f1d2017-09-19 14:25:28 +020090
91 from = vlib_frame_vector_args (from_frame);
92 n_left_from = from_frame->n_vectors;
Zhiyong Yangd3ebc832019-05-21 01:46:35 -040093 vlib_get_buffers (vm, from, bufs, n_left_from);
Marco Varleseb598f1d2017-09-19 14:25:28 +020094
95 next_index = node->cached_next_index;
96 stats_sw_if_index = node->runtime_data[0];
97 stats_n_packets = stats_n_bytes = 0;
98
99 while (n_left_from > 0)
100 {
101 u32 n_left_to_next;
102
103 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
104 while (n_left_from >= 4 && n_left_to_next >= 2)
105 {
106 u32 bi0, bi1;
107 vlib_buffer_t *b0, *b1;
108 u32 next0, next1;
109 ip4_header_t *ip4_0, *ip4_1;
110 ip6_header_t *ip6_0, *ip6_1;
111 geneve_header_t *geneve0, *geneve1;
112 uword *p0, *p1;
113 u32 tunnel_index0, tunnel_index1;
114 geneve_tunnel_t *t0, *t1, *mt0 = NULL, *mt1 = NULL;
115 geneve4_tunnel_key_t key4_0, key4_1;
116 geneve6_tunnel_key_t key6_0, key6_1;
117 u32 error0, error1;
118 u32 sw_if_index0, sw_if_index1, len0, len1;
119
120 /* Prefetch next iteration. */
121 {
Zhiyong Yangd3ebc832019-05-21 01:46:35 -0400122 vlib_prefetch_buffer_header (b[2], LOAD);
123 vlib_prefetch_buffer_header (b[3], LOAD);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200124
Zhiyong Yangd3ebc832019-05-21 01:46:35 -0400125 CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
126 CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200127 }
128
129 bi0 = from[0];
130 bi1 = from[1];
131 to_next[0] = bi0;
132 to_next[1] = bi1;
133 from += 2;
134 to_next += 2;
135 n_left_to_next -= 2;
136 n_left_from -= 2;
137
Zhiyong Yangd3ebc832019-05-21 01:46:35 -0400138 b0 = b[0];
139 b1 = b[1];
140 b += 2;
Marco Varleseb598f1d2017-09-19 14:25:28 +0200141
142 /* udp leaves current_data pointing at the geneve header */
143 geneve0 = vlib_buffer_get_current (b0);
144 geneve1 = vlib_buffer_get_current (b1);
Marco Varlese60d48bb2017-11-20 09:20:38 +0100145
146 vnet_geneve_hdr_1word_ntoh (geneve0);
147 vnet_geneve_hdr_1word_ntoh (geneve1);
148
Marco Varleseb598f1d2017-09-19 14:25:28 +0200149 if (is_ip4)
150 {
151 vlib_buffer_advance
152 (b0, -(word) (sizeof (udp_header_t) + sizeof (ip4_header_t)));
153 vlib_buffer_advance
154 (b1, -(word) (sizeof (udp_header_t) + sizeof (ip4_header_t)));
155 ip4_0 = vlib_buffer_get_current (b0);
156 ip4_1 = vlib_buffer_get_current (b1);
157 }
158 else
159 {
160 vlib_buffer_advance
161 (b0, -(word) (sizeof (udp_header_t) + sizeof (ip6_header_t)));
162 vlib_buffer_advance
163 (b1, -(word) (sizeof (udp_header_t) + sizeof (ip6_header_t)));
164 ip6_0 = vlib_buffer_get_current (b0);
165 ip6_1 = vlib_buffer_get_current (b1);
166 }
167
168 /* pop (ip, udp, geneve) */
169 if (is_ip4)
170 {
171 vlib_buffer_advance
172 (b0,
173 sizeof (*ip4_0) + sizeof (udp_header_t) +
174 GENEVE_BASE_HEADER_LENGTH +
175 vnet_get_geneve_options_len (geneve0));
176 vlib_buffer_advance (b1,
177 sizeof (*ip4_1) + sizeof (udp_header_t) +
178 GENEVE_BASE_HEADER_LENGTH +
179 vnet_get_geneve_options_len (geneve1));
180 }
181 else
182 {
183 vlib_buffer_advance
184 (b0,
185 sizeof (*ip6_0) + sizeof (udp_header_t) +
186 GENEVE_BASE_HEADER_LENGTH +
187 vnet_get_geneve_options_len (geneve0));
188 vlib_buffer_advance (b0,
189 sizeof (*ip6_0) + sizeof (udp_header_t) +
190 GENEVE_BASE_HEADER_LENGTH +
191 vnet_get_geneve_options_len (geneve1));
192 }
193
194 tunnel_index0 = ~0;
195 error0 = 0;
196
197 tunnel_index1 = ~0;
198 error1 = 0;
199
Marco Varlese60d48bb2017-11-20 09:20:38 +0100200 if (PREDICT_FALSE
201 (vnet_get_geneve_version (geneve0) != GENEVE_VERSION))
Marco Varleseb598f1d2017-09-19 14:25:28 +0200202 {
203 error0 = GENEVE_ERROR_BAD_FLAGS;
204 next0 = GENEVE_INPUT_NEXT_DROP;
205 goto trace0;
206 }
Marco Varlese60d48bb2017-11-20 09:20:38 +0100207#if SUPPORT_OPTIONS_HEADER==1
Marco Varleseb598f1d2017-09-19 14:25:28 +0200208 if (PREDICT_FALSE (vnet_get_geneve_critical_bit (geneve0) == 1))
209 {
210 error0 = GENEVE_ERROR_BAD_FLAGS;
211 next0 = GENEVE_INPUT_NEXT_DROP;
212 goto trace0;
213 }
214#endif
215 if (is_ip4)
216 {
217 key4_0.remote = ip4_0->src_address.as_u32;
Marco Varlese60d48bb2017-11-20 09:20:38 +0100218 key4_0.vni = vnet_get_geneve_vni_bigendian (geneve0);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200219
220 /* Make sure GENEVE tunnel exist according to packet SIP and VNI */
221 if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64))
222 {
223 p0 = hash_get (vxm->geneve4_tunnel_by_key, key4_0.as_u64);
224 if (PREDICT_FALSE (p0 == NULL))
225 {
226 error0 = GENEVE_ERROR_NO_SUCH_TUNNEL;
227 next0 = GENEVE_INPUT_NEXT_DROP;
228 goto trace0;
229 }
230 last_key4.as_u64 = key4_0.as_u64;
231 tunnel_index0 = last_tunnel_index = p0[0];
232 }
233 else
234 tunnel_index0 = last_tunnel_index;
235 t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
236
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700237 /* Validate GENEVE tunnel encap-fib index against packet */
Marco Varleseb598f1d2017-09-19 14:25:28 +0200238 if (PREDICT_FALSE (validate_geneve_fib (b0, t0, is_ip4) == 0))
239 {
240 error0 = GENEVE_ERROR_NO_SUCH_TUNNEL;
241 next0 = GENEVE_INPUT_NEXT_DROP;
242 goto trace0;
243 }
244
245 /* Validate GENEVE tunnel SIP against packet DIP */
246 if (PREDICT_TRUE
247 (ip4_0->dst_address.as_u32 == t0->local.ip4.as_u32))
248 goto next0; /* valid packet */
249 if (PREDICT_FALSE
250 (ip4_address_is_multicast (&ip4_0->dst_address)))
251 {
252 key4_0.remote = ip4_0->dst_address.as_u32;
Marco Varlese60d48bb2017-11-20 09:20:38 +0100253 key4_0.vni = vnet_get_geneve_vni_bigendian (geneve0);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200254 /* Make sure mcast GENEVE tunnel exist by packet DIP and VNI */
255 p0 = hash_get (vxm->geneve4_tunnel_by_key, key4_0.as_u64);
256 if (PREDICT_TRUE (p0 != NULL))
257 {
258 mt0 = pool_elt_at_index (vxm->tunnels, p0[0]);
259 goto next0; /* valid packet */
260 }
261 }
262 error0 = GENEVE_ERROR_NO_SUCH_TUNNEL;
263 next0 = GENEVE_INPUT_NEXT_DROP;
264 goto trace0;
265
266 }
267 else /* !is_ip4 */
268 {
269 key6_0.remote.as_u64[0] = ip6_0->src_address.as_u64[0];
270 key6_0.remote.as_u64[1] = ip6_0->src_address.as_u64[1];
Marco Varlese60d48bb2017-11-20 09:20:38 +0100271 key6_0.vni = vnet_get_geneve_vni_bigendian (geneve0);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200272
273 /* Make sure GENEVE tunnel exist according to packet SIP and VNI */
274 if (PREDICT_FALSE
275 (memcmp (&key6_0, &last_key6, sizeof (last_key6)) != 0))
276 {
277 p0 = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6_0);
278 if (PREDICT_FALSE (p0 == NULL))
279 {
280 error0 = GENEVE_ERROR_NO_SUCH_TUNNEL;
281 next0 = GENEVE_INPUT_NEXT_DROP;
282 goto trace0;
283 }
Dave Barach178cf492018-11-13 16:34:13 -0500284 clib_memcpy_fast (&last_key6, &key6_0, sizeof (key6_0));
Marco Varleseb598f1d2017-09-19 14:25:28 +0200285 tunnel_index0 = last_tunnel_index = p0[0];
286 }
287 else
288 tunnel_index0 = last_tunnel_index;
289 t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
290
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700291 /* Validate GENEVE tunnel encap-fib index against packet */
Marco Varleseb598f1d2017-09-19 14:25:28 +0200292 if (PREDICT_FALSE (validate_geneve_fib (b0, t0, is_ip4) == 0))
293 {
294 error0 = GENEVE_ERROR_NO_SUCH_TUNNEL;
295 next0 = GENEVE_INPUT_NEXT_DROP;
296 goto trace0;
297 }
298
299 /* Validate GENEVE tunnel SIP against packet DIP */
300 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address,
301 &t0->local.ip6)))
302 goto next0; /* valid packet */
303 if (PREDICT_FALSE
304 (ip6_address_is_multicast (&ip6_0->dst_address)))
305 {
306 key6_0.remote.as_u64[0] = ip6_0->dst_address.as_u64[0];
307 key6_0.remote.as_u64[1] = ip6_0->dst_address.as_u64[1];
Marco Varlese60d48bb2017-11-20 09:20:38 +0100308 key6_0.vni = vnet_get_geneve_vni_bigendian (geneve0);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200309 p0 = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6_0);
310 if (PREDICT_TRUE (p0 != NULL))
311 {
312 mt0 = pool_elt_at_index (vxm->tunnels, p0[0]);
313 goto next0; /* valid packet */
314 }
315 }
316 error0 = GENEVE_ERROR_NO_SUCH_TUNNEL;
317 next0 = GENEVE_INPUT_NEXT_DROP;
318 goto trace0;
319 }
320
321 next0:
322 next0 = t0->decap_next_index;
323 sw_if_index0 = t0->sw_if_index;
324 len0 = vlib_buffer_length_in_chain (vm, b0);
325
326 /* Required to make the l2 tag push / pop code work on l2 subifs */
327 if (PREDICT_TRUE (next0 == GENEVE_INPUT_NEXT_L2_INPUT))
328 vnet_update_l2_len (b0);
329
330 /* Set packet input sw_if_index to unicast GENEVE tunnel for learning */
331 vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index0;
332 sw_if_index0 = (mt0) ? mt0->sw_if_index : sw_if_index0;
333
334 pkts_decapsulated++;
335 stats_n_packets += 1;
336 stats_n_bytes += len0;
337
338 /* Batch stats increment on the same geneve tunnel so counter
339 is not incremented per packet */
340 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
341 {
342 stats_n_packets -= 1;
343 stats_n_bytes -= len0;
344 if (stats_n_packets)
345 vlib_increment_combined_counter
346 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
347 thread_index, stats_sw_if_index,
348 stats_n_packets, stats_n_bytes);
349 stats_n_packets = 1;
350 stats_n_bytes = len0;
351 stats_sw_if_index = sw_if_index0;
352 }
353
354 trace0:
355 b0->error = error0 ? node->errors[error0] : 0;
356
357 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
358 {
359 geneve_rx_trace_t *tr
360 = vlib_add_trace (vm, node, b0, sizeof (*tr));
361 tr->next_index = next0;
362 tr->error = error0;
363 tr->tunnel_index = tunnel_index0;
Marco Varlese60d48bb2017-11-20 09:20:38 +0100364 tr->vni_rsvd = vnet_get_geneve_vni (geneve0);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200365 }
366
Marco Varlese60d48bb2017-11-20 09:20:38 +0100367 if (PREDICT_FALSE
368 (vnet_get_geneve_version (geneve1) != GENEVE_VERSION))
Marco Varleseb598f1d2017-09-19 14:25:28 +0200369 {
370 error1 = GENEVE_ERROR_BAD_FLAGS;
371 next1 = GENEVE_INPUT_NEXT_DROP;
372 goto trace1;
373 }
Marco Varlese60d48bb2017-11-20 09:20:38 +0100374#if SUPPORT_OPTIONS_HEADER==1
Marco Varleseb598f1d2017-09-19 14:25:28 +0200375 if (PREDICT_FALSE (vnet_get_geneve_critical_bit (geneve1) == 1))
376 {
377 error1 = GENEVE_ERROR_BAD_FLAGS;
378 next1 = GENEVE_INPUT_NEXT_DROP;
379 goto trace1;
380 }
381#endif
382 if (is_ip4)
383 {
384 key4_1.remote = ip4_1->src_address.as_u32;
Marco Varlese60d48bb2017-11-20 09:20:38 +0100385 key4_1.vni = vnet_get_geneve_vni_bigendian (geneve1);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200386
387 /* Make sure unicast GENEVE tunnel exist by packet SIP and VNI */
388 if (PREDICT_FALSE (key4_1.as_u64 != last_key4.as_u64))
389 {
390 p1 = hash_get (vxm->geneve4_tunnel_by_key, key4_1.as_u64);
391 if (PREDICT_FALSE (p1 == NULL))
392 {
393 error1 = GENEVE_ERROR_NO_SUCH_TUNNEL;
394 next1 = GENEVE_INPUT_NEXT_DROP;
395 goto trace1;
396 }
397 last_key4.as_u64 = key4_1.as_u64;
398 tunnel_index1 = last_tunnel_index = p1[0];
399 }
400 else
401 tunnel_index1 = last_tunnel_index;
402 t1 = pool_elt_at_index (vxm->tunnels, tunnel_index1);
403
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700404 /* Validate GENEVE tunnel encap-fib index against packet */
Marco Varleseb598f1d2017-09-19 14:25:28 +0200405 if (PREDICT_FALSE (validate_geneve_fib (b1, t1, is_ip4) == 0))
406 {
407 error1 = GENEVE_ERROR_NO_SUCH_TUNNEL;
408 next1 = GENEVE_INPUT_NEXT_DROP;
409 goto trace1;
410 }
411
412 /* Validate GENEVE tunnel SIP against packet DIP */
413 if (PREDICT_TRUE
414 (ip4_1->dst_address.as_u32 == t1->local.ip4.as_u32))
415 goto next1; /* valid packet */
416 if (PREDICT_FALSE
417 (ip4_address_is_multicast (&ip4_1->dst_address)))
418 {
419 key4_1.remote = ip4_1->dst_address.as_u32;
Marco Varlese60d48bb2017-11-20 09:20:38 +0100420 key4_1.vni = vnet_get_geneve_vni_bigendian (geneve1);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200421 /* Make sure mcast GENEVE tunnel exist by packet DIP and VNI */
422 p1 = hash_get (vxm->geneve4_tunnel_by_key, key4_1.as_u64);
423 if (PREDICT_TRUE (p1 != NULL))
424 {
425 mt1 = pool_elt_at_index (vxm->tunnels, p1[0]);
426 goto next1; /* valid packet */
427 }
428 }
429 error1 = GENEVE_ERROR_NO_SUCH_TUNNEL;
430 next1 = GENEVE_INPUT_NEXT_DROP;
431 goto trace1;
432
433 }
434 else /* !is_ip4 */
435 {
436 key6_1.remote.as_u64[0] = ip6_1->src_address.as_u64[0];
437 key6_1.remote.as_u64[1] = ip6_1->src_address.as_u64[1];
Marco Varlese60d48bb2017-11-20 09:20:38 +0100438 key6_1.vni = vnet_get_geneve_vni_bigendian (geneve1);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200439
440 /* Make sure GENEVE tunnel exist according to packet SIP and VNI */
441 if (PREDICT_FALSE
442 (memcmp (&key6_1, &last_key6, sizeof (last_key6)) != 0))
443 {
444 p1 = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6_1);
445
446 if (PREDICT_FALSE (p1 == NULL))
447 {
448 error1 = GENEVE_ERROR_NO_SUCH_TUNNEL;
449 next1 = GENEVE_INPUT_NEXT_DROP;
450 goto trace1;
451 }
452
Dave Barach178cf492018-11-13 16:34:13 -0500453 clib_memcpy_fast (&last_key6, &key6_1, sizeof (key6_1));
Marco Varleseb598f1d2017-09-19 14:25:28 +0200454 tunnel_index1 = last_tunnel_index = p1[0];
455 }
456 else
457 tunnel_index1 = last_tunnel_index;
458 t1 = pool_elt_at_index (vxm->tunnels, tunnel_index1);
459
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700460 /* Validate GENEVE tunnel encap-fib index against packet */
Marco Varleseb598f1d2017-09-19 14:25:28 +0200461 if (PREDICT_FALSE (validate_geneve_fib (b1, t1, is_ip4) == 0))
462 {
463 error1 = GENEVE_ERROR_NO_SUCH_TUNNEL;
464 next1 = GENEVE_INPUT_NEXT_DROP;
465 goto trace1;
466 }
467
468 /* Validate GENEVE tunnel SIP against packet DIP */
469 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_1->dst_address,
470 &t1->local.ip6)))
471 goto next1; /* valid packet */
472 if (PREDICT_FALSE
473 (ip6_address_is_multicast (&ip6_1->dst_address)))
474 {
475 key6_1.remote.as_u64[0] = ip6_1->dst_address.as_u64[0];
476 key6_1.remote.as_u64[1] = ip6_1->dst_address.as_u64[1];
Marco Varlese60d48bb2017-11-20 09:20:38 +0100477 key6_1.vni = vnet_get_geneve_vni_bigendian (geneve1);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200478 p1 = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6_1);
479 if (PREDICT_TRUE (p1 != NULL))
480 {
481 mt1 = pool_elt_at_index (vxm->tunnels, p1[0]);
482 goto next1; /* valid packet */
483 }
484 }
485 error1 = GENEVE_ERROR_NO_SUCH_TUNNEL;
486 next1 = GENEVE_INPUT_NEXT_DROP;
487 goto trace1;
488 }
489
490 next1:
491 next1 = t1->decap_next_index;
492 sw_if_index1 = t1->sw_if_index;
493 len1 = vlib_buffer_length_in_chain (vm, b1);
494
495 /* Required to make the l2 tag push / pop code work on l2 subifs */
496 if (PREDICT_TRUE (next1 == GENEVE_INPUT_NEXT_L2_INPUT))
497 vnet_update_l2_len (b1);
498
499 /* Set packet input sw_if_index to unicast GENEVE tunnel for learning */
500 vnet_buffer (b1)->sw_if_index[VLIB_RX] = sw_if_index1;
501 sw_if_index1 = (mt1) ? mt1->sw_if_index : sw_if_index1;
502
503 pkts_decapsulated++;
504 stats_n_packets += 1;
505 stats_n_bytes += len1;
506
507 /* Batch stats increment on the same geneve tunnel so counter
508 is not incremented per packet */
509 if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
510 {
511 stats_n_packets -= 1;
512 stats_n_bytes -= len1;
513 if (stats_n_packets)
514 vlib_increment_combined_counter
515 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
516 thread_index, stats_sw_if_index,
517 stats_n_packets, stats_n_bytes);
518 stats_n_packets = 1;
519 stats_n_bytes = len1;
520 stats_sw_if_index = sw_if_index1;
521 }
522
523 trace1:
524 b1->error = error1 ? node->errors[error1] : 0;
525
526 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
527 {
528 geneve_rx_trace_t *tr
529 = vlib_add_trace (vm, node, b1, sizeof (*tr));
530 tr->next_index = next1;
531 tr->error = error1;
532 tr->tunnel_index = tunnel_index1;
Marco Varlese60d48bb2017-11-20 09:20:38 +0100533 tr->vni_rsvd = vnet_get_geneve_vni (geneve1);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200534 }
535
536 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
537 to_next, n_left_to_next,
538 bi0, bi1, next0, next1);
539 }
540
541 while (n_left_from > 0 && n_left_to_next > 0)
542 {
543 u32 bi0;
544 vlib_buffer_t *b0;
545 u32 next0;
546 ip4_header_t *ip4_0;
547 ip6_header_t *ip6_0;
548 geneve_header_t *geneve0;
549 uword *p0;
550 u32 tunnel_index0;
551 geneve_tunnel_t *t0, *mt0 = NULL;
552 geneve4_tunnel_key_t key4_0;
553 geneve6_tunnel_key_t key6_0;
554 u32 error0;
555 u32 sw_if_index0, len0;
556
557 bi0 = from[0];
558 to_next[0] = bi0;
559 from += 1;
560 to_next += 1;
561 n_left_from -= 1;
562 n_left_to_next -= 1;
563
Zhiyong Yangd3ebc832019-05-21 01:46:35 -0400564 b0 = b[0];
565 b += 1;
Marco Varleseb598f1d2017-09-19 14:25:28 +0200566
567 /* udp leaves current_data pointing at the geneve header */
568 geneve0 = vlib_buffer_get_current (b0);
Marco Varlese60d48bb2017-11-20 09:20:38 +0100569 vnet_geneve_hdr_1word_ntoh (geneve0);
570
Marco Varleseb598f1d2017-09-19 14:25:28 +0200571 if (is_ip4)
572 {
573 vlib_buffer_advance
574 (b0, -(word) (sizeof (udp_header_t) + sizeof (ip4_header_t)));
575 ip4_0 = vlib_buffer_get_current (b0);
576 }
577 else
578 {
579 vlib_buffer_advance
580 (b0, -(word) (sizeof (udp_header_t) + sizeof (ip6_header_t)));
581 ip6_0 = vlib_buffer_get_current (b0);
582 }
583
584 /* pop (ip, udp, geneve) */
585 if (is_ip4)
586 {
587 vlib_buffer_advance
588 (b0,
589 sizeof (*ip4_0) + sizeof (udp_header_t) +
590 GENEVE_BASE_HEADER_LENGTH +
591 vnet_get_geneve_options_len (geneve0));
592 }
593 else
594 {
595 vlib_buffer_advance
596 (b0,
597 sizeof (*ip6_0) + sizeof (udp_header_t) +
598 GENEVE_BASE_HEADER_LENGTH +
599 vnet_get_geneve_options_len (geneve0));
600 }
601
602 tunnel_index0 = ~0;
603 error0 = 0;
604
Marco Varlese60d48bb2017-11-20 09:20:38 +0100605 if (PREDICT_FALSE
606 (vnet_get_geneve_version (geneve0) != GENEVE_VERSION))
Marco Varleseb598f1d2017-09-19 14:25:28 +0200607 {
608 error0 = GENEVE_ERROR_BAD_FLAGS;
609 next0 = GENEVE_INPUT_NEXT_DROP;
610 goto trace00;
611 }
Marco Varlese60d48bb2017-11-20 09:20:38 +0100612#if SUPPORT_OPTIONS_HEADER==1
Marco Varleseb598f1d2017-09-19 14:25:28 +0200613 if (PREDICT_FALSE (vnet_get_geneve_critical_bit (geneve0) == 1))
614 {
615 error0 = GENEVE_ERROR_BAD_FLAGS;
616 next0 = GENEVE_INPUT_NEXT_DROP;
617 goto trace00;
618 }
619#endif
620 if (is_ip4)
621 {
622 key4_0.remote = ip4_0->src_address.as_u32;
Marco Varlese60d48bb2017-11-20 09:20:38 +0100623 key4_0.vni = vnet_get_geneve_vni_bigendian (geneve0);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200624
625 /* Make sure unicast GENEVE tunnel exist by packet SIP and VNI */
626 if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64))
627 {
628 p0 = hash_get (vxm->geneve4_tunnel_by_key, key4_0.as_u64);
629 if (PREDICT_FALSE (p0 == NULL))
630 {
631 error0 = GENEVE_ERROR_NO_SUCH_TUNNEL;
632 next0 = GENEVE_INPUT_NEXT_DROP;
633 goto trace00;
634 }
635 last_key4.as_u64 = key4_0.as_u64;
636 tunnel_index0 = last_tunnel_index = p0[0];
637 }
638 else
639 tunnel_index0 = last_tunnel_index;
640 t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
641
642 /* Validate GENEVE tunnel encap-fib index agaist packet */
643 if (PREDICT_FALSE (validate_geneve_fib (b0, t0, is_ip4) == 0))
644 {
645 error0 = GENEVE_ERROR_NO_SUCH_TUNNEL;
646 next0 = GENEVE_INPUT_NEXT_DROP;
647 goto trace00;
648 }
649
650 /* Validate GENEVE tunnel SIP against packet DIP */
651 if (PREDICT_TRUE
652 (ip4_0->dst_address.as_u32 == t0->local.ip4.as_u32))
653 goto next00; /* valid packet */
654 if (PREDICT_FALSE
655 (ip4_address_is_multicast (&ip4_0->dst_address)))
656 {
657 key4_0.remote = ip4_0->dst_address.as_u32;
Marco Varlese60d48bb2017-11-20 09:20:38 +0100658 key4_0.vni = vnet_get_geneve_vni_bigendian (geneve0);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200659 /* Make sure mcast GENEVE tunnel exist by packet DIP and VNI */
660 p0 = hash_get (vxm->geneve4_tunnel_by_key, key4_0.as_u64);
661 if (PREDICT_TRUE (p0 != NULL))
662 {
663 mt0 = pool_elt_at_index (vxm->tunnels, p0[0]);
664 goto next00; /* valid packet */
665 }
666 }
667 error0 = GENEVE_ERROR_NO_SUCH_TUNNEL;
668 next0 = GENEVE_INPUT_NEXT_DROP;
669 goto trace00;
670
671 }
672 else /* !is_ip4 */
673 {
674 key6_0.remote.as_u64[0] = ip6_0->src_address.as_u64[0];
675 key6_0.remote.as_u64[1] = ip6_0->src_address.as_u64[1];
Marco Varlese60d48bb2017-11-20 09:20:38 +0100676 key6_0.vni = vnet_get_geneve_vni_bigendian (geneve0);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200677
678 /* Make sure GENEVE tunnel exist according to packet SIP and VNI */
679 if (PREDICT_FALSE
680 (memcmp (&key6_0, &last_key6, sizeof (last_key6)) != 0))
681 {
682 p0 = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6_0);
683 if (PREDICT_FALSE (p0 == NULL))
684 {
685 error0 = GENEVE_ERROR_NO_SUCH_TUNNEL;
686 next0 = GENEVE_INPUT_NEXT_DROP;
687 goto trace00;
688 }
Dave Barach178cf492018-11-13 16:34:13 -0500689 clib_memcpy_fast (&last_key6, &key6_0, sizeof (key6_0));
Marco Varleseb598f1d2017-09-19 14:25:28 +0200690 tunnel_index0 = last_tunnel_index = p0[0];
691 }
692 else
693 tunnel_index0 = last_tunnel_index;
694 t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
695
696 /* Validate GENEVE tunnel encap-fib index agaist packet */
697 if (PREDICT_FALSE (validate_geneve_fib (b0, t0, is_ip4) == 0))
698 {
699 error0 = GENEVE_ERROR_NO_SUCH_TUNNEL;
700 next0 = GENEVE_INPUT_NEXT_DROP;
701 goto trace00;
702 }
703
704 /* Validate GENEVE tunnel SIP against packet DIP */
705 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address,
706 &t0->local.ip6)))
707 goto next00; /* valid packet */
708 if (PREDICT_FALSE
709 (ip6_address_is_multicast (&ip6_0->dst_address)))
710 {
711 key6_0.remote.as_u64[0] = ip6_0->dst_address.as_u64[0];
712 key6_0.remote.as_u64[1] = ip6_0->dst_address.as_u64[1];
Marco Varlese60d48bb2017-11-20 09:20:38 +0100713 key6_0.vni = vnet_get_geneve_vni_bigendian (geneve0);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200714 p0 = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6_0);
715 if (PREDICT_TRUE (p0 != NULL))
716 {
717 mt0 = pool_elt_at_index (vxm->tunnels, p0[0]);
718 goto next00; /* valid packet */
719 }
720 }
721 error0 = GENEVE_ERROR_NO_SUCH_TUNNEL;
722 next0 = GENEVE_INPUT_NEXT_DROP;
723 goto trace00;
724 }
725
726 next00:
727 next0 = t0->decap_next_index;
728 sw_if_index0 = t0->sw_if_index;
729 len0 = vlib_buffer_length_in_chain (vm, b0);
730
731 /* Required to make the l2 tag push / pop code work on l2 subifs */
732 if (PREDICT_TRUE (next0 == GENEVE_INPUT_NEXT_L2_INPUT))
733 vnet_update_l2_len (b0);
734
735 /* Set packet input sw_if_index to unicast GENEVE tunnel for learning */
736 vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index0;
737 sw_if_index0 = (mt0) ? mt0->sw_if_index : sw_if_index0;
738
739 pkts_decapsulated++;
740 stats_n_packets += 1;
741 stats_n_bytes += len0;
742
743 /* Batch stats increment on the same geneve tunnel so counter
744 is not incremented per packet */
745 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
746 {
747 stats_n_packets -= 1;
748 stats_n_bytes -= len0;
749 if (stats_n_packets)
750 vlib_increment_combined_counter
751 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
752 thread_index, stats_sw_if_index,
753 stats_n_packets, stats_n_bytes);
754 stats_n_packets = 1;
755 stats_n_bytes = len0;
756 stats_sw_if_index = sw_if_index0;
757 }
758
759 trace00:
760 b0->error = error0 ? node->errors[error0] : 0;
761
762 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
763 {
764 geneve_rx_trace_t *tr
765 = vlib_add_trace (vm, node, b0, sizeof (*tr));
766 tr->next_index = next0;
767 tr->error = error0;
768 tr->tunnel_index = tunnel_index0;
Marco Varlese60d48bb2017-11-20 09:20:38 +0100769 tr->vni_rsvd = vnet_get_geneve_vni (geneve0);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200770 }
771 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
772 to_next, n_left_to_next,
773 bi0, next0);
774 }
775
776 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
777 }
778 /* Do we still need this now that tunnel tx stats is kept? */
779 vlib_node_increment_counter (vm, is_ip4 ?
780 geneve4_input_node.
781 index : geneve6_input_node.index,
782 GENEVE_ERROR_DECAPSULATED, pkts_decapsulated);
783
784 /* Increment any remaining batch stats */
785 if (stats_n_packets)
786 {
787 vlib_increment_combined_counter
788 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
789 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
790 node->runtime_data[0] = stats_sw_if_index;
791 }
792
793 return from_frame->n_vectors;
794}
795
Filip Tehlar55333d72019-03-05 00:36:04 -0800796VLIB_NODE_FN (geneve4_input_node) (vlib_main_t * vm,
797 vlib_node_runtime_t * node,
798 vlib_frame_t * from_frame)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200799{
800 return geneve_input (vm, node, from_frame, /* is_ip4 */ 1);
801}
802
Filip Tehlar55333d72019-03-05 00:36:04 -0800803VLIB_NODE_FN (geneve6_input_node) (vlib_main_t * vm,
804 vlib_node_runtime_t * node,
805 vlib_frame_t * from_frame)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200806{
807 return geneve_input (vm, node, from_frame, /* is_ip4 */ 0);
808}
809
810static char *geneve_error_strings[] = {
811#define geneve_error(n,s) s,
812#include <vnet/geneve/geneve_error.def>
813#undef geneve_error
814#undef _
815};
816
817/* *INDENT-OFF* */
818VLIB_REGISTER_NODE (geneve4_input_node) = {
Marco Varleseb598f1d2017-09-19 14:25:28 +0200819 .name = "geneve4-input",
820 /* Takes a vector of packets. */
821 .vector_size = sizeof (u32),
822 .n_errors = GENEVE_N_ERROR,
823 .error_strings = geneve_error_strings,
824 .n_next_nodes = GENEVE_INPUT_N_NEXT,
825 .next_nodes = {
826#define _(s,n) [GENEVE_INPUT_NEXT_##s] = n,
827 foreach_geneve_input_next
828#undef _
829 },
830
831//temp .format_buffer = format_geneve_header,
832 .format_trace = format_geneve_rx_trace,
833 // $$$$ .unformat_buffer = unformat_geneve_header,
834};
835
Marco Varleseb598f1d2017-09-19 14:25:28 +0200836VLIB_REGISTER_NODE (geneve6_input_node) = {
Marco Varleseb598f1d2017-09-19 14:25:28 +0200837 .name = "geneve6-input",
838 /* Takes a vector of packets. */
839 .vector_size = sizeof (u32),
840 .n_errors = GENEVE_N_ERROR,
841 .error_strings = geneve_error_strings,
842 .n_next_nodes = GENEVE_INPUT_N_NEXT,
843 .next_nodes = {
844#define _(s,n) [GENEVE_INPUT_NEXT_##s] = n,
845 foreach_geneve_input_next
846#undef _
847 },
848//temp .format_buffer = format_geneve_header,
849 .format_trace = format_geneve_rx_trace,
850 // $$$$ .unformat_buffer = unformat_geneve_header,
851};
Marco Varleseb598f1d2017-09-19 14:25:28 +0200852/* *INDENT-ON* */
853
854typedef enum
855{
856 IP_GENEVE_BYPASS_NEXT_DROP,
857 IP_GENEVE_BYPASS_NEXT_GENEVE,
858 IP_GENEVE_BYPASS_N_NEXT,
859} ip_vxan_bypass_next_t;
860
861always_inline uword
862ip_geneve_bypass_inline (vlib_main_t * vm,
863 vlib_node_runtime_t * node,
864 vlib_frame_t * frame, u32 is_ip4)
865{
866 geneve_main_t *vxm = &geneve_main;
867 u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
868 vlib_node_runtime_t *error_node =
869 vlib_node_get_runtime (vm, ip4_input_node.index);
870 ip4_address_t addr4; /* last IPv4 address matching a local VTEP address */
871 ip6_address_t addr6; /* last IPv6 address matching a local VTEP address */
872
873 from = vlib_frame_vector_args (frame);
874 n_left_from = frame->n_vectors;
875 next_index = node->cached_next_index;
876
877 if (node->flags & VLIB_NODE_FLAG_TRACE)
878 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
879
880 if (is_ip4)
881 addr4.data_u32 = ~0;
882 else
883 ip6_address_set_zero (&addr6);
884
885 while (n_left_from > 0)
886 {
887 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
888
889 while (n_left_from >= 4 && n_left_to_next >= 2)
890 {
891 vlib_buffer_t *b0, *b1;
892 ip4_header_t *ip40, *ip41;
893 ip6_header_t *ip60, *ip61;
894 udp_header_t *udp0, *udp1;
895 u32 bi0, ip_len0, udp_len0, flags0, next0;
896 u32 bi1, ip_len1, udp_len1, flags1, next1;
897 i32 len_diff0, len_diff1;
898 u8 error0, good_udp0, proto0;
899 u8 error1, good_udp1, proto1;
900
901 /* Prefetch next iteration. */
902 {
903 vlib_buffer_t *p2, *p3;
904
905 p2 = vlib_get_buffer (vm, from[2]);
906 p3 = vlib_get_buffer (vm, from[3]);
907
908 vlib_prefetch_buffer_header (p2, LOAD);
909 vlib_prefetch_buffer_header (p3, LOAD);
910
911 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
912 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
913 }
914
915 bi0 = to_next[0] = from[0];
916 bi1 = to_next[1] = from[1];
917 from += 2;
918 n_left_from -= 2;
919 to_next += 2;
920 n_left_to_next -= 2;
921
922 b0 = vlib_get_buffer (vm, bi0);
923 b1 = vlib_get_buffer (vm, bi1);
924 if (is_ip4)
925 {
926 ip40 = vlib_buffer_get_current (b0);
927 ip41 = vlib_buffer_get_current (b1);
928 }
929 else
930 {
931 ip60 = vlib_buffer_get_current (b0);
932 ip61 = vlib_buffer_get_current (b1);
933 }
934
935 /* Setup packet for next IP feature */
Damjan Marion7d98a122018-07-19 20:42:08 +0200936 vnet_feature_next (&next0, b0);
937 vnet_feature_next (&next1, b1);
Marco Varleseb598f1d2017-09-19 14:25:28 +0200938
939 if (is_ip4)
940 {
941 /* Treat IP frag packets as "experimental" protocol for now
942 until support of IP frag reassembly is implemented */
943 proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol;
944 proto1 = ip4_is_fragment (ip41) ? 0xfe : ip41->protocol;
945 }
946 else
947 {
948 proto0 = ip60->protocol;
949 proto1 = ip61->protocol;
950 }
951
952 /* Process packet 0 */
953 if (proto0 != IP_PROTOCOL_UDP)
954 goto exit0; /* not UDP packet */
955
956 if (is_ip4)
957 udp0 = ip4_next_header (ip40);
958 else
959 udp0 = ip6_next_header (ip60);
960
961 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_geneve))
962 goto exit0; /* not GENEVE packet */
963
964 /* Validate DIP against VTEPs */
965 if (is_ip4)
966 {
967 if (addr4.as_u32 != ip40->dst_address.as_u32)
968 {
969 if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32))
970 goto exit0; /* no local VTEP for GENEVE packet */
971 addr4 = ip40->dst_address;
972 }
973 }
974 else
975 {
976 if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
977 {
978 if (!hash_get_mem (vxm->vtep6, &ip60->dst_address))
979 goto exit0; /* no local VTEP for GENEVE packet */
980 addr6 = ip60->dst_address;
981 }
982 }
983
984 flags0 = b0->flags;
985 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
986
987 /* Don't verify UDP checksum for packets with explicit zero checksum. */
988 good_udp0 |= udp0->checksum == 0;
989
990 /* Verify UDP length */
991 if (is_ip4)
992 ip_len0 = clib_net_to_host_u16 (ip40->length);
993 else
994 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
995 udp_len0 = clib_net_to_host_u16 (udp0->length);
996 len_diff0 = ip_len0 - udp_len0;
997
998 /* Verify UDP checksum */
999 if (PREDICT_FALSE (!good_udp0))
1000 {
1001 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
1002 {
1003 if (is_ip4)
1004 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
1005 else
1006 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
1007 good_udp0 =
1008 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1009 }
1010 }
1011
1012 if (is_ip4)
1013 {
1014 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
1015 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
1016 }
1017 else
1018 {
1019 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
1020 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
1021 }
1022
1023 next0 = error0 ?
1024 IP_GENEVE_BYPASS_NEXT_DROP : IP_GENEVE_BYPASS_NEXT_GENEVE;
1025 b0->error = error0 ? error_node->errors[error0] : 0;
1026
1027 /* geneve-input node expect current at GENEVE header */
1028 if (is_ip4)
1029 vlib_buffer_advance (b0,
1030 sizeof (ip4_header_t) +
1031 sizeof (udp_header_t));
1032 else
1033 vlib_buffer_advance (b0,
1034 sizeof (ip6_header_t) +
1035 sizeof (udp_header_t));
1036
1037 exit0:
1038 /* Process packet 1 */
1039 if (proto1 != IP_PROTOCOL_UDP)
1040 goto exit1; /* not UDP packet */
1041
1042 if (is_ip4)
1043 udp1 = ip4_next_header (ip41);
1044 else
1045 udp1 = ip6_next_header (ip61);
1046
1047 if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_geneve))
1048 goto exit1; /* not GENEVE packet */
1049
1050 /* Validate DIP against VTEPs */
1051 if (is_ip4)
1052 {
1053 if (addr4.as_u32 != ip41->dst_address.as_u32)
1054 {
1055 if (!hash_get (vxm->vtep4, ip41->dst_address.as_u32))
1056 goto exit1; /* no local VTEP for GENEVE packet */
1057 addr4 = ip41->dst_address;
1058 }
1059 }
1060 else
1061 {
1062 if (!ip6_address_is_equal (&addr6, &ip61->dst_address))
1063 {
1064 if (!hash_get_mem (vxm->vtep6, &ip61->dst_address))
1065 goto exit1; /* no local VTEP for GENEVE packet */
1066 addr6 = ip61->dst_address;
1067 }
1068 }
1069
1070 flags1 = b1->flags;
1071 good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1072
1073 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1074 good_udp1 |= udp1->checksum == 0;
1075
1076 /* Verify UDP length */
1077 if (is_ip4)
1078 ip_len1 = clib_net_to_host_u16 (ip41->length);
1079 else
1080 ip_len1 = clib_net_to_host_u16 (ip61->payload_length);
1081 udp_len1 = clib_net_to_host_u16 (udp1->length);
1082 len_diff1 = ip_len1 - udp_len1;
1083
1084 /* Verify UDP checksum */
1085 if (PREDICT_FALSE (!good_udp1))
1086 {
1087 if ((flags1 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
1088 {
1089 if (is_ip4)
1090 flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
1091 else
1092 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
1093 good_udp1 =
1094 (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1095 }
1096 }
1097
1098 if (is_ip4)
1099 {
1100 error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM;
1101 error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH;
1102 }
1103 else
1104 {
1105 error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM;
1106 error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH;
1107 }
1108
1109 next1 = error1 ?
1110 IP_GENEVE_BYPASS_NEXT_DROP : IP_GENEVE_BYPASS_NEXT_GENEVE;
1111 b1->error = error1 ? error_node->errors[error1] : 0;
1112
1113 /* geneve-input node expect current at GENEVE header */
1114 if (is_ip4)
1115 vlib_buffer_advance (b1,
1116 sizeof (ip4_header_t) +
1117 sizeof (udp_header_t));
1118 else
1119 vlib_buffer_advance (b1,
1120 sizeof (ip6_header_t) +
1121 sizeof (udp_header_t));
1122
1123 exit1:
1124 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1125 to_next, n_left_to_next,
1126 bi0, bi1, next0, next1);
1127 }
1128
1129 while (n_left_from > 0 && n_left_to_next > 0)
1130 {
1131 vlib_buffer_t *b0;
1132 ip4_header_t *ip40;
1133 ip6_header_t *ip60;
1134 udp_header_t *udp0;
1135 u32 bi0, ip_len0, udp_len0, flags0, next0;
1136 i32 len_diff0;
1137 u8 error0, good_udp0, proto0;
1138
1139 bi0 = to_next[0] = from[0];
1140 from += 1;
1141 n_left_from -= 1;
1142 to_next += 1;
1143 n_left_to_next -= 1;
1144
1145 b0 = vlib_get_buffer (vm, bi0);
1146 if (is_ip4)
1147 ip40 = vlib_buffer_get_current (b0);
1148 else
1149 ip60 = vlib_buffer_get_current (b0);
1150
1151 /* Setup packet for next IP feature */
Damjan Marion7d98a122018-07-19 20:42:08 +02001152 vnet_feature_next (&next0, b0);
Marco Varleseb598f1d2017-09-19 14:25:28 +02001153
1154 if (is_ip4)
1155 /* Treat IP4 frag packets as "experimental" protocol for now
1156 until support of IP frag reassembly is implemented */
1157 proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol;
1158 else
1159 proto0 = ip60->protocol;
1160
1161 if (proto0 != IP_PROTOCOL_UDP)
1162 goto exit; /* not UDP packet */
1163
1164 if (is_ip4)
1165 udp0 = ip4_next_header (ip40);
1166 else
1167 udp0 = ip6_next_header (ip60);
1168
1169 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_geneve))
1170 goto exit; /* not GENEVE packet */
1171
1172 /* Validate DIP against VTEPs */
1173 if (is_ip4)
1174 {
1175 if (addr4.as_u32 != ip40->dst_address.as_u32)
1176 {
1177 if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32))
1178 goto exit; /* no local VTEP for GENEVE packet */
1179 addr4 = ip40->dst_address;
1180 }
1181 }
1182 else
1183 {
1184 if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
1185 {
1186 if (!hash_get_mem (vxm->vtep6, &ip60->dst_address))
1187 goto exit; /* no local VTEP for GENEVE packet */
1188 addr6 = ip60->dst_address;
1189 }
1190 }
1191
1192 flags0 = b0->flags;
1193 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1194
1195 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1196 good_udp0 |= udp0->checksum == 0;
1197
1198 /* Verify UDP length */
1199 if (is_ip4)
1200 ip_len0 = clib_net_to_host_u16 (ip40->length);
1201 else
1202 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
1203 udp_len0 = clib_net_to_host_u16 (udp0->length);
1204 len_diff0 = ip_len0 - udp_len0;
1205
1206 /* Verify UDP checksum */
1207 if (PREDICT_FALSE (!good_udp0))
1208 {
1209 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
1210 {
1211 if (is_ip4)
1212 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
1213 else
1214 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
1215 good_udp0 =
1216 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1217 }
1218 }
1219
1220 if (is_ip4)
1221 {
1222 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
1223 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
1224 }
1225 else
1226 {
1227 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
1228 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
1229 }
1230
1231 next0 = error0 ?
1232 IP_GENEVE_BYPASS_NEXT_DROP : IP_GENEVE_BYPASS_NEXT_GENEVE;
1233 b0->error = error0 ? error_node->errors[error0] : 0;
1234
1235 /* geneve-input node expect current at GENEVE header */
1236 if (is_ip4)
1237 vlib_buffer_advance (b0,
1238 sizeof (ip4_header_t) +
1239 sizeof (udp_header_t));
1240 else
1241 vlib_buffer_advance (b0,
1242 sizeof (ip6_header_t) +
1243 sizeof (udp_header_t));
1244
1245 exit:
1246 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1247 to_next, n_left_to_next,
1248 bi0, next0);
1249 }
1250
1251 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1252 }
1253
1254 return frame->n_vectors;
1255}
1256
Filip Tehlar55333d72019-03-05 00:36:04 -08001257VLIB_NODE_FN (ip4_geneve_bypass_node) (vlib_main_t * vm,
1258 vlib_node_runtime_t * node,
1259 vlib_frame_t * frame)
Marco Varleseb598f1d2017-09-19 14:25:28 +02001260{
1261 return ip_geneve_bypass_inline (vm, node, frame, /* is_ip4 */ 1);
1262}
1263
1264/* *INDENT-OFF* */
1265VLIB_REGISTER_NODE (ip4_geneve_bypass_node) =
1266{
Filip Tehlar55333d72019-03-05 00:36:04 -08001267 .name = "ip4-geneve-bypass",.vector_size =
Marco Varleseb598f1d2017-09-19 14:25:28 +02001268 sizeof (u32),.n_next_nodes = IP_GENEVE_BYPASS_N_NEXT,.next_nodes =
1269 {
1270 [IP_GENEVE_BYPASS_NEXT_DROP] = "error-drop",
1271 [IP_GENEVE_BYPASS_NEXT_GENEVE] = "geneve4-input",}
1272,.format_buffer = format_ip4_header,.format_trace =
1273 format_ip4_forward_next_trace,};
1274
Filip Tehlar55333d72019-03-05 00:36:04 -08001275#ifndef CLIB_MARCH_VARIANT
Marco Varleseb598f1d2017-09-19 14:25:28 +02001276/* Dummy init function to get us linked in. */
1277 clib_error_t *ip4_geneve_bypass_init (vlib_main_t * vm)
1278{
1279 return 0;
1280}
1281
1282VLIB_INIT_FUNCTION (ip4_geneve_bypass_init);
1283/* *INDENT-ON* */
Filip Tehlar55333d72019-03-05 00:36:04 -08001284#endif /* CLIB_MARCH_VARIANT */
Marco Varleseb598f1d2017-09-19 14:25:28 +02001285
Filip Tehlar55333d72019-03-05 00:36:04 -08001286VLIB_NODE_FN (ip6_geneve_bypass_node) (vlib_main_t * vm,
1287 vlib_node_runtime_t * node,
1288 vlib_frame_t * frame)
Marco Varleseb598f1d2017-09-19 14:25:28 +02001289{
1290 return ip_geneve_bypass_inline (vm, node, frame, /* is_ip4 */ 0);
1291}
1292
1293/* *INDENT-OFF* */
1294VLIB_REGISTER_NODE (ip6_geneve_bypass_node) =
1295{
Filip Tehlar55333d72019-03-05 00:36:04 -08001296 .name = "ip6-geneve-bypass",.vector_size =
Marco Varleseb598f1d2017-09-19 14:25:28 +02001297 sizeof (u32),.n_next_nodes = IP_GENEVE_BYPASS_N_NEXT,.next_nodes =
1298 {
1299 [IP_GENEVE_BYPASS_NEXT_DROP] = "error-drop",
1300 [IP_GENEVE_BYPASS_NEXT_GENEVE] = "geneve6-input",}
1301,.format_buffer = format_ip6_header,.format_trace =
1302 format_ip6_forward_next_trace,};
1303/* *INDENT-ON* */
1304
Filip Tehlar55333d72019-03-05 00:36:04 -08001305#ifndef CLIB_MARCH_VARIANT
Marco Varleseb598f1d2017-09-19 14:25:28 +02001306/* Dummy init function to get us linked in. */
Filip Tehlar55333d72019-03-05 00:36:04 -08001307clib_error_t *
1308ip6_geneve_bypass_init (vlib_main_t * vm)
Marco Varleseb598f1d2017-09-19 14:25:28 +02001309{
1310 return 0;
1311}
1312
1313VLIB_INIT_FUNCTION (ip6_geneve_bypass_init);
Filip Tehlar55333d72019-03-05 00:36:04 -08001314#endif /* CLIB_MARCH_VARIANT */
Marco Varleseb598f1d2017-09-19 14:25:28 +02001315
1316/*
1317 * fd.io coding-style-patch-verification: ON
1318 *
1319 * Local Variables:
1320 * eval: (c-set-style "gnu")
1321 * End:
1322 */