2 * Copyright (c) 2015 Cisco and/or its affiliates.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief Functions for encapsulating VXLAN GPE tunnels
20 #include <vppinfra/error.h>
21 #include <vppinfra/hash.h>
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/udp/udp_inlines.h>
26 #include <vnet/vxlan-gpe/vxlan_gpe.h>
28 /** Statistics (not really errors) */
29 #define foreach_vxlan_gpe_encap_error \
30 _(ENCAPSULATED, "good packets encapsulated")
33 * @brief VXLAN GPE encap error strings
35 static char *vxlan_gpe_encap_error_strings[] = {
36 #define _(sym,string) string,
37 foreach_vxlan_gpe_encap_error
42 * @brief Struct for VXLAN GPE errors/counters
46 #define _(sym,str) VXLAN_GPE_ENCAP_ERROR_##sym,
47 foreach_vxlan_gpe_encap_error
49 VXLAN_GPE_ENCAP_N_ERROR,
50 } vxlan_gpe_encap_error_t;
53 * @brief Struct for tracing VXLAN GPE encapsulated packets
58 } vxlan_gpe_encap_trace_t;
61 * @brief Trace of packets encapsulated in VXLAN GPE
70 format_vxlan_gpe_encap_trace (u8 * s, va_list * args)
72 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
73 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
74 vxlan_gpe_encap_trace_t *t = va_arg (*args, vxlan_gpe_encap_trace_t *);
76 s = format (s, "VXLAN-GPE-ENCAP: tunnel %d", t->tunnel_index);
81 * @brief Instantiates UDP + VXLAN-GPE header then set next node to IP4|6 lookup
85 * @param *t0 contains rewrite header
86 * @param *next0 relative index of next dispatch function (next node)
87 * @param is_v4 Is this IPv4? (or IPv6)
91 vxlan_gpe_encap_one_inline (vxlan_gpe_main_t *ngm, vlib_buffer_t *b0,
92 vxlan_gpe_tunnel_t *t0, u32 *next0,
93 ip_address_family_t af)
95 ASSERT (sizeof (ip4_vxlan_gpe_header_t) == 36);
96 ASSERT (sizeof (ip6_vxlan_gpe_header_t) == 56);
98 ip_udp_encap_one (ngm->vlib_main, b0, t0->rewrite, t0->rewrite_size, af,
99 N_AF, UDP_ENCAP_FIXUP_NONE);
100 next0[0] = t0->encap_next_node;
104 * @brief Instantiates UDP + VXLAN-GPE header then set next node to IP4|6 lookup for two packets
109 * @param *t0 contains rewrite header for Packet0
110 * @param *t1 contains rewrite header for Packet1
111 * @param *next0 relative index of next dispatch function (next node) for Packet0
112 * @param *next1 relative index of next dispatch function (next node) for Packet1
113 * @param is_v4 Is this IPv4? (or IPv6)
117 vxlan_gpe_encap_two_inline (vxlan_gpe_main_t *ngm, vlib_buffer_t *b0,
118 vlib_buffer_t *b1, vxlan_gpe_tunnel_t *t0,
119 vxlan_gpe_tunnel_t *t1, u32 *next0, u32 *next1,
120 ip_address_family_t af)
122 ASSERT (sizeof (ip4_vxlan_gpe_header_t) == 36);
123 ASSERT (sizeof (ip6_vxlan_gpe_header_t) == 56);
125 ip_udp_encap_one (ngm->vlib_main, b0, t0->rewrite, t0->rewrite_size, af,
126 N_AF, UDP_ENCAP_FIXUP_NONE);
127 ip_udp_encap_one (ngm->vlib_main, b1, t1->rewrite, t1->rewrite_size, af,
128 N_AF, UDP_ENCAP_FIXUP_NONE);
129 next0[0] = next1[0] = t0->encap_next_node;
133 * @brief Common processing for IPv4 and IPv6 VXLAN GPE encap dispatch functions
135 * It is worth noting that other than trivial UDP forwarding (transit), VXLAN GPE
136 * tunnels are "establish local". This means that we don't have a TX interface as yet
137 * as we need to look up where the outer-header dest is. By setting the TX index in the
138 * buffer metadata to the encap FIB, we can do a lookup to get the adjacency and real TX.
140 * vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
142 * @node vxlan-gpe-input
147 * @return from_frame->n_vectors
151 vxlan_gpe_encap (vlib_main_t * vm,
152 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
154 u32 n_left_from, next_index, *from, *to_next;
155 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
156 vnet_main_t *vnm = ngm->vnet_main;
157 vnet_interface_main_t *im = &vnm->interface_main;
158 u32 pkts_encapsulated = 0;
159 u32 thread_index = vm->thread_index;
160 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
161 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
163 from = vlib_frame_vector_args (from_frame);
164 n_left_from = from_frame->n_vectors;
166 next_index = node->cached_next_index;
167 stats_sw_if_index = node->runtime_data[0];
168 stats_n_packets = stats_n_bytes = 0;
169 vlib_get_buffers (vm, from, bufs, n_left_from);
171 while (n_left_from > 0)
174 u32 sw_if_index0 = ~0, sw_if_index1 = ~0, len0, len1;
175 vnet_hw_interface_t *hi0, *hi1;
176 vxlan_gpe_tunnel_t *t0 = NULL, *t1 = NULL;
177 ip_address_family_t af_0 = AF_IP4, af_1 = AF_IP4;
179 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
181 while (n_left_from >= 4 && n_left_to_next >= 2)
186 next0 = next1 = VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
188 /* Prefetch next iteration. */
190 vlib_prefetch_buffer_header (b[2], LOAD);
191 vlib_prefetch_buffer_header (b[3], LOAD);
193 CLIB_PREFETCH (b[2]->data - CLIB_CACHE_LINE_BYTES,
194 2 * CLIB_CACHE_LINE_BYTES, LOAD);
195 CLIB_PREFETCH (b[3]->data - CLIB_CACHE_LINE_BYTES,
196 2 * CLIB_CACHE_LINE_BYTES, LOAD);
209 if (sw_if_index0 != vnet_buffer (b[0])->sw_if_index[VLIB_TX])
211 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
213 vnet_get_sup_hw_interface (vnm,
214 vnet_buffer (b[0])->sw_if_index
216 t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
217 af_0 = (t0->flags & VXLAN_GPE_TUNNEL_IS_IPV4 ? AF_IP4 : AF_IP6);
221 if (sw_if_index1 != vnet_buffer (b[1])->sw_if_index[VLIB_TX])
223 if (sw_if_index0 == vnet_buffer (b[1])->sw_if_index[VLIB_TX])
225 sw_if_index1 = sw_if_index0;
232 sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
234 vnet_get_sup_hw_interface (vnm,
235 vnet_buffer (b[1])->sw_if_index
237 t1 = pool_elt_at_index (ngm->tunnels, hi1->dev_instance);
239 (t1->flags & VXLAN_GPE_TUNNEL_IS_IPV4 ? AF_IP4 : AF_IP6);
243 if (PREDICT_TRUE (af_0 == af_1))
245 vxlan_gpe_encap_two_inline (ngm, b[0], b[1], t0, t1, &next0,
250 vxlan_gpe_encap_one_inline (ngm, b[0], t0, &next0, af_0);
251 vxlan_gpe_encap_one_inline (ngm, b[1], t1, &next1, af_1);
254 /* Reset to look up tunnel partner in the configured FIB */
255 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = t0->encap_fib_index;
256 vnet_buffer (b[1])->sw_if_index[VLIB_TX] = t1->encap_fib_index;
257 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = sw_if_index0;
258 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = sw_if_index1;
259 pkts_encapsulated += 2;
261 len0 = vlib_buffer_length_in_chain (vm, b[0]);
262 len1 = vlib_buffer_length_in_chain (vm, b[1]);
263 stats_n_packets += 2;
264 stats_n_bytes += len0 + len1;
266 /* Batch stats increment on the same vxlan tunnel so counter is not
267 incremented per packet. Note stats are still incremented for deleted
268 and admin-down tunnel where packets are dropped. It is not worthwhile
269 to check for this rare case and affect normal path performance. */
270 if (PREDICT_FALSE ((sw_if_index0 != stats_sw_if_index)
271 || (sw_if_index1 != stats_sw_if_index)))
273 stats_n_packets -= 2;
274 stats_n_bytes -= len0 + len1;
275 if (sw_if_index0 == sw_if_index1)
278 vlib_increment_combined_counter
279 (im->combined_sw_if_counters +
280 VNET_INTERFACE_COUNTER_TX, thread_index,
281 stats_sw_if_index, stats_n_packets, stats_n_bytes);
282 stats_sw_if_index = sw_if_index0;
284 stats_n_bytes = len0 + len1;
288 vlib_increment_combined_counter (im->combined_sw_if_counters
290 VNET_INTERFACE_COUNTER_TX,
291 thread_index, sw_if_index0,
293 vlib_increment_combined_counter (im->combined_sw_if_counters
295 VNET_INTERFACE_COUNTER_TX,
296 thread_index, sw_if_index1,
301 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
303 vxlan_gpe_encap_trace_t *tr =
304 vlib_add_trace (vm, node, b[0], sizeof (*tr));
305 tr->tunnel_index = t0 - ngm->tunnels;
308 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
310 vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b[1],
312 tr->tunnel_index = t1 - ngm->tunnels;
316 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
317 n_left_to_next, bi0, bi1, next0,
321 while (n_left_from > 0 && n_left_to_next > 0)
324 u32 next0 = VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
334 if (sw_if_index0 != vnet_buffer (b[0])->sw_if_index[VLIB_TX])
336 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
338 vnet_get_sup_hw_interface (vnm,
339 vnet_buffer (b[0])->sw_if_index
342 t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
344 af_0 = (t0->flags & VXLAN_GPE_TUNNEL_IS_IPV4 ? AF_IP4 : AF_IP6);
347 vxlan_gpe_encap_one_inline (ngm, b[0], t0, &next0, af_0);
349 /* Reset to look up tunnel partner in the configured FIB */
350 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = t0->encap_fib_index;
351 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = sw_if_index0;
354 len0 = vlib_buffer_length_in_chain (vm, b[0]);
355 stats_n_packets += 1;
356 stats_n_bytes += len0;
358 /* Batch stats increment on the same vxlan tunnel so counter is not
359 * incremented per packet. Note stats are still incremented for deleted
360 * and admin-down tunnel where packets are dropped. It is not worthwhile
361 * to check for this rare case and affect normal path performance. */
362 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
364 stats_n_packets -= 1;
365 stats_n_bytes -= len0;
367 vlib_increment_combined_counter (im->combined_sw_if_counters +
368 VNET_INTERFACE_COUNTER_TX,
374 stats_n_bytes = len0;
375 stats_sw_if_index = sw_if_index0;
377 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
379 vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b[0],
381 tr->tunnel_index = t0 - ngm->tunnels;
385 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
386 n_left_to_next, bi0, next0);
389 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
391 vlib_node_increment_counter (vm, node->node_index,
392 VXLAN_GPE_ENCAP_ERROR_ENCAPSULATED,
394 /* Increment any remaining batch stats */
397 vlib_increment_combined_counter (im->combined_sw_if_counters +
398 VNET_INTERFACE_COUNTER_TX,
399 thread_index, stats_sw_if_index,
400 stats_n_packets, stats_n_bytes);
401 node->runtime_data[0] = stats_sw_if_index;
404 return from_frame->n_vectors;
408 VLIB_REGISTER_NODE (vxlan_gpe_encap_node) = {
409 .function = vxlan_gpe_encap,
410 .name = "vxlan-gpe-encap",
411 .vector_size = sizeof (u32),
412 .format_trace = format_vxlan_gpe_encap_trace,
413 .type = VLIB_NODE_TYPE_INTERNAL,
415 .n_errors = ARRAY_LEN(vxlan_gpe_encap_error_strings),
416 .error_strings = vxlan_gpe_encap_error_strings,
418 .n_next_nodes = VXLAN_GPE_ENCAP_N_NEXT,
421 [VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
422 [VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP] = "ip6-lookup",
423 [VXLAN_GPE_ENCAP_NEXT_DROP] = "error-drop",
430 * fd.io coding-style-patch-verification: ON
433 * eval: (c-set-style "gnu")