2 * decap.c: vxlan tunnel decap packet processing
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/vxlan/vxlan.h>
22 vlib_node_registration_t vxlan_input_node;
31 static u8 * format_vxlan_rx_trace (u8 * s, va_list * args)
33 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
34 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
35 vxlan_rx_trace_t * t = va_arg (*args, vxlan_rx_trace_t *);
37 if (t->tunnel_index != ~0)
39 s = format (s, "VXLAN: tunnel %d vni %d next %d error %d",
40 t->tunnel_index, t->vni, t->next_index, t->error);
44 s = format (s, "VXLAN: no tunnel for vni %d next %d error %d",
45 t->vni, t->next_index, t->error);
51 vxlan_input (vlib_main_t * vm,
52 vlib_node_runtime_t * node,
53 vlib_frame_t * from_frame)
55 u32 n_left_from, next_index, * from, * to_next;
56 vxlan_main_t * vxm = &vxlan_main;
57 vnet_main_t * vnm = vxm->vnet_main;
58 vnet_interface_main_t * im = &vnm->interface_main;
59 u32 last_tunnel_index = ~0;
60 vxlan_tunnel_key_t last_key;
61 u32 pkts_decapsulated = 0;
62 u32 cpu_index = os_get_cpu_number();
63 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
67 from = vlib_frame_vector_args (from_frame);
68 n_left_from = from_frame->n_vectors;
70 next_index = node->cached_next_index;
71 stats_sw_if_index = node->runtime_data[0];
72 stats_n_packets = stats_n_bytes = 0;
74 while (n_left_from > 0)
78 vlib_get_next_frame (vm, node, next_index,
79 to_next, n_left_to_next);
80 while (n_left_from >= 4 && n_left_to_next >= 2)
83 vlib_buffer_t * b0, * b1;
85 ip4_header_t * ip0, * ip1;
86 vxlan_header_t * vxlan0, * vxlan1;
88 u32 tunnel_index0, tunnel_index1;
89 vxlan_tunnel_t * t0, * t1;
90 vxlan_tunnel_key_t key0, key1;
92 u32 sw_if_index0, sw_if_index1, len0, len1;
94 /* Prefetch next iteration. */
96 vlib_buffer_t * p2, * p3;
98 p2 = vlib_get_buffer (vm, from[2]);
99 p3 = vlib_get_buffer (vm, from[3]);
101 vlib_prefetch_buffer_header (p2, LOAD);
102 vlib_prefetch_buffer_header (p3, LOAD);
104 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
105 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
117 b0 = vlib_get_buffer (vm, bi0);
118 b1 = vlib_get_buffer (vm, bi1);
120 /* udp leaves current_data pointing at the vxlan header */
121 vxlan0 = vlib_buffer_get_current (b0);
122 vxlan1 = vlib_buffer_get_current (b1);
125 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
127 (b1, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
128 ip0 = vlib_buffer_get_current (b0);
129 ip1 = vlib_buffer_get_current (b1);
131 /* pop (ip, udp, vxlan) */
133 (b0, sizeof(*ip0)+sizeof(udp_header_t)+sizeof(*vxlan0));
135 (b1, sizeof(*ip1)+sizeof(udp_header_t)+sizeof(*vxlan1));
143 key0.src = ip0->src_address.as_u32;
144 key0.vni = vxlan0->vni_reserved;
146 if (PREDICT_FALSE (key0.as_u64 != last_key.as_u64))
148 p0 = hash_get (vxm->vxlan_tunnel_by_key, key0.as_u64);
152 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
153 next0 = VXLAN_INPUT_NEXT_DROP;
157 last_key.as_u64 = key0.as_u64;
158 tunnel_index0 = last_tunnel_index = p0[0];
161 tunnel_index0 = last_tunnel_index;
163 t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
165 next0 = t0->decap_next_index;
166 sw_if_index0 = t0->sw_if_index;
167 len0 = vlib_buffer_length_in_chain (vm, b0);
169 /* Required to make the l2 tag push / pop code work on l2 subifs */
170 vnet_update_l2_len (b0);
172 /* Set input sw_if_index to VXLAN tunnel for learning */
173 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
175 pkts_decapsulated ++;
176 stats_n_packets += 1;
177 stats_n_bytes += len0;
179 /* Batch stats increment on the same vxlan tunnel so counter
180 is not incremented per packet */
181 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
183 stats_n_packets -= 1;
184 stats_n_bytes -= len0;
186 vlib_increment_combined_counter
187 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
188 cpu_index, stats_sw_if_index,
189 stats_n_packets, stats_n_bytes);
191 stats_n_bytes = len0;
192 stats_sw_if_index = sw_if_index0;
196 b0->error = error0 ? node->errors[error0] : 0;
198 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
201 = vlib_add_trace (vm, node, b0, sizeof (*tr));
202 tr->next_index = next0;
204 tr->tunnel_index = tunnel_index0;
205 tr->vni = vnet_get_vni (vxlan0);
208 key1.src = ip1->src_address.as_u32;
209 key1.vni = vxlan1->vni_reserved;
211 if (PREDICT_FALSE (key1.as_u64 != last_key.as_u64))
213 p1 = hash_get (vxm->vxlan_tunnel_by_key, key1.as_u64);
217 error1 = VXLAN_ERROR_NO_SUCH_TUNNEL;
218 next1 = VXLAN_INPUT_NEXT_DROP;
222 last_key.as_u64 = key1.as_u64;
223 tunnel_index1 = last_tunnel_index = p1[0];
226 tunnel_index1 = last_tunnel_index;
228 t1 = pool_elt_at_index (vxm->tunnels, tunnel_index1);
230 next1 = t1->decap_next_index;
231 sw_if_index1 = t1->sw_if_index;
232 len1 = vlib_buffer_length_in_chain (vm, b1);
234 /* Required to make the l2 tag push / pop code work on l2 subifs */
235 vnet_update_l2_len (b1);
237 /* Set input sw_if_index to VXLAN tunnel for learning */
238 vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
240 pkts_decapsulated ++;
241 stats_n_packets += 1;
242 stats_n_bytes += len1;
244 /* Batch stats increment on the same vxlan tunnel so counter
245 is not incremented per packet */
246 if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
248 stats_n_packets -= 1;
249 stats_n_bytes -= len1;
251 vlib_increment_combined_counter
252 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
253 cpu_index, stats_sw_if_index,
254 stats_n_packets, stats_n_bytes);
256 stats_n_bytes = len1;
257 stats_sw_if_index = sw_if_index1;
261 b1->error = error1 ? node->errors[error1] : 0;
263 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
266 = vlib_add_trace (vm, node, b1, sizeof (*tr));
267 tr->next_index = next1;
269 tr->tunnel_index = tunnel_index1;
270 tr->vni = vnet_get_vni (vxlan1);
273 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
274 to_next, n_left_to_next,
275 bi0, bi1, next0, next1);
278 while (n_left_from > 0 && n_left_to_next > 0)
284 vxlan_header_t * vxlan0;
288 vxlan_tunnel_key_t key0;
290 u32 sw_if_index0, len0;
299 b0 = vlib_get_buffer (vm, bi0);
301 /* udp leaves current_data pointing at the vxlan header */
302 vxlan0 = vlib_buffer_get_current (b0);
305 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
306 ip0 = vlib_buffer_get_current (b0);
308 /* pop (ip, udp, vxlan) */
310 (b0, sizeof(*ip0)+sizeof(udp_header_t)+sizeof(*vxlan0));
315 key0.src = ip0->src_address.as_u32;
316 key0.vni = vxlan0->vni_reserved;
318 if (PREDICT_FALSE (key0.as_u64 != last_key.as_u64))
320 p0 = hash_get (vxm->vxlan_tunnel_by_key, key0.as_u64);
324 error0 = VXLAN_ERROR_NO_SUCH_TUNNEL;
325 next0 = VXLAN_INPUT_NEXT_DROP;
329 last_key.as_u64 = key0.as_u64;
330 tunnel_index0 = last_tunnel_index = p0[0];
333 tunnel_index0 = last_tunnel_index;
335 t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0);
337 next0 = t0->decap_next_index;
338 sw_if_index0 = t0->sw_if_index;
339 len0 = vlib_buffer_length_in_chain (vm, b0);
341 /* Required to make the l2 tag push / pop code work on l2 subifs */
342 vnet_update_l2_len (b0);
344 /* Set input sw_if_index to VXLAN tunnel for learning */
345 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
347 pkts_decapsulated ++;
348 stats_n_packets += 1;
349 stats_n_bytes += len0;
351 /* Batch stats increment on the same vxlan tunnel so counter
352 is not incremented per packet */
353 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
355 stats_n_packets -= 1;
356 stats_n_bytes -= len0;
358 vlib_increment_combined_counter
359 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
360 cpu_index, stats_sw_if_index,
361 stats_n_packets, stats_n_bytes);
363 stats_n_bytes = len0;
364 stats_sw_if_index = sw_if_index0;
368 b0->error = error0 ? node->errors[error0] : 0;
370 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
373 = vlib_add_trace (vm, node, b0, sizeof (*tr));
374 tr->next_index = next0;
376 tr->tunnel_index = tunnel_index0;
377 tr->vni = vnet_get_vni (vxlan0);
379 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
380 to_next, n_left_to_next,
384 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
386 /* Do we still need this now that tunnel tx stats is kept? */
387 vlib_node_increment_counter (vm, vxlan_input_node.index,
388 VXLAN_ERROR_DECAPSULATED,
391 /* Increment any remaining batch stats */
394 vlib_increment_combined_counter
395 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
396 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
397 node->runtime_data[0] = stats_sw_if_index;
400 return from_frame->n_vectors;
403 static char * vxlan_error_strings[] = {
404 #define vxlan_error(n,s) s,
405 #include <vnet/vxlan/vxlan_error.def>
410 VLIB_REGISTER_NODE (vxlan_input_node) = {
411 .function = vxlan_input,
412 .name = "vxlan-input",
413 /* Takes a vector of packets. */
414 .vector_size = sizeof (u32),
416 .n_errors = VXLAN_N_ERROR,
417 .error_strings = vxlan_error_strings,
419 .n_next_nodes = VXLAN_INPUT_N_NEXT,
421 #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n,
422 foreach_vxlan_input_next
426 //temp .format_buffer = format_vxlan_header,
427 .format_trace = format_vxlan_rx_trace,
428 // $$$$ .unformat_buffer = unformat_vxlan_header,