blob: 90854ad097ea8170d34869a77dc4e841cac50eee [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
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#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/vxlan.h>
21
22/* Statistics (not all errors) */
23#define foreach_vxlan_encap_error \
24_(ENCAPSULATED, "good packets encapsulated") \
25_(DEL_TUNNEL, "deleted tunnel packets")
26
27static char * vxlan_encap_error_strings[] = {
28#define _(sym,string) string,
29 foreach_vxlan_encap_error
30#undef _
31};
32
33typedef enum {
34#define _(sym,str) VXLAN_ENCAP_ERROR_##sym,
35 foreach_vxlan_encap_error
36#undef _
37 VXLAN_ENCAP_N_ERROR,
38} vxlan_encap_error_t;
39
40typedef enum {
41 VXLAN_ENCAP_NEXT_IP4_LOOKUP,
42 VXLAN_ENCAP_NEXT_DROP,
43 VXLAN_ENCAP_N_NEXT,
44} vxlan_encap_next_t;
45
46typedef struct {
47 u32 tunnel_index;
48 u32 vni;
49} vxlan_encap_trace_t;
50
51u8 * format_vxlan_encap_trace (u8 * s, va_list * args)
52{
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_encap_trace_t * t
56 = va_arg (*args, vxlan_encap_trace_t *);
57
58 s = format (s, "VXLAN-ENCAP: tunnel %d vni %d", t->tunnel_index, t->vni);
59 return s;
60}
61
62#define foreach_fixed_header_offset \
63 _(0) _(1) _(2) _(3)
64
65static uword
66vxlan_encap (vlib_main_t * vm,
67 vlib_node_runtime_t * node,
68 vlib_frame_t * from_frame)
69{
70 u32 n_left_from, next_index, * from, * to_next;
71 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 pkts_encapsulated = 0;
75 u16 old_l0 = 0, old_l1 = 0;
76 u32 cpu_index = os_get_cpu_number();
77 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
78
79 from = vlib_frame_vector_args (from_frame);
80 n_left_from = from_frame->n_vectors;
81
82 next_index = node->cached_next_index;
83 stats_sw_if_index = node->runtime_data[0];
84 stats_n_packets = stats_n_bytes = 0;
85
86 while (n_left_from > 0)
87 {
88 u32 n_left_to_next;
89
90 vlib_get_next_frame (vm, node, next_index,
91 to_next, n_left_to_next);
92
93 while (n_left_from >= 4 && n_left_to_next >= 2)
94 {
95 u32 bi0, bi1;
96 vlib_buffer_t * b0, * b1;
97 u32 flow_hash0, flow_hash1;
98 u32 next0 = VXLAN_ENCAP_NEXT_IP4_LOOKUP;
99 u32 next1 = VXLAN_ENCAP_NEXT_IP4_LOOKUP;
100 u32 sw_if_index0, sw_if_index1, len0, len1;
101 vnet_hw_interface_t * hi0, * hi1;
102 ip4_header_t * ip0, * ip1;
103 udp_header_t * udp0, * udp1;
104 u64 * copy_src0, * copy_dst0;
105 u64 * copy_src1, * copy_dst1;
106 u32 * copy_src_last0, * copy_dst_last0;
107 u32 * copy_src_last1, * copy_dst_last1;
108 vxlan_tunnel_t * t0, * t1;
109 u16 new_l0, new_l1;
110 ip_csum_t sum0, sum1;
111
112 /* Prefetch next iteration. */
113 {
114 vlib_buffer_t * p2, * p3;
115
116 p2 = vlib_get_buffer (vm, from[2]);
117 p3 = vlib_get_buffer (vm, from[3]);
118
119 vlib_prefetch_buffer_header (p2, LOAD);
120 vlib_prefetch_buffer_header (p3, LOAD);
121
122 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
123 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
124 }
125
126 bi0 = from[0];
127 bi1 = from[1];
128 to_next[0] = bi0;
129 to_next[1] = bi1;
130 from += 2;
131 to_next += 2;
132 n_left_to_next -= 2;
133 n_left_from -= 2;
134
135 b0 = vlib_get_buffer (vm, bi0);
136 b1 = vlib_get_buffer (vm, bi1);
137
138 flow_hash0 = vnet_l2_compute_flow_hash (b0);
139 flow_hash1 = vnet_l2_compute_flow_hash (b1);
140
141 /* 1-wide cache? */
142 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
143 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_TX];
144 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
145 hi1 = vnet_get_sup_hw_interface (vnm, sw_if_index1);
146
147 t0 = &vxm->tunnels[hi0->dev_instance];
148 t1 = &vxm->tunnels[hi1->dev_instance];
149
150 /* Check rewrite string and drop packet if tunnel is deleted */
151 if (PREDICT_FALSE(t0->rewrite == vxlan_dummy_rewrite))
152 {
153 next0 = VXLAN_ENCAP_NEXT_DROP;
154 b0->error = node->errors[VXLAN_ENCAP_ERROR_DEL_TUNNEL];
155 pkts_encapsulated --;
156 } /* Still go through normal encap with dummy rewrite */
157 if (PREDICT_FALSE(t1->rewrite == vxlan_dummy_rewrite))
158 {
159 next1 = VXLAN_ENCAP_NEXT_DROP;
160 b1->error = node->errors[VXLAN_ENCAP_ERROR_DEL_TUNNEL];
161 pkts_encapsulated --;
162 } /* Still go through normal encap with dummy rewrite */
163
164 /* IP4 VXLAN header sizeof(ip4_vxlan_header_t) should be 36 octects */
165 ASSERT(vec_len(t0->rewrite) == 36);
166 ASSERT(vec_len(t1->rewrite) == 36);
167
168 /* Apply the rewrite string. $$$$ vnet_rewrite? */
169 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
170 vlib_buffer_advance (b1, -(word)_vec_len(t1->rewrite));
171
172 ip0 = vlib_buffer_get_current(b0);
173 ip1 = vlib_buffer_get_current(b1);
174 /* Copy the fixed header */
175 copy_dst0 = (u64 *) ip0;
176 copy_src0 = (u64 *) t0->rewrite;
177 copy_dst1 = (u64 *) ip1;
178 copy_src1 = (u64 *) t1->rewrite;
179
180 /* Copy first 32 octets 8-bytes at a time */
181#define _(offs) copy_dst0[offs] = copy_src0[offs];
182 foreach_fixed_header_offset;
183#undef _
184#define _(offs) copy_dst1[offs] = copy_src1[offs];
185 foreach_fixed_header_offset;
186#undef _
187
188 /* Last 4 octets. Hopefully gcc will be our friend */
189 copy_dst_last0 = (u32 *)(&copy_dst0[4]);
190 copy_src_last0 = (u32 *)(&copy_src0[4]);
191 copy_dst_last1 = (u32 *)(&copy_dst1[4]);
192 copy_src_last1 = (u32 *)(&copy_src1[4]);
193
194 copy_dst_last0[0] = copy_src_last0[0];
195 copy_dst_last1[0] = copy_src_last1[0];
196
197 /* fix the <bleep>ing outer-IP checksum */
198 sum0 = ip0->checksum;
199 /* old_l0 always 0, see the rewrite setup */
200 new_l0 =
201 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
202
203 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
204 length /* changed member */);
205 ip0->checksum = ip_csum_fold (sum0);
206 ip0->length = new_l0;
207
208 sum1 = ip1->checksum;
209 /* old_l1 always 0, see the rewrite setup */
210 new_l1 =
211 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
212
213 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
214 length /* changed member */);
215 ip1->checksum = ip_csum_fold (sum1);
216 ip1->length = new_l1;
217
218 /* Fix UDP length */
219 udp0 = (udp_header_t *)(ip0+1);
220 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
221 - sizeof (*ip0));
222 udp1 = (udp_header_t *)(ip1+1);
223 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
224 - sizeof (*ip1));
225
226 udp0->length = new_l0;
227 udp0->src_port = flow_hash0;
228
229 udp1->length = new_l1;
230 udp1->src_port = flow_hash1;
231
232 /* Reset to look up tunnel partner in the configured FIB */
233 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
234 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
John Lo2d343742016-01-19 17:27:17 -0500235 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
236 vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 pkts_encapsulated += 2;
238
239 len0 = vlib_buffer_length_in_chain (vm, b0);
240 len1 = vlib_buffer_length_in_chain (vm, b0);
241 stats_n_packets += 2;
242 stats_n_bytes += len0 + len1;
243
244 /* Batch stats increment on the same vxlan tunnel so counter is not
245 incremented per packet. Note stats are still incremented for deleted
246 and admin-down tunnel where packets are dropped. It is not worthwhile
247 to check for this rare case and affect normal path performance. */
248 if (PREDICT_FALSE ((sw_if_index0 != stats_sw_if_index) ||
Hongjun Nib2cdd2f2016-04-06 16:20:22 -0700249 (sw_if_index1 != stats_sw_if_index)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250 {
251 stats_n_packets -= 2;
252 stats_n_bytes -= len0 + len1;
253 if (sw_if_index0 == sw_if_index1)
254 {
255 if (stats_n_packets)
256 vlib_increment_combined_counter
257 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
258 cpu_index, stats_sw_if_index,
259 stats_n_packets, stats_n_bytes);
260 stats_sw_if_index = sw_if_index0;
261 stats_n_packets = 2;
262 stats_n_bytes = len0 + len1;
263 }
264 else
265 {
266 vlib_increment_combined_counter
267 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
268 cpu_index, sw_if_index0, 1, len0);
269 vlib_increment_combined_counter
270 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
271 cpu_index, sw_if_index1, 1, len1);
272 }
273 }
274
275 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
276 {
277 vxlan_encap_trace_t *tr =
278 vlib_add_trace (vm, node, b0, sizeof (*tr));
279 tr->tunnel_index = t0 - vxm->tunnels;
280 tr->vni = t0->vni;
281 }
282
283 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
284 {
285 vxlan_encap_trace_t *tr =
286 vlib_add_trace (vm, node, b1, sizeof (*tr));
287 tr->tunnel_index = t1 - vxm->tunnels;
288 tr->vni = t1->vni;
289 }
290
291 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
292 to_next, n_left_to_next,
293 bi0, bi1, next0, next1);
294 }
295
296 while (n_left_from > 0 && n_left_to_next > 0)
297 {
298 u32 bi0;
299 vlib_buffer_t * b0;
300 u32 flow_hash0;
301 u32 next0 = VXLAN_ENCAP_NEXT_IP4_LOOKUP;
302 u32 sw_if_index0, len0;
303 vnet_hw_interface_t * hi0;
304 ip4_header_t * ip0;
305 udp_header_t * udp0;
306 u64 * copy_src0, * copy_dst0;
307 u32 * copy_src_last0, * copy_dst_last0;
308 vxlan_tunnel_t * t0;
309 u16 new_l0;
310 ip_csum_t sum0;
311
312 bi0 = from[0];
313 to_next[0] = bi0;
314 from += 1;
315 to_next += 1;
316 n_left_from -= 1;
317 n_left_to_next -= 1;
318
319 b0 = vlib_get_buffer (vm, bi0);
320
321 flow_hash0 = vnet_l2_compute_flow_hash(b0);
322
323 /* 1-wide cache? */
324 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
325 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
326
327 t0 = &vxm->tunnels[hi0->dev_instance];
328
329 /* Check rewrite string and drop packet if tunnel is deleted */
330 if (PREDICT_FALSE(t0->rewrite == vxlan_dummy_rewrite))
331 {
332 next0 = VXLAN_ENCAP_NEXT_DROP;
333 b0->error = node->errors[VXLAN_ENCAP_ERROR_DEL_TUNNEL];
334 pkts_encapsulated --;
335 } /* Still go through normal encap with dummy rewrite */
336
337 /* IP4 VXLAN header sizeof(ip4_vxlan_header_t) should be 36 octects */
338 ASSERT(vec_len(t0->rewrite) == 36);
339
340 /* Apply the rewrite string. $$$$ vnet_rewrite? */
341 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
342
343 ip0 = vlib_buffer_get_current(b0);
344 /* Copy the fixed header */
345 copy_dst0 = (u64 *) ip0;
346 copy_src0 = (u64 *) t0->rewrite;
347
348 /* Copy first 32 octets 8-bytes at a time */
349#define _(offs) copy_dst0[offs] = copy_src0[offs];
350 foreach_fixed_header_offset;
351#undef _
352 /* Last 4 octets. Hopefully gcc will be our friend */
353 copy_dst_last0 = (u32 *)(&copy_dst0[4]);
354 copy_src_last0 = (u32 *)(&copy_src0[4]);
355
356 copy_dst_last0[0] = copy_src_last0[0];
357
358 /* fix the <bleep>ing outer-IP checksum */
359 sum0 = ip0->checksum;
360 /* old_l0 always 0, see the rewrite setup */
361 new_l0 =
362 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
363
364 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
365 length /* changed member */);
366 ip0->checksum = ip_csum_fold (sum0);
367 ip0->length = new_l0;
368
369 /* Fix UDP length */
370 udp0 = (udp_header_t *)(ip0+1);
371 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
372 - sizeof (*ip0));
373
374 udp0->length = new_l0;
375 udp0->src_port = flow_hash0;
376
377 /* Reset to look up tunnel partner in the configured FIB */
378 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
John Lo2d343742016-01-19 17:27:17 -0500379 vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 pkts_encapsulated ++;
381
382 len0 = vlib_buffer_length_in_chain (vm, b0);
383 stats_n_packets += 1;
384 stats_n_bytes += len0;
385
386 /* Batch stats increment on the same vxlan tunnel so counter is not
387 incremented per packet. Note stats are still incremented for deleted
388 and admin-down tunnel where packets are dropped. It is not worthwhile
389 to check for this rare case and affect normal path performance. */
390 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
391 {
392 stats_n_packets -= 1;
393 stats_n_bytes -= len0;
394 if (stats_n_packets)
395 vlib_increment_combined_counter
396 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
397 cpu_index, stats_sw_if_index,
398 stats_n_packets, stats_n_bytes);
399 stats_n_packets = 1;
400 stats_n_bytes = len0;
401 stats_sw_if_index = sw_if_index0;
402 }
403
404 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
405 {
406 vxlan_encap_trace_t *tr =
407 vlib_add_trace (vm, node, b0, sizeof (*tr));
408 tr->tunnel_index = t0 - vxm->tunnels;
409 tr->vni = t0->vni;
410 }
411 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
412 to_next, n_left_to_next,
413 bi0, next0);
414 }
415
416 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
417 }
418
419 /* Do we still need this now that tunnel tx stats is kept? */
420 vlib_node_increment_counter (vm, node->node_index,
421 VXLAN_ENCAP_ERROR_ENCAPSULATED,
422 pkts_encapsulated);
423
424 /* Increment any remaining batch stats */
425 if (stats_n_packets)
426 {
427 vlib_increment_combined_counter
428 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
429 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
430 node->runtime_data[0] = stats_sw_if_index;
431 }
432
433 return from_frame->n_vectors;
434}
435
436VLIB_REGISTER_NODE (vxlan_encap_node) = {
437 .function = vxlan_encap,
438 .name = "vxlan-encap",
439 .vector_size = sizeof (u32),
440 .format_trace = format_vxlan_encap_trace,
441 .type = VLIB_NODE_TYPE_INTERNAL,
442
443 .n_errors = ARRAY_LEN(vxlan_encap_error_strings),
444 .error_strings = vxlan_encap_error_strings,
445
446 .n_next_nodes = VXLAN_ENCAP_N_NEXT,
447
448 .next_nodes = {
449 [VXLAN_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
450 [VXLAN_ENCAP_NEXT_DROP] = "error-drop",
451 },
452};