blob: a26df9df15fb436a268795625a36b9c46b2df023 [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
John Lo13e3d452016-08-09 19:20:51 -040022vlib_node_registration_t vxlan4_input_node;
23vlib_node_registration_t vxlan6_input_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -070024
25typedef struct {
26 u32 next_index;
27 u32 tunnel_index;
28 u32 error;
29 u32 vni;
30} vxlan_rx_trace_t;
31
32static u8 * format_vxlan_rx_trace (u8 * s, va_list * args)
33{
34 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
35 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
36 vxlan_rx_trace_t * t = va_arg (*args, vxlan_rx_trace_t *);
37
38 if (t->tunnel_index != ~0)
39 {
John Loc42912d2016-11-07 18:30:47 -050040 s = format (s, "VXLAN decap from vxlan_tunnel%d vni %d next %d error %d",
Ed Warnickecb9cada2015-12-08 15:45:58 -070041 t->tunnel_index, t->vni, t->next_index, t->error);
42 }
43 else
44 {
John Loc42912d2016-11-07 18:30:47 -050045 s = format (s, "VXLAN decap error - tunnel for vni %d does not exist",
46 t->vni);
Ed Warnickecb9cada2015-12-08 15:45:58 -070047 }
48 return s;
49}
50
John Lo2b81eb82017-01-30 13:12:10 -050051always_inline u32
52validate_vxlan_fib (vlib_buffer_t *b, vxlan_tunnel_t *t, u32 is_ip4)
53{
Eyal Barifb663012017-10-19 15:27:51 +030054 u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
John Lo2b81eb82017-01-30 13:12:10 -050055
Eyal Barifb663012017-10-19 15:27:51 +030056 u32 * fib_index_by_sw_if_index = is_ip4 ?
57 ip4_main.fib_index_by_sw_if_index : ip6_main.fib_index_by_sw_if_index;
58 u32 tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
59 u32 fib_index = (tx_sw_if_index == (u32) ~ 0) ?
60 vec_elt (fib_index_by_sw_if_index, sw_if_index) : tx_sw_if_index;
John Lo2b81eb82017-01-30 13:12:10 -050061
62 return (fib_index == t->encap_fib_index);
63}
64
Chris Luke99cb3352016-04-26 10:49:53 -040065always_inline uword
Ed Warnickecb9cada2015-12-08 15:45:58 -070066vxlan_input (vlib_main_t * vm,
67 vlib_node_runtime_t * node,
Chris Luke99cb3352016-04-26 10:49:53 -040068 vlib_frame_t * from_frame,
John Lo37682e12016-11-30 12:51:39 -050069 u32 is_ip4)
Ed Warnickecb9cada2015-12-08 15:45:58 -070070{
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 vxlan_main_t * vxm = &vxlan_main;
72 vnet_main_t * vnm = vxm->vnet_main;
73 vnet_interface_main_t * im = &vnm->interface_main;
74 u32 last_tunnel_index = ~0;
Chris Luke99cb3352016-04-26 10:49:53 -040075 vxlan4_tunnel_key_t last_key4;
76 vxlan6_tunnel_key_t last_key6;
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 u32 pkts_decapsulated = 0;
Damjan Marion586afd72017-04-05 19:18:20 +020078 u32 thread_index = vlib_get_thread_index();
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
Dave Barachf9c231e2016-08-05 10:10:18 -040080 if (is_ip4)
81 last_key4.as_u64 = ~0;
82 else
83 memset (&last_key6, 0xff, sizeof (last_key6));
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
Eyal Barifb663012017-10-19 15:27:51 +030085 u32 next_index = node->cached_next_index;
86 u32 stats_sw_if_index = node->runtime_data[0];
87 u32 stats_n_packets = 0, stats_n_bytes = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
Eyal Barifb663012017-10-19 15:27:51 +030089 u32 * from = vlib_frame_vector_args (from_frame);
90 u32 n_left_from = from_frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
92 while (n_left_from > 0)
93 {
Eyal Barifb663012017-10-19 15:27:51 +030094 u32 * to_next, n_left_to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 vlib_get_next_frame (vm, node, next_index,
96 to_next, n_left_to_next);
Eyal Barifb663012017-10-19 15:27:51 +030097
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 while (n_left_from >= 4 && n_left_to_next >= 2)
99 {
100 u32 bi0, bi1;
101 vlib_buffer_t * b0, * b1;
102 u32 next0, next1;
Eyal Barifb663012017-10-19 15:27:51 +0300103 vxlan_tunnel_t * t0, * t1, * stats_t0, * stats_t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
105 /* Prefetch next iteration. */
106 {
107 vlib_buffer_t * p2, * p3;
108
109 p2 = vlib_get_buffer (vm, from[2]);
110 p3 = vlib_get_buffer (vm, from[3]);
111
112 vlib_prefetch_buffer_header (p2, LOAD);
113 vlib_prefetch_buffer_header (p3, LOAD);
114
115 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
116 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
117 }
118
119 bi0 = from[0];
120 bi1 = from[1];
121 to_next[0] = bi0;
122 to_next[1] = bi1;
123 from += 2;
124 to_next += 2;
125 n_left_to_next -= 2;
126 n_left_from -= 2;
127
128 b0 = vlib_get_buffer (vm, bi0);
129 b1 = vlib_get_buffer (vm, bi1);
130
131 /* udp leaves current_data pointing at the vxlan header */
Eyal Barifb663012017-10-19 15:27:51 +0300132 void * cur0 = vlib_buffer_get_current (b0);
133 void * cur1 = vlib_buffer_get_current (b1);
134 vxlan_header_t * vxlan0 = cur0;
135 vxlan_header_t * vxlan1 = cur1;
136
137 ip4_header_t * ip4_0, * ip4_1;
138 ip6_header_t * ip6_0, * ip6_1;
Chris Luke99cb3352016-04-26 10:49:53 -0400139 if (is_ip4) {
Eyal Barifb663012017-10-19 15:27:51 +0300140 ip4_0 = cur0 - sizeof(udp_header_t) - sizeof(ip4_header_t);
141 ip4_1 = cur1 - sizeof(udp_header_t) - sizeof(ip4_header_t);
Chris Luke99cb3352016-04-26 10:49:53 -0400142 } else {
Eyal Barifb663012017-10-19 15:27:51 +0300143 ip6_0 = cur0 - sizeof(udp_header_t) - sizeof(ip6_header_t);
144 ip6_1 = cur1 - sizeof(udp_header_t) - sizeof(ip6_header_t);
Chris Luke99cb3352016-04-26 10:49:53 -0400145 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
147 /* pop (ip, udp, vxlan) */
Eyal Barifb663012017-10-19 15:27:51 +0300148 vlib_buffer_advance (b0, sizeof *vxlan0);
149 vlib_buffer_advance (b1, sizeof *vxlan1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Eyal Barifb663012017-10-19 15:27:51 +0300151 u32 tunnel_index0 = ~0, tunnel_index1 = ~0;
152 u32 error0 = 0, error1 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
John Loc42912d2016-11-07 18:30:47 -0500154 if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
155 {
156 error0 = VXLAN_ERROR_BAD_FLAGS;
157 next0 = VXLAN_INPUT_NEXT_DROP;
158 goto trace0;
159 }
160
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Eyal Barifb663012017-10-19 15:27:51 +0300162 if (is_ip4) {
John Lo56912c82016-12-08 16:10:02 -0500163 /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
Eyal Barifb663012017-10-19 15:27:51 +0300164 vxlan4_tunnel_key_t key4_0 = {
165 .src = ip4_0->src_address.as_u32,
166 .vni = vxlan0->vni_reserved,
167 };
168
John Lo56912c82016-12-08 16:10:02 -0500169 if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64))
Chris Luke99cb3352016-04-26 10:49:53 -0400170 {
Eyal Barifb663012017-10-19 15:27:51 +0300171 uword * p = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64);
172 if (PREDICT_FALSE (p == NULL))
Chris Luke99cb3352016-04-26 10:49:53 -0400173 {
174 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
175 next0 = VXLAN_INPUT_NEXT_DROP;
176 goto trace0;
177 }
Eyal Barifb663012017-10-19 15:27:51 +0300178 last_key4 = key4_0;
179 last_tunnel_index = p[0];
Chris Luke99cb3352016-04-26 10:49:53 -0400180 }
Eyal Barifb663012017-10-19 15:27:51 +0300181 tunnel_index0 = last_tunnel_index;
182 stats_t0 = t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
John Lo56912c82016-12-08 16:10:02 -0500183
John Lo2b81eb82017-01-30 13:12:10 -0500184 /* Validate VXLAN tunnel encap-fib index agaist packet */
185 if (PREDICT_FALSE (validate_vxlan_fib (b0, t0, is_ip4) == 0))
186 {
187 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
188 next0 = VXLAN_INPUT_NEXT_DROP;
189 goto trace0;
190 }
191
192 /* Validate VXLAN tunnel SIP against packet DIP */
John Lo56912c82016-12-08 16:10:02 -0500193 if (PREDICT_TRUE (ip4_0->dst_address.as_u32 == t0->src.ip4.as_u32))
194 goto next0; /* valid packet */
195 if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_0->dst_address)))
196 {
197 key4_0.src = ip4_0->dst_address.as_u32;
John Lo56912c82016-12-08 16:10:02 -0500198 /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */
Eyal Barifb663012017-10-19 15:27:51 +0300199 uword * p = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64);
200 if (PREDICT_TRUE (p != NULL))
John Lo56912c82016-12-08 16:10:02 -0500201 {
Eyal Barifb663012017-10-19 15:27:51 +0300202 stats_t0 = pool_elt_at_index (vxm->tunnels, p[0]);
John Lo56912c82016-12-08 16:10:02 -0500203 goto next0; /* valid packet */
204 }
205 }
206 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
207 next0 = VXLAN_INPUT_NEXT_DROP;
208 goto trace0;
209
210 } else /* !is_ip4 */ {
John Lo56912c82016-12-08 16:10:02 -0500211 /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
Eyal Barifb663012017-10-19 15:27:51 +0300212 vxlan6_tunnel_key_t key6_0 = {
213 .src = ip6_0->src_address,
214 .vni = vxlan0->vni_reserved,
215 };
216
217 if (PREDICT_FALSE (memcmp(&key6_0, &last_key6, sizeof(last_key6)) != 0))
Chris Lukec7949152016-05-31 10:42:14 -0400218 {
Eyal Barifb663012017-10-19 15:27:51 +0300219 uword * p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_0);
220 if (PREDICT_FALSE (p == NULL))
Chris Lukec7949152016-05-31 10:42:14 -0400221 {
222 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
223 next0 = VXLAN_INPUT_NEXT_DROP;
224 goto trace0;
225 }
Chris Lukec7949152016-05-31 10:42:14 -0400226 clib_memcpy (&last_key6, &key6_0, sizeof(key6_0));
Eyal Barifb663012017-10-19 15:27:51 +0300227 last_tunnel_index = p[0];
Chris Lukec7949152016-05-31 10:42:14 -0400228 }
Eyal Barifb663012017-10-19 15:27:51 +0300229 tunnel_index0 = last_tunnel_index;
230 stats_t0 = t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
John Lo56912c82016-12-08 16:10:02 -0500231
John Lo2b81eb82017-01-30 13:12:10 -0500232 /* Validate VXLAN tunnel encap-fib index agaist packet */
233 if (PREDICT_FALSE (validate_vxlan_fib (b0, t0, is_ip4) == 0))
234 {
235 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
236 next0 = VXLAN_INPUT_NEXT_DROP;
237 goto trace0;
238 }
239
John Lo56912c82016-12-08 16:10:02 -0500240 /* Validate VXLAN tunnel SIP against packet DIP */
241 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address,
242 &t0->src.ip6)))
243 goto next0; /* valid packet */
244 if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_0->dst_address)))
245 {
Eyal Barifb663012017-10-19 15:27:51 +0300246 key6_0.src = ip6_0->dst_address;
247 uword * p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_0);
248 if (PREDICT_TRUE (p != NULL))
John Lo56912c82016-12-08 16:10:02 -0500249 {
Eyal Barifb663012017-10-19 15:27:51 +0300250 stats_t0 = pool_elt_at_index (vxm->tunnels, p[0]);
John Lo56912c82016-12-08 16:10:02 -0500251 goto next0; /* valid packet */
252 }
253 }
254 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
255 next0 = VXLAN_INPUT_NEXT_DROP;
256 goto trace0;
Chris Luke99cb3352016-04-26 10:49:53 -0400257 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258
John Lo56912c82016-12-08 16:10:02 -0500259 next0:
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800260 next0 = t0->decap_next_index;
Eyal Barifb663012017-10-19 15:27:51 +0300261 u32 len0 = vlib_buffer_length_in_chain (vm, b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262
263 /* Required to make the l2 tag push / pop code work on l2 subifs */
Chris Luke99cb3352016-04-26 10:49:53 -0400264 if (PREDICT_TRUE(next0 == VXLAN_INPUT_NEXT_L2_INPUT))
265 vnet_update_l2_len (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266
John Lo56912c82016-12-08 16:10:02 -0500267 /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
Eyal Barifb663012017-10-19 15:27:51 +0300268 vnet_buffer(b0)->sw_if_index[VLIB_RX] = t0->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
270 /* Batch stats increment on the same vxlan tunnel so counter
271 is not incremented per packet */
Eyal Barifb663012017-10-19 15:27:51 +0300272 u32 sw_if_index0 = stats_t0->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
274 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275 if (stats_n_packets)
Eyal Barifb663012017-10-19 15:27:51 +0300276 {
277 vlib_increment_combined_counter
278 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
279 thread_index, stats_sw_if_index,
280 stats_n_packets, stats_n_bytes);
281 pkts_decapsulated += stats_n_packets;
282 stats_n_packets = stats_n_bytes = 0;
283 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284 stats_sw_if_index = sw_if_index0;
285 }
Eyal Barifb663012017-10-19 15:27:51 +0300286 stats_n_packets += 1;
287 stats_n_bytes += len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
289 trace0:
290 b0->error = error0 ? node->errors[error0] : 0;
291
292 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
293 {
294 vxlan_rx_trace_t *tr
295 = vlib_add_trace (vm, node, b0, sizeof (*tr));
296 tr->next_index = next0;
297 tr->error = error0;
298 tr->tunnel_index = tunnel_index0;
299 tr->vni = vnet_get_vni (vxlan0);
300 }
301
John Loc42912d2016-11-07 18:30:47 -0500302 if (PREDICT_FALSE (vxlan1->flags != VXLAN_FLAGS_I))
303 {
304 error1 = VXLAN_ERROR_BAD_FLAGS;
305 next1 = VXLAN_INPUT_NEXT_DROP;
306 goto trace1;
307 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
Chris Luke99cb3352016-04-26 10:49:53 -0400309 if (is_ip4) {
Eyal Barifb663012017-10-19 15:27:51 +0300310 /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
311 vxlan4_tunnel_key_t key4_1 = {
312 .src = ip4_1->src_address.as_u32,
313 .vni = vxlan1->vni_reserved
314 };
Chris Luke99cb3352016-04-26 10:49:53 -0400315
John Lo56912c82016-12-08 16:10:02 -0500316 if (PREDICT_FALSE (key4_1.as_u64 != last_key4.as_u64))
Chris Luke99cb3352016-04-26 10:49:53 -0400317 {
Eyal Barifb663012017-10-19 15:27:51 +0300318 uword * p = hash_get (vxm->vxlan4_tunnel_by_key, key4_1.as_u64);
319 if (PREDICT_FALSE (p == NULL))
Chris Luke99cb3352016-04-26 10:49:53 -0400320 {
321 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
322 next1 = VXLAN_INPUT_NEXT_DROP;
323 goto trace1;
324 }
Eyal Barifb663012017-10-19 15:27:51 +0300325 last_key4 = key4_1;
326 last_tunnel_index = p[0];
Chris Luke99cb3352016-04-26 10:49:53 -0400327 }
Eyal Barifb663012017-10-19 15:27:51 +0300328 tunnel_index1 = last_tunnel_index;
329 stats_t1 = t1 = pool_elt_at_index (vxm->tunnels, tunnel_index1);
John Lo56912c82016-12-08 16:10:02 -0500330
Eyal Barifb663012017-10-19 15:27:51 +0300331 /* Validate VXLAN tunnel encap-fib index against packet */
John Lo2b81eb82017-01-30 13:12:10 -0500332 if (PREDICT_FALSE (validate_vxlan_fib (b1, t1, is_ip4) == 0))
333 {
334 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
335 next1 = VXLAN_INPUT_NEXT_DROP;
336 goto trace1;
337 }
338
John Lo56912c82016-12-08 16:10:02 -0500339 /* Validate VXLAN tunnel SIP against packet DIP */
340 if (PREDICT_TRUE (ip4_1->dst_address.as_u32 == t1->src.ip4.as_u32))
341 goto next1; /* valid packet */
342 if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_1->dst_address)))
343 {
John Lo56912c82016-12-08 16:10:02 -0500344 /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */
Eyal Barifb663012017-10-19 15:27:51 +0300345 key4_1.src = ip4_1->dst_address.as_u32;
346
347 uword * p = hash_get (vxm->vxlan4_tunnel_by_key, key4_1.as_u64);
348 if (PREDICT_TRUE (p != NULL))
John Lo56912c82016-12-08 16:10:02 -0500349 {
Eyal Barifb663012017-10-19 15:27:51 +0300350 stats_t1 = pool_elt_at_index (vxm->tunnels, p[0]);
John Lo56912c82016-12-08 16:10:02 -0500351 goto next1; /* valid packet */
352 }
353 }
354 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
355 next1 = VXLAN_INPUT_NEXT_DROP;
356 goto trace1;
357
358 } else /* !is_ip4 */ {
John Lo56912c82016-12-08 16:10:02 -0500359 /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
Eyal Barifb663012017-10-19 15:27:51 +0300360 vxlan6_tunnel_key_t key6_1 = {
361 .src = ip6_1->src_address,
362 .vni = vxlan1->vni_reserved,
363 };
364
Chris Luke99cb3352016-04-26 10:49:53 -0400365 if (PREDICT_FALSE (memcmp(&key6_1, &last_key6, sizeof(last_key6)) != 0))
Chris Lukec7949152016-05-31 10:42:14 -0400366 {
Eyal Barifb663012017-10-19 15:27:51 +0300367 uword * p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_1);
368 if (PREDICT_FALSE (p == NULL))
Chris Lukec7949152016-05-31 10:42:14 -0400369 {
370 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
371 next1 = VXLAN_INPUT_NEXT_DROP;
372 goto trace1;
373 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374
Chris Lukec7949152016-05-31 10:42:14 -0400375 clib_memcpy (&last_key6, &key6_1, sizeof(key6_1));
Eyal Barifb663012017-10-19 15:27:51 +0300376 last_tunnel_index = p[0];
Chris Lukec7949152016-05-31 10:42:14 -0400377 }
Eyal Barifb663012017-10-19 15:27:51 +0300378 tunnel_index1 = last_tunnel_index;
379 stats_t1 = t1 = pool_elt_at_index (vxm->tunnels, tunnel_index1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380
John Lo2b81eb82017-01-30 13:12:10 -0500381 /* Validate VXLAN tunnel encap-fib index agaist packet */
382 if (PREDICT_FALSE (validate_vxlan_fib (b1, t1, is_ip4) == 0))
383 {
384 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
385 next1 = VXLAN_INPUT_NEXT_DROP;
386 goto trace1;
387 }
388
John Lo56912c82016-12-08 16:10:02 -0500389 /* Validate VXLAN tunnel SIP against packet DIP */
390 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_1->dst_address,
391 &t1->src.ip6)))
392 goto next1; /* valid packet */
393 if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_1->dst_address)))
394 {
Eyal Barifb663012017-10-19 15:27:51 +0300395 key6_1.src = ip6_1->dst_address;
396
397 uword * p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_1);
398 if (PREDICT_TRUE (p != NULL))
John Lo56912c82016-12-08 16:10:02 -0500399 {
Eyal Barifb663012017-10-19 15:27:51 +0300400 stats_t1 = pool_elt_at_index (vxm->tunnels, p[0]);
John Lo56912c82016-12-08 16:10:02 -0500401 goto next1; /* valid packet */
402 }
403 }
404 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
405 next1 = VXLAN_INPUT_NEXT_DROP;
406 goto trace1;
407 }
408
409 next1:
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800410 next1 = t1->decap_next_index;
Eyal Barifb663012017-10-19 15:27:51 +0300411 u32 len1 = vlib_buffer_length_in_chain (vm, b1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412
413 /* Required to make the l2 tag push / pop code work on l2 subifs */
Chris Luke99cb3352016-04-26 10:49:53 -0400414 if (PREDICT_TRUE(next1 == VXLAN_INPUT_NEXT_L2_INPUT))
415 vnet_update_l2_len (b1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416
John Lo56912c82016-12-08 16:10:02 -0500417 /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
Eyal Barifb663012017-10-19 15:27:51 +0300418 vnet_buffer(b1)->sw_if_index[VLIB_RX] = t1->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419
420 /* Batch stats increment on the same vxlan tunnel so counter
421 is not incremented per packet */
Eyal Barifb663012017-10-19 15:27:51 +0300422 u32 sw_if_index1 = stats_t1->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
424 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425 if (stats_n_packets)
Eyal Barifb663012017-10-19 15:27:51 +0300426 {
427 vlib_increment_combined_counter
428 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
429 thread_index, stats_sw_if_index,
430 stats_n_packets, stats_n_bytes);
431 pkts_decapsulated += stats_n_packets;
432 stats_n_packets = stats_n_bytes = 0;
433 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434 stats_sw_if_index = sw_if_index1;
435 }
Eyal Barifb663012017-10-19 15:27:51 +0300436 stats_n_packets += 1;
437 stats_n_bytes += len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438
439 trace1:
440 b1->error = error1 ? node->errors[error1] : 0;
441
442 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
443 {
444 vxlan_rx_trace_t *tr
445 = vlib_add_trace (vm, node, b1, sizeof (*tr));
446 tr->next_index = next1;
447 tr->error = error1;
448 tr->tunnel_index = tunnel_index1;
449 tr->vni = vnet_get_vni (vxlan1);
450 }
451
452 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
453 to_next, n_left_to_next,
454 bi0, bi1, next0, next1);
455 }
456
457 while (n_left_from > 0 && n_left_to_next > 0)
458 {
459 u32 bi0;
460 vlib_buffer_t * b0;
461 u32 next0;
Chris Luke99cb3352016-04-26 10:49:53 -0400462 ip4_header_t * ip4_0;
463 ip6_header_t * ip6_0;
Eyal Barifb663012017-10-19 15:27:51 +0300464 vxlan_tunnel_t * t0, * stats_t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
466 bi0 = from[0];
467 to_next[0] = bi0;
468 from += 1;
469 to_next += 1;
470 n_left_from -= 1;
471 n_left_to_next -= 1;
472
473 b0 = vlib_get_buffer (vm, bi0);
474
475 /* udp leaves current_data pointing at the vxlan header */
Eyal Barifb663012017-10-19 15:27:51 +0300476 void * cur0 = vlib_buffer_get_current (b0);
477 vxlan_header_t * vxlan0 = cur0;
478 if (is_ip4)
479 ip4_0 = cur0 -sizeof(udp_header_t) - sizeof(ip4_header_t);
480 else
481 ip6_0 = cur0 -sizeof(udp_header_t) - sizeof(ip6_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482
483 /* pop (ip, udp, vxlan) */
Eyal Barifb663012017-10-19 15:27:51 +0300484 vlib_buffer_advance (b0, sizeof(*vxlan0));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485
Eyal Barifb663012017-10-19 15:27:51 +0300486 u32 tunnel_index0 = ~0;
487 u32 error0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488
John Loc42912d2016-11-07 18:30:47 -0500489 if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
490 {
491 error0 = VXLAN_ERROR_BAD_FLAGS;
492 next0 = VXLAN_INPUT_NEXT_DROP;
493 goto trace00;
494 }
495
Chris Luke99cb3352016-04-26 10:49:53 -0400496 if (is_ip4) {
Eyal Barifb663012017-10-19 15:27:51 +0300497 vxlan4_tunnel_key_t key4_0 = {
498 .src = ip4_0->src_address.as_u32,
499 .vni = vxlan0->vni_reserved,
500 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501
John Lo56912c82016-12-08 16:10:02 -0500502 /* Make sure unicast VXLAN tunnel exist by packet SIP and VNI */
Chris Luke99cb3352016-04-26 10:49:53 -0400503 if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64))
504 {
Eyal Barifb663012017-10-19 15:27:51 +0300505 uword * p = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64);
506 if (PREDICT_FALSE (p == NULL))
Chris Luke99cb3352016-04-26 10:49:53 -0400507 {
508 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
509 next0 = VXLAN_INPUT_NEXT_DROP;
510 goto trace00;
511 }
Eyal Barifb663012017-10-19 15:27:51 +0300512 last_key4 = key4_0;
513 last_tunnel_index = p[0];
Chris Luke99cb3352016-04-26 10:49:53 -0400514 }
Eyal Barifb663012017-10-19 15:27:51 +0300515 tunnel_index0 = last_tunnel_index;
516 stats_t0 = t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
John Lo56912c82016-12-08 16:10:02 -0500517
John Lo2b81eb82017-01-30 13:12:10 -0500518 /* Validate VXLAN tunnel encap-fib index agaist packet */
519 if (PREDICT_FALSE (validate_vxlan_fib (b0, t0, is_ip4) == 0))
520 {
521 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
522 next0 = VXLAN_INPUT_NEXT_DROP;
523 goto trace00;
524 }
525
John Lo56912c82016-12-08 16:10:02 -0500526 /* Validate VXLAN tunnel SIP against packet DIP */
527 if (PREDICT_TRUE (ip4_0->dst_address.as_u32 == t0->src.ip4.as_u32))
528 goto next00; /* valid packet */
529 if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_0->dst_address)))
530 {
John Lo56912c82016-12-08 16:10:02 -0500531 /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */
Eyal Barifb663012017-10-19 15:27:51 +0300532 key4_0.src = ip4_0->dst_address.as_u32;
533 uword * p = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64);
534 if (PREDICT_TRUE (p != NULL))
John Lo56912c82016-12-08 16:10:02 -0500535 {
Eyal Barifb663012017-10-19 15:27:51 +0300536 stats_t0 = pool_elt_at_index (vxm->tunnels, p[0]);
John Lo56912c82016-12-08 16:10:02 -0500537 goto next00; /* valid packet */
538 }
539 }
540 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
541 next0 = VXLAN_INPUT_NEXT_DROP;
542 goto trace00;
543
Chris Luke99cb3352016-04-26 10:49:53 -0400544 } else /* !is_ip4 */ {
John Lo56912c82016-12-08 16:10:02 -0500545 /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
Eyal Barifb663012017-10-19 15:27:51 +0300546 vxlan6_tunnel_key_t key6_0 = {
547 .src = ip6_0->src_address,
548 .vni = vxlan0->vni_reserved,
549 };
550
Chris Luke99cb3352016-04-26 10:49:53 -0400551 if (PREDICT_FALSE (memcmp(&key6_0, &last_key6, sizeof(last_key6)) != 0))
Chris Lukec7949152016-05-31 10:42:14 -0400552 {
Eyal Barifb663012017-10-19 15:27:51 +0300553 uword * p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_0);
554 if (PREDICT_FALSE (p == NULL))
Chris Lukec7949152016-05-31 10:42:14 -0400555 {
556 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
557 next0 = VXLAN_INPUT_NEXT_DROP;
558 goto trace00;
559 }
Chris Lukec7949152016-05-31 10:42:14 -0400560 clib_memcpy (&last_key6, &key6_0, sizeof(key6_0));
Eyal Barifb663012017-10-19 15:27:51 +0300561 last_tunnel_index = p[0];
Chris Lukec7949152016-05-31 10:42:14 -0400562 }
Eyal Barifb663012017-10-19 15:27:51 +0300563 tunnel_index0 = last_tunnel_index;
564 stats_t0 = t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
John Lo56912c82016-12-08 16:10:02 -0500565
John Lo2b81eb82017-01-30 13:12:10 -0500566 /* Validate VXLAN tunnel encap-fib index agaist packet */
567 if (PREDICT_FALSE (validate_vxlan_fib (b0, t0, is_ip4) == 0))
568 {
569 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
570 next0 = VXLAN_INPUT_NEXT_DROP;
571 goto trace00;
572 }
573
John Lo56912c82016-12-08 16:10:02 -0500574 /* Validate VXLAN tunnel SIP against packet DIP */
575 if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address,
576 &t0->src.ip6)))
577 goto next00; /* valid packet */
578 if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_0->dst_address)))
579 {
Eyal Barifb663012017-10-19 15:27:51 +0300580 key6_0.src = ip6_0->dst_address;
581 uword * p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_0);
582 if (PREDICT_TRUE (p != NULL))
John Lo56912c82016-12-08 16:10:02 -0500583 {
Eyal Barifb663012017-10-19 15:27:51 +0300584 stats_t0 = pool_elt_at_index (vxm->tunnels, p[0]);
John Lo56912c82016-12-08 16:10:02 -0500585 goto next00; /* valid packet */
586 }
587 }
588 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
589 next0 = VXLAN_INPUT_NEXT_DROP;
590 goto trace00;
Chris Luke99cb3352016-04-26 10:49:53 -0400591 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
John Lo56912c82016-12-08 16:10:02 -0500593 next00:
Hongjun Nibeb4bf72016-11-25 00:03:46 +0800594 next0 = t0->decap_next_index;
Eyal Barifb663012017-10-19 15:27:51 +0300595 u32 len0 = vlib_buffer_length_in_chain (vm, b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596
597 /* Required to make the l2 tag push / pop code work on l2 subifs */
Chris Luke99cb3352016-04-26 10:49:53 -0400598 if (PREDICT_TRUE(next0 == VXLAN_INPUT_NEXT_L2_INPUT))
599 vnet_update_l2_len (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700600
John Lo56912c82016-12-08 16:10:02 -0500601 /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
Eyal Barifb663012017-10-19 15:27:51 +0300602 vnet_buffer(b0)->sw_if_index[VLIB_RX] = t0->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603
604 /* Batch stats increment on the same vxlan tunnel so counter
605 is not incremented per packet */
Eyal Barifb663012017-10-19 15:27:51 +0300606 u32 sw_if_index0 = stats_t0->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
608 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609 if (stats_n_packets)
Eyal Barifb663012017-10-19 15:27:51 +0300610 {
611 vlib_increment_combined_counter
612 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
613 thread_index, stats_sw_if_index,
614 stats_n_packets, stats_n_bytes);
615 pkts_decapsulated += stats_n_packets;
616 stats_n_packets = stats_n_bytes = 0;
617 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618 stats_sw_if_index = sw_if_index0;
619 }
Eyal Barifb663012017-10-19 15:27:51 +0300620 stats_n_packets += 1;
621 stats_n_bytes += len0;
622
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623
624 trace00:
625 b0->error = error0 ? node->errors[error0] : 0;
626
627 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
628 {
629 vxlan_rx_trace_t *tr
630 = vlib_add_trace (vm, node, b0, sizeof (*tr));
631 tr->next_index = next0;
632 tr->error = error0;
633 tr->tunnel_index = tunnel_index0;
634 tr->vni = vnet_get_vni (vxlan0);
635 }
636 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
637 to_next, n_left_to_next,
638 bi0, next0);
639 }
640
641 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
642 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643 /* Increment any remaining batch stats */
644 if (stats_n_packets)
645 {
Eyal Barifb663012017-10-19 15:27:51 +0300646 pkts_decapsulated += stats_n_packets;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647 vlib_increment_combined_counter
648 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200649 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650 node->runtime_data[0] = stats_sw_if_index;
651 }
652
Eyal Barifb663012017-10-19 15:27:51 +0300653 /* Do we still need this now that tunnel tx stats is kept? */
654 u32 node_idx = is_ip4 ? vxlan4_input_node.index : vxlan6_input_node.index;
655 vlib_node_increment_counter (vm, node_idx, VXLAN_ERROR_DECAPSULATED,
656 pkts_decapsulated);
657
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658 return from_frame->n_vectors;
659}
660
Chris Luke99cb3352016-04-26 10:49:53 -0400661static uword
662vxlan4_input (vlib_main_t * vm,
663 vlib_node_runtime_t * node,
664 vlib_frame_t * from_frame)
665{
666 return vxlan_input(vm, node, from_frame, /* is_ip4 */ 1);
667}
668
669static uword
670vxlan6_input (vlib_main_t * vm,
671 vlib_node_runtime_t * node,
672 vlib_frame_t * from_frame)
673{
674 return vxlan_input(vm, node, from_frame, /* is_ip4 */ 0);
675}
676
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677static char * vxlan_error_strings[] = {
678#define vxlan_error(n,s) s,
679#include <vnet/vxlan/vxlan_error.def>
680#undef vxlan_error
681#undef _
682};
683
Chris Luke99cb3352016-04-26 10:49:53 -0400684VLIB_REGISTER_NODE (vxlan4_input_node) = {
685 .function = vxlan4_input,
686 .name = "vxlan4-input",
687 /* Takes a vector of packets. */
688 .vector_size = sizeof (u32),
689
690 .n_errors = VXLAN_N_ERROR,
691 .error_strings = vxlan_error_strings,
692
693 .n_next_nodes = VXLAN_INPUT_N_NEXT,
694 .next_nodes = {
695#define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
696 foreach_vxlan_input_next
697#undef _
698 },
699
700//temp .format_buffer = format_vxlan_header,
701 .format_trace = format_vxlan_rx_trace,
702 // $$$$ .unformat_buffer = unformat_vxlan_header,
703};
704
Damjan Marion1c80e832016-05-11 23:07:18 +0200705VLIB_NODE_FUNCTION_MULTIARCH (vxlan4_input_node, vxlan4_input)
706
Chris Luke99cb3352016-04-26 10:49:53 -0400707VLIB_REGISTER_NODE (vxlan6_input_node) = {
708 .function = vxlan6_input,
709 .name = "vxlan6-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 /* Takes a vector of packets. */
711 .vector_size = sizeof (u32),
712
713 .n_errors = VXLAN_N_ERROR,
714 .error_strings = vxlan_error_strings,
715
716 .n_next_nodes = VXLAN_INPUT_N_NEXT,
717 .next_nodes = {
718#define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
719 foreach_vxlan_input_next
720#undef _
721 },
722
723//temp .format_buffer = format_vxlan_header,
724 .format_trace = format_vxlan_rx_trace,
725 // $$$$ .unformat_buffer = unformat_vxlan_header,
726};
Damjan Marion1c80e832016-05-11 23:07:18 +0200727
728VLIB_NODE_FUNCTION_MULTIARCH (vxlan6_input_node, vxlan6_input)
729
John Lo37682e12016-11-30 12:51:39 -0500730
731typedef enum {
732 IP_VXLAN_BYPASS_NEXT_DROP,
733 IP_VXLAN_BYPASS_NEXT_VXLAN,
734 IP_VXLAN_BYPASS_N_NEXT,
735} ip_vxan_bypass_next_t;
736
737always_inline uword
738ip_vxlan_bypass_inline (vlib_main_t * vm,
John Lo2b81eb82017-01-30 13:12:10 -0500739 vlib_node_runtime_t * node,
740 vlib_frame_t * frame,
741 u32 is_ip4)
John Lo37682e12016-11-30 12:51:39 -0500742{
743 vxlan_main_t * vxm = &vxlan_main;
744 u32 * from, * to_next, n_left_from, n_left_to_next, next_index;
745 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
746 ip4_address_t addr4; /* last IPv4 address matching a local VTEP address */
747 ip6_address_t addr6; /* last IPv6 address matching a local VTEP address */
748
749 from = vlib_frame_vector_args (frame);
750 n_left_from = frame->n_vectors;
751 next_index = node->cached_next_index;
752
753 if (node->flags & VLIB_NODE_FLAG_TRACE)
754 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
755
756 if (is_ip4) addr4.data_u32 = ~0;
757 else ip6_address_set_zero (&addr6);
758
759 while (n_left_from > 0)
760 {
761 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
762
763 while (n_left_from >= 4 && n_left_to_next >= 2)
764 {
765 vlib_buffer_t * b0, * b1;
John Lo2b81eb82017-01-30 13:12:10 -0500766 ip4_header_t * ip40, * ip41;
767 ip6_header_t * ip60, * ip61;
John Lo37682e12016-11-30 12:51:39 -0500768 udp_header_t * udp0, * udp1;
769 u32 bi0, ip_len0, udp_len0, flags0, next0;
770 u32 bi1, ip_len1, udp_len1, flags1, next1;
771 i32 len_diff0, len_diff1;
772 u8 error0, good_udp0, proto0;
773 u8 error1, good_udp1, proto1;
774
775 /* Prefetch next iteration. */
776 {
777 vlib_buffer_t * p2, * p3;
778
779 p2 = vlib_get_buffer (vm, from[2]);
780 p3 = vlib_get_buffer (vm, from[3]);
781
782 vlib_prefetch_buffer_header (p2, LOAD);
783 vlib_prefetch_buffer_header (p3, LOAD);
784
785 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
786 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
787 }
788
789 bi0 = to_next[0] = from[0];
790 bi1 = to_next[1] = from[1];
791 from += 2;
792 n_left_from -= 2;
793 to_next += 2;
794 n_left_to_next -= 2;
795
796 b0 = vlib_get_buffer (vm, bi0);
797 b1 = vlib_get_buffer (vm, bi1);
John Lo2b81eb82017-01-30 13:12:10 -0500798 if (is_ip4)
799 {
800 ip40 = vlib_buffer_get_current (b0);
801 ip41 = vlib_buffer_get_current (b1);
802 }
803 else
804 {
805 ip60 = vlib_buffer_get_current (b0);
806 ip61 = vlib_buffer_get_current (b1);
807 }
John Lo37682e12016-11-30 12:51:39 -0500808
809 /* Setup packet for next IP feature */
810 vnet_feature_next(vnet_buffer(b0)->sw_if_index[VLIB_RX], &next0, b0);
811 vnet_feature_next(vnet_buffer(b1)->sw_if_index[VLIB_RX], &next1, b1);
812
John Lo2b81eb82017-01-30 13:12:10 -0500813 if (is_ip4)
814 {
815 /* Treat IP frag packets as "experimental" protocol for now
816 until support of IP frag reassembly is implemented */
817 proto0 = ip4_is_fragment(ip40) ? 0xfe : ip40->protocol;
818 proto1 = ip4_is_fragment(ip41) ? 0xfe : ip41->protocol;
819 }
820 else
821 {
822 proto0 = ip60->protocol;
823 proto1 = ip61->protocol;
824 }
John Lo37682e12016-11-30 12:51:39 -0500825
826 /* Process packet 0 */
827 if (proto0 != IP_PROTOCOL_UDP)
828 goto exit0; /* not UDP packet */
829
John Lo2b81eb82017-01-30 13:12:10 -0500830 if (is_ip4)
831 udp0 = ip4_next_header (ip40);
832 else
833 udp0 = ip6_next_header (ip60);
834
John Lo37682e12016-11-30 12:51:39 -0500835 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
836 goto exit0; /* not VXLAN packet */
837
John Lo2b81eb82017-01-30 13:12:10 -0500838 /* Validate DIP against VTEPs*/
John Lo37682e12016-11-30 12:51:39 -0500839 if (is_ip4)
840 {
John Lo2b81eb82017-01-30 13:12:10 -0500841 if (addr4.as_u32 != ip40->dst_address.as_u32)
John Lo37682e12016-11-30 12:51:39 -0500842 {
John Lo2b81eb82017-01-30 13:12:10 -0500843 if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32))
844 goto exit0; /* no local VTEP for VXLAN packet */
845 addr4 = ip40->dst_address;
John Lo37682e12016-11-30 12:51:39 -0500846 }
847 }
John Lo2b81eb82017-01-30 13:12:10 -0500848 else
849 {
850 if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
851 {
852 if (!hash_get_mem (vxm->vtep6, &ip60->dst_address))
853 goto exit0; /* no local VTEP for VXLAN packet */
854 addr6 = ip60->dst_address;
855 }
856 }
John Lo37682e12016-11-30 12:51:39 -0500857
858 flags0 = b0->flags;
Damjan Marion213b5aa2017-07-13 21:19:27 +0200859 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo37682e12016-11-30 12:51:39 -0500860
861 /* Don't verify UDP checksum for packets with explicit zero checksum. */
862 good_udp0 |= udp0->checksum == 0;
863
864 /* Verify UDP length */
John Lo2b81eb82017-01-30 13:12:10 -0500865 if (is_ip4)
866 ip_len0 = clib_net_to_host_u16 (ip40->length);
867 else
868 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
John Lo37682e12016-11-30 12:51:39 -0500869 udp_len0 = clib_net_to_host_u16 (udp0->length);
John Lo37682e12016-11-30 12:51:39 -0500870 len_diff0 = ip_len0 - udp_len0;
871
872 /* Verify UDP checksum */
873 if (PREDICT_FALSE (!good_udp0))
874 {
Damjan Marion213b5aa2017-07-13 21:19:27 +0200875 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
John Lo2b81eb82017-01-30 13:12:10 -0500876 {
877 if (is_ip4)
878 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
879 else
880 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
881 good_udp0 =
Damjan Marion213b5aa2017-07-13 21:19:27 +0200882 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo2b81eb82017-01-30 13:12:10 -0500883 }
John Lo37682e12016-11-30 12:51:39 -0500884 }
885
John Lo2b81eb82017-01-30 13:12:10 -0500886 if (is_ip4)
887 {
888 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
889 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
890 }
891 else
892 {
893 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
894 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
895 }
John Lo37682e12016-11-30 12:51:39 -0500896
897 next0 = error0 ?
898 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
899 b0->error = error0 ? error_node->errors[error0] : 0;
900
John Lo2b81eb82017-01-30 13:12:10 -0500901 /* vxlan-input node expect current at VXLAN header */
902 if (is_ip4)
903 vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t));
904 else
905 vlib_buffer_advance (b0, sizeof(ip6_header_t)+sizeof(udp_header_t));
906
John Lo37682e12016-11-30 12:51:39 -0500907 exit0:
908 /* Process packet 1 */
909 if (proto1 != IP_PROTOCOL_UDP)
910 goto exit1; /* not UDP packet */
911
John Lo2b81eb82017-01-30 13:12:10 -0500912 if (is_ip4)
913 udp1 = ip4_next_header (ip41);
914 else
915 udp1 = ip6_next_header (ip61);
916
John Lo37682e12016-11-30 12:51:39 -0500917 if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
918 goto exit1; /* not VXLAN packet */
919
John Lo2b81eb82017-01-30 13:12:10 -0500920 /* Validate DIP against VTEPs*/
John Lo37682e12016-11-30 12:51:39 -0500921 if (is_ip4)
922 {
John Lo2b81eb82017-01-30 13:12:10 -0500923 if (addr4.as_u32 != ip41->dst_address.as_u32)
John Lo37682e12016-11-30 12:51:39 -0500924 {
John Lo2b81eb82017-01-30 13:12:10 -0500925 if (!hash_get (vxm->vtep4, ip41->dst_address.as_u32))
926 goto exit1; /* no local VTEP for VXLAN packet */
927 addr4 = ip41->dst_address;
928 }
John Lo37682e12016-11-30 12:51:39 -0500929 }
John Lo2b81eb82017-01-30 13:12:10 -0500930 else
931 {
932 if (!ip6_address_is_equal (&addr6, &ip61->dst_address))
933 {
934 if (!hash_get_mem (vxm->vtep6, &ip61->dst_address))
935 goto exit1; /* no local VTEP for VXLAN packet */
936 addr6 = ip61->dst_address;
937 }
938 }
John Lo37682e12016-11-30 12:51:39 -0500939
940 flags1 = b1->flags;
Damjan Marion213b5aa2017-07-13 21:19:27 +0200941 good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo37682e12016-11-30 12:51:39 -0500942
943 /* Don't verify UDP checksum for packets with explicit zero checksum. */
944 good_udp1 |= udp1->checksum == 0;
945
946 /* Verify UDP length */
John Lo2b81eb82017-01-30 13:12:10 -0500947 if (is_ip4)
948 ip_len1 = clib_net_to_host_u16 (ip41->length);
949 else
950 ip_len1 = clib_net_to_host_u16 (ip61->payload_length);
John Lo37682e12016-11-30 12:51:39 -0500951 udp_len1 = clib_net_to_host_u16 (udp1->length);
John Lo37682e12016-11-30 12:51:39 -0500952 len_diff1 = ip_len1 - udp_len1;
953
954 /* Verify UDP checksum */
955 if (PREDICT_FALSE (!good_udp1))
956 {
Damjan Marion213b5aa2017-07-13 21:19:27 +0200957 if ((flags1 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
John Lo2b81eb82017-01-30 13:12:10 -0500958 {
959 if (is_ip4)
960 flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
961 else
962 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
963 good_udp1 =
Damjan Marion213b5aa2017-07-13 21:19:27 +0200964 (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo2b81eb82017-01-30 13:12:10 -0500965 }
John Lo37682e12016-11-30 12:51:39 -0500966 }
967
John Lo2b81eb82017-01-30 13:12:10 -0500968 if (is_ip4)
969 {
970 error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM;
971 error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH;
972 }
973 else
974 {
Eyal Baria93ea422017-02-01 13:36:15 +0200975 error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM;
976 error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH;
John Lo2b81eb82017-01-30 13:12:10 -0500977 }
John Lo37682e12016-11-30 12:51:39 -0500978
979 next1 = error1 ?
980 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
981 b1->error = error1 ? error_node->errors[error1] : 0;
982
John Lo2b81eb82017-01-30 13:12:10 -0500983 /* vxlan-input node expect current at VXLAN header */
984 if (is_ip4)
985 vlib_buffer_advance (b1, sizeof(ip4_header_t)+sizeof(udp_header_t));
986 else
987 vlib_buffer_advance (b1, sizeof(ip6_header_t)+sizeof(udp_header_t));
988
John Lo37682e12016-11-30 12:51:39 -0500989 exit1:
990 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
991 to_next, n_left_to_next,
992 bi0, bi1, next0, next1);
993 }
994
995 while (n_left_from > 0 && n_left_to_next > 0)
996 {
997 vlib_buffer_t * b0;
John Lo2b81eb82017-01-30 13:12:10 -0500998 ip4_header_t * ip40;
999 ip6_header_t * ip60;
John Lo37682e12016-11-30 12:51:39 -05001000 udp_header_t * udp0;
1001 u32 bi0, ip_len0, udp_len0, flags0, next0;
1002 i32 len_diff0;
1003 u8 error0, good_udp0, proto0;
1004
1005 bi0 = to_next[0] = from[0];
1006 from += 1;
1007 n_left_from -= 1;
1008 to_next += 1;
1009 n_left_to_next -= 1;
1010
1011 b0 = vlib_get_buffer (vm, bi0);
John Lo2b81eb82017-01-30 13:12:10 -05001012 if (is_ip4)
1013 ip40 = vlib_buffer_get_current (b0);
1014 else
1015 ip60 = vlib_buffer_get_current (b0);
John Lo37682e12016-11-30 12:51:39 -05001016
1017 /* Setup packet for next IP feature */
1018 vnet_feature_next(vnet_buffer(b0)->sw_if_index[VLIB_RX], &next0, b0);
1019
John Lo2b81eb82017-01-30 13:12:10 -05001020 if (is_ip4)
1021 /* Treat IP4 frag packets as "experimental" protocol for now
1022 until support of IP frag reassembly is implemented */
1023 proto0 = ip4_is_fragment(ip40) ? 0xfe : ip40->protocol;
1024 else
1025 proto0 = ip60->protocol;
John Lo37682e12016-11-30 12:51:39 -05001026
1027 if (proto0 != IP_PROTOCOL_UDP)
1028 goto exit; /* not UDP packet */
1029
John Lo2b81eb82017-01-30 13:12:10 -05001030 if (is_ip4)
1031 udp0 = ip4_next_header (ip40);
1032 else
1033 udp0 = ip6_next_header (ip60);
1034
John Lo37682e12016-11-30 12:51:39 -05001035 if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
1036 goto exit; /* not VXLAN packet */
1037
John Lo2b81eb82017-01-30 13:12:10 -05001038 /* Validate DIP against VTEPs*/
John Lo37682e12016-11-30 12:51:39 -05001039 if (is_ip4)
1040 {
John Lo2b81eb82017-01-30 13:12:10 -05001041 if (addr4.as_u32 != ip40->dst_address.as_u32)
John Lo37682e12016-11-30 12:51:39 -05001042 {
John Lo2b81eb82017-01-30 13:12:10 -05001043 if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32))
1044 goto exit; /* no local VTEP for VXLAN packet */
1045 addr4 = ip40->dst_address;
1046 }
John Lo37682e12016-11-30 12:51:39 -05001047 }
John Lo2b81eb82017-01-30 13:12:10 -05001048 else
1049 {
1050 if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
1051 {
1052 if (!hash_get_mem (vxm->vtep6, &ip60->dst_address))
1053 goto exit; /* no local VTEP for VXLAN packet */
1054 addr6 = ip60->dst_address;
1055 }
1056 }
John Lo37682e12016-11-30 12:51:39 -05001057
1058 flags0 = b0->flags;
Damjan Marion213b5aa2017-07-13 21:19:27 +02001059 good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo37682e12016-11-30 12:51:39 -05001060
1061 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1062 good_udp0 |= udp0->checksum == 0;
1063
1064 /* Verify UDP length */
John Lo2b81eb82017-01-30 13:12:10 -05001065 if (is_ip4)
1066 ip_len0 = clib_net_to_host_u16 (ip40->length);
1067 else
1068 ip_len0 = clib_net_to_host_u16 (ip60->payload_length);
John Lo37682e12016-11-30 12:51:39 -05001069 udp_len0 = clib_net_to_host_u16 (udp0->length);
John Lo37682e12016-11-30 12:51:39 -05001070 len_diff0 = ip_len0 - udp_len0;
1071
1072 /* Verify UDP checksum */
1073 if (PREDICT_FALSE (!good_udp0))
1074 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001075 if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
John Lo2b81eb82017-01-30 13:12:10 -05001076 {
1077 if (is_ip4)
1078 flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
1079 else
1080 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
1081 good_udp0 =
Damjan Marion213b5aa2017-07-13 21:19:27 +02001082 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
John Lo2b81eb82017-01-30 13:12:10 -05001083 }
John Lo37682e12016-11-30 12:51:39 -05001084 }
1085
John Lo2b81eb82017-01-30 13:12:10 -05001086 if (is_ip4)
1087 {
1088 error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM;
1089 error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH;
1090 }
1091 else
1092 {
1093 error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM;
1094 error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH;
1095 }
John Lo37682e12016-11-30 12:51:39 -05001096
1097 next0 = error0 ?
1098 IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN;
1099 b0->error = error0 ? error_node->errors[error0] : 0;
1100
John Lo2b81eb82017-01-30 13:12:10 -05001101 /* vxlan-input node expect current at VXLAN header */
1102 if (is_ip4)
1103 vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t));
1104 else
1105 vlib_buffer_advance (b0, sizeof(ip6_header_t)+sizeof(udp_header_t));
1106
John Lo37682e12016-11-30 12:51:39 -05001107 exit:
1108 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1109 to_next, n_left_to_next,
1110 bi0, next0);
1111 }
1112
1113 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1114 }
1115
1116 return frame->n_vectors;
1117}
1118
1119static uword
1120ip4_vxlan_bypass (vlib_main_t * vm,
1121 vlib_node_runtime_t * node,
1122 vlib_frame_t * frame)
1123{
1124 return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 1);
1125}
1126
1127VLIB_REGISTER_NODE (ip4_vxlan_bypass_node) = {
1128 .function = ip4_vxlan_bypass,
1129 .name = "ip4-vxlan-bypass",
1130 .vector_size = sizeof (u32),
1131
1132 .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
1133 .next_nodes = {
1134 [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
1135 [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan4-input",
1136 },
1137
1138 .format_buffer = format_ip4_header,
1139 .format_trace = format_ip4_forward_next_trace,
1140};
1141
1142VLIB_NODE_FUNCTION_MULTIARCH (ip4_vxlan_bypass_node,ip4_vxlan_bypass)
1143
John Lo37682e12016-11-30 12:51:39 -05001144/* Dummy init function to get us linked in. */
1145clib_error_t * ip4_vxlan_bypass_init (vlib_main_t * vm)
1146{ return 0; }
1147
1148VLIB_INIT_FUNCTION (ip4_vxlan_bypass_init);
John Lo2b81eb82017-01-30 13:12:10 -05001149
1150static uword
1151ip6_vxlan_bypass (vlib_main_t * vm,
1152 vlib_node_runtime_t * node,
1153 vlib_frame_t * frame)
1154{
1155 return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 0);
1156}
1157
1158VLIB_REGISTER_NODE (ip6_vxlan_bypass_node) = {
1159 .function = ip6_vxlan_bypass,
1160 .name = "ip6-vxlan-bypass",
1161 .vector_size = sizeof (u32),
1162
1163 .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT,
1164 .next_nodes = {
1165 [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop",
1166 [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan6-input",
1167 },
1168
1169 .format_buffer = format_ip6_header,
1170 .format_trace = format_ip6_forward_next_trace,
1171};
1172
1173VLIB_NODE_FUNCTION_MULTIARCH (ip6_vxlan_bypass_node,ip6_vxlan_bypass)
1174
1175/* Dummy init function to get us linked in. */
1176clib_error_t * ip6_vxlan_bypass_init (vlib_main_t * vm)
1177{ return 0; }
1178
1179VLIB_INIT_FUNCTION (ip6_vxlan_bypass_init);