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.
15 #include <vppinfra/error.h>
16 #include <vppinfra/hash.h>
17 #include <vnet/vnet.h>
18 #include <vnet/ip/ip.h>
19 #include <vnet/ethernet/ethernet.h>
20 #include <vnet/vxlan-gpe/vxlan_gpe.h>
22 /* Statistics (not really errors) */
23 #define foreach_vxlan_gpe_encap_error \
24 _(ENCAPSULATED, "good packets encapsulated")
26 static char * vxlan_gpe_encap_error_strings[] = {
27 #define _(sym,string) string,
28 foreach_vxlan_gpe_encap_error
33 #define _(sym,str) VXLAN_GPE_ENCAP_ERROR_##sym,
34 foreach_vxlan_gpe_encap_error
36 VXLAN_GPE_ENCAP_N_ERROR,
37 } vxlan_gpe_encap_error_t;
40 VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP,
41 VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP,
42 VXLAN_GPE_ENCAP_NEXT_DROP,
43 VXLAN_GPE_ENCAP_N_NEXT
44 } vxlan_gpe_encap_next_t;
48 } vxlan_gpe_encap_trace_t;
51 u8 * format_vxlan_gpe_encap_trace (u8 * s, va_list * args)
53 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
54 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
55 vxlan_gpe_encap_trace_t * t
56 = va_arg (*args, vxlan_gpe_encap_trace_t *);
58 s = format (s, "VXLAN-GPE-ENCAP: tunnel %d", t->tunnel_index);
63 vxlan_gpe_encap_one_inline (vxlan_gpe_main_t * ngm, vlib_buffer_t * b0,
64 vxlan_gpe_tunnel_t * t0, u32 * next0, u8 is_v4)
66 ASSERT(sizeof(ip4_vxlan_gpe_header_t) == 36);
67 ASSERT(sizeof(ip6_vxlan_gpe_header_t) == 56);
71 ip_udp_encap_one (ngm->vlib_main, b0, t0->rewrite, 36, 1);
72 next0[0] = VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
77 ip_udp_encap_one (ngm->vlib_main, b0, t0->rewrite, 56, 0);
78 next0[0] = VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP;
83 vxlan_gpe_encap_two_inline (vxlan_gpe_main_t * ngm, vlib_buffer_t * b0, vlib_buffer_t * b1,
84 vxlan_gpe_tunnel_t * t0, vxlan_gpe_tunnel_t * t1, u32 * next0,
85 u32 * next1, u8 is_v4)
87 ASSERT(sizeof(ip4_vxlan_gpe_header_t) == 36);
88 ASSERT(sizeof(ip6_vxlan_gpe_header_t) == 56);
92 ip_udp_encap_one (ngm->vlib_main, b0, t0->rewrite, 36, 1);
93 ip_udp_encap_one (ngm->vlib_main, b1, t1->rewrite, 36, 1);
94 next0[0] = next1[0] = VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
98 ip_udp_encap_one (ngm->vlib_main, b0, t0->rewrite, 56, 0);
99 ip_udp_encap_one (ngm->vlib_main, b1, t1->rewrite, 56, 0);
100 next0[0] = next1[0] = VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP;
105 vxlan_gpe_encap (vlib_main_t * vm,
106 vlib_node_runtime_t * node,
107 vlib_frame_t * from_frame)
109 u32 n_left_from, next_index, *from, *to_next;
110 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
111 vnet_main_t * vnm = ngm->vnet_main;
112 vnet_interface_main_t * im = &vnm->interface_main;
113 u32 pkts_encapsulated = 0;
114 u32 cpu_index = os_get_cpu_number ();
115 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
117 from = vlib_frame_vector_args (from_frame);
118 n_left_from = from_frame->n_vectors;
120 next_index = node->cached_next_index;
121 stats_sw_if_index = node->runtime_data[0];
122 stats_n_packets = stats_n_bytes = 0;
124 while (n_left_from > 0)
128 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
130 while (n_left_from >= 4 && n_left_to_next >= 2)
133 vlib_buffer_t * b0, *b1;
135 u32 sw_if_index0, sw_if_index1, len0, len1;
136 vnet_hw_interface_t * hi0, *hi1;
137 vxlan_gpe_tunnel_t * t0, *t1;
138 u8 is_ip4_0, is_ip4_1;
140 next0 = next1 = VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
142 /* Prefetch next iteration. */
144 vlib_buffer_t * p2, *p3;
146 p2 = vlib_get_buffer (vm, from[2]);
147 p3 = vlib_get_buffer (vm, from[3]);
149 vlib_prefetch_buffer_header(p2, LOAD);
150 vlib_prefetch_buffer_header(p3, LOAD);
152 CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
153 CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
165 b0 = vlib_get_buffer (vm, bi0);
166 b1 = vlib_get_buffer (vm, bi1);
169 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
170 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_TX];
171 hi0 = vnet_get_sup_hw_interface (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
172 hi1 = vnet_get_sup_hw_interface (vnm, vnet_buffer(b1)->sw_if_index[VLIB_TX]);
174 t0 = pool_elt_at_index(ngm->tunnels, hi0->dev_instance);
175 t1 = pool_elt_at_index(ngm->tunnels, hi1->dev_instance);
177 is_ip4_0 = (t0->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
178 is_ip4_1 = (t1->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
180 if (PREDICT_TRUE(is_ip4_0 == is_ip4_1))
182 vxlan_gpe_encap_two_inline (ngm, b0, b1, t0, t1, &next0, &next1,is_ip4_0);
186 vxlan_gpe_encap_one_inline (ngm, b0, t0, &next0, is_ip4_0);
187 vxlan_gpe_encap_one_inline (ngm, b1, t1, &next1, is_ip4_1);
190 /* Reset to look up tunnel partner in the configured FIB */
191 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
192 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
193 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
194 vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
195 pkts_encapsulated += 2;
197 len0 = vlib_buffer_length_in_chain (vm, b0);
198 len1 = vlib_buffer_length_in_chain (vm, b0);
199 stats_n_packets += 2;
200 stats_n_bytes += len0 + len1;
202 /* Batch stats increment on the same vxlan tunnel so counter is not
203 incremented per packet. Note stats are still incremented for deleted
204 and admin-down tunnel where packets are dropped. It is not worthwhile
205 to check for this rare case and affect normal path performance. */
206 if (PREDICT_FALSE((sw_if_index0 != stats_sw_if_index)
207 || (sw_if_index1 != stats_sw_if_index)))
209 stats_n_packets -= 2;
210 stats_n_bytes -= len0 + len1;
211 if (sw_if_index0 == sw_if_index1)
214 vlib_increment_combined_counter (
215 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
216 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
217 stats_sw_if_index = sw_if_index0;
219 stats_n_bytes = len0 + len1;
223 vlib_increment_combined_counter (
224 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
225 cpu_index, sw_if_index0, 1, len0);
226 vlib_increment_combined_counter (
227 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
228 cpu_index, sw_if_index1, 1, len1);
232 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
234 vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof(*tr));
235 tr->tunnel_index = t0 - ngm->tunnels;
238 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
240 vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b1,
242 tr->tunnel_index = t1 - ngm->tunnels;
245 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
246 n_left_to_next, bi0, bi1, next0, next1);
249 while (n_left_from > 0 && n_left_to_next > 0)
253 u32 next0 = VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
254 u32 sw_if_index0, len0;
255 vnet_hw_interface_t * hi0;
256 vxlan_gpe_tunnel_t * t0;
266 b0 = vlib_get_buffer (vm, bi0);
269 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
270 hi0 = vnet_get_sup_hw_interface (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
272 t0 = pool_elt_at_index(ngm->tunnels, hi0->dev_instance);
274 is_ip4_0 = (t0->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
276 vxlan_gpe_encap_one_inline (ngm, b0, t0, &next0, is_ip4_0);
278 /* Reset to look up tunnel partner in the configured FIB */
279 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
280 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
283 len0 = vlib_buffer_length_in_chain (vm, b0);
284 stats_n_packets += 1;
285 stats_n_bytes += len0;
287 /* Batch stats increment on the same vxlan tunnel so counter is not
288 * incremented per packet. Note stats are still incremented for deleted
289 * and admin-down tunnel where packets are dropped. It is not worthwhile
290 * to check for this rare case and affect normal path performance. */
291 if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
293 stats_n_packets -= 1;
294 stats_n_bytes -= len0;
296 vlib_increment_combined_counter (
297 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
298 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
300 stats_n_bytes = len0;
301 stats_sw_if_index = sw_if_index0;
303 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
305 vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b0,
307 tr->tunnel_index = t0 - ngm->tunnels;
309 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
310 n_left_to_next, bi0, next0);
313 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
315 vlib_node_increment_counter (vm, node->node_index,
316 VXLAN_GPE_ENCAP_ERROR_ENCAPSULATED,
318 /* Increment any remaining batch stats */
321 vlib_increment_combined_counter (
322 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, cpu_index,
323 stats_sw_if_index, stats_n_packets, stats_n_bytes);
324 node->runtime_data[0] = stats_sw_if_index;
327 return from_frame->n_vectors;
330 VLIB_REGISTER_NODE (vxlan_gpe_encap_node) = {
331 .function = vxlan_gpe_encap,
332 .name = "vxlan-gpe-encap",
333 .vector_size = sizeof (u32),
334 .format_trace = format_vxlan_gpe_encap_trace,
335 .type = VLIB_NODE_TYPE_INTERNAL,
337 .n_errors = ARRAY_LEN(vxlan_gpe_encap_error_strings),
338 .error_strings = vxlan_gpe_encap_error_strings,
340 .n_next_nodes = VXLAN_GPE_ENCAP_N_NEXT,
343 [VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
344 [VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP] = "ip6-lookup",
345 [VXLAN_GPE_ENCAP_NEXT_DROP] = "error-drop",