Initial commit of vpp code.
[vpp.git] / vnet / vnet / vxlan / encap.c
1 /*
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
27 static char * vxlan_encap_error_strings[] = {
28 #define _(sym,string) string,
29   foreach_vxlan_encap_error
30 #undef _
31 };
32
33 typedef 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
40 typedef enum {
41     VXLAN_ENCAP_NEXT_IP4_LOOKUP,
42     VXLAN_ENCAP_NEXT_DROP,
43     VXLAN_ENCAP_N_NEXT,
44 } vxlan_encap_next_t;
45
46 typedef struct {
47   u32 tunnel_index;
48   u32 vni;
49 } vxlan_encap_trace_t;
50
51 u8 * 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
65 static uword
66 vxlan_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;
235           pkts_encapsulated += 2;
236
237           len0 = vlib_buffer_length_in_chain (vm, b0);
238           len1 = vlib_buffer_length_in_chain (vm, b0);
239           stats_n_packets += 2;
240           stats_n_bytes += len0 + len1;
241
242           /* Batch stats increment on the same vxlan tunnel so counter is not
243              incremented per packet. Note stats are still incremented for deleted
244              and admin-down tunnel where packets are dropped. It is not worthwhile
245              to check for this rare case and affect normal path performance. */
246           if (PREDICT_FALSE ((sw_if_index0 != stats_sw_if_index) ||
247                              (sw_if_index0 != stats_sw_if_index))) 
248             {
249               stats_n_packets -= 2;
250               stats_n_bytes -= len0 + len1;
251               if (sw_if_index0 == sw_if_index1) 
252                 {
253                   if (stats_n_packets) 
254                     vlib_increment_combined_counter 
255                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
256                        cpu_index, stats_sw_if_index, 
257                        stats_n_packets, stats_n_bytes);
258                   stats_sw_if_index = sw_if_index0;
259                   stats_n_packets = 2;
260                   stats_n_bytes = len0 + len1;
261                 }
262               else 
263                 {
264                   vlib_increment_combined_counter 
265                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
266                        cpu_index, sw_if_index0, 1, len0);
267                   vlib_increment_combined_counter 
268                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
269                        cpu_index, sw_if_index1, 1, len1);
270                 }
271             }
272
273           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
274             {
275               vxlan_encap_trace_t *tr = 
276                 vlib_add_trace (vm, node, b0, sizeof (*tr));
277               tr->tunnel_index = t0 - vxm->tunnels;
278               tr->vni = t0->vni;
279            }
280
281           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) 
282             {
283               vxlan_encap_trace_t *tr = 
284                 vlib_add_trace (vm, node, b1, sizeof (*tr));
285               tr->tunnel_index = t1 - vxm->tunnels;
286               tr->vni = t1->vni;
287             }
288
289           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
290                                            to_next, n_left_to_next,
291                                            bi0, bi1, next0, next1);
292         }
293
294       while (n_left_from > 0 && n_left_to_next > 0)
295         {
296           u32 bi0;
297           vlib_buffer_t * b0;
298           u32 flow_hash0;
299           u32 next0 = VXLAN_ENCAP_NEXT_IP4_LOOKUP;
300           u32 sw_if_index0, len0;
301           vnet_hw_interface_t * hi0;
302           ip4_header_t * ip0;
303           udp_header_t * udp0;
304           u64 * copy_src0, * copy_dst0;
305           u32 * copy_src_last0, * copy_dst_last0;
306           vxlan_tunnel_t * t0;
307           u16 new_l0;
308           ip_csum_t sum0;
309
310           bi0 = from[0];
311           to_next[0] = bi0;
312           from += 1;
313           to_next += 1;
314           n_left_from -= 1;
315           n_left_to_next -= 1;
316
317           b0 = vlib_get_buffer (vm, bi0);
318
319           flow_hash0 = vnet_l2_compute_flow_hash(b0);
320
321           /* 1-wide cache? */
322           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
323           hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
324
325           t0 = &vxm->tunnels[hi0->dev_instance];
326
327           /* Check rewrite string and drop packet if tunnel is deleted */
328           if (PREDICT_FALSE(t0->rewrite == vxlan_dummy_rewrite))
329             {
330               next0 = VXLAN_ENCAP_NEXT_DROP;
331               b0->error = node->errors[VXLAN_ENCAP_ERROR_DEL_TUNNEL];
332               pkts_encapsulated --;
333             }  /* Still go through normal encap with dummy rewrite */
334
335           /* IP4 VXLAN header sizeof(ip4_vxlan_header_t) should be 36 octects */
336           ASSERT(vec_len(t0->rewrite) == 36);
337
338           /* Apply the rewrite string. $$$$ vnet_rewrite? */
339           vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
340
341           ip0 = vlib_buffer_get_current(b0);
342           /* Copy the fixed header */
343           copy_dst0 = (u64 *) ip0;
344           copy_src0 = (u64 *) t0->rewrite;
345
346           /* Copy first 32 octets 8-bytes at a time */
347 #define _(offs) copy_dst0[offs] = copy_src0[offs];
348           foreach_fixed_header_offset;
349 #undef _
350           /* Last 4 octets. Hopefully gcc will be our friend */
351           copy_dst_last0 = (u32 *)(&copy_dst0[4]);
352           copy_src_last0 = (u32 *)(&copy_src0[4]);
353           
354           copy_dst_last0[0] = copy_src_last0[0];
355
356           /* fix the <bleep>ing outer-IP checksum */
357           sum0 = ip0->checksum;
358           /* old_l0 always 0, see the rewrite setup */
359           new_l0 = 
360             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
361           
362           sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
363                                  length /* changed member */);
364           ip0->checksum = ip_csum_fold (sum0);
365           ip0->length = new_l0;
366           
367           /* Fix UDP length */
368           udp0 = (udp_header_t *)(ip0+1);
369           new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
370                                          - sizeof (*ip0));
371           
372           udp0->length = new_l0;
373           udp0->src_port = flow_hash0;
374
375           /* Reset to look up tunnel partner in the configured FIB */
376           vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
377           pkts_encapsulated ++;
378
379           len0 = vlib_buffer_length_in_chain (vm, b0);
380           stats_n_packets += 1;
381           stats_n_bytes += len0;
382
383           /* Batch stats increment on the same vxlan tunnel so counter is not
384              incremented per packet. Note stats are still incremented for deleted
385              and admin-down tunnel where packets are dropped. It is not worthwhile
386              to check for this rare case and affect normal path performance. */
387           if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index)) 
388             {
389               stats_n_packets -= 1;
390               stats_n_bytes -= len0;
391               if (stats_n_packets)
392                 vlib_increment_combined_counter 
393                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
394                    cpu_index, stats_sw_if_index, 
395                    stats_n_packets, stats_n_bytes);
396               stats_n_packets = 1;
397               stats_n_bytes = len0;
398               stats_sw_if_index = sw_if_index0;
399             }
400
401           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
402             {
403               vxlan_encap_trace_t *tr = 
404                 vlib_add_trace (vm, node, b0, sizeof (*tr));
405               tr->tunnel_index = t0 - vxm->tunnels;
406               tr->vni = t0->vni;
407             }
408           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
409                                            to_next, n_left_to_next,
410                                            bi0, next0);
411         }
412
413       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
414     }
415
416   /* Do we still need this now that tunnel tx stats is kept? */
417   vlib_node_increment_counter (vm, node->node_index, 
418                                VXLAN_ENCAP_ERROR_ENCAPSULATED, 
419                                pkts_encapsulated);
420
421   /* Increment any remaining batch stats */
422   if (stats_n_packets)
423     {
424       vlib_increment_combined_counter 
425         (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
426          cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
427       node->runtime_data[0] = stats_sw_if_index;
428     }
429
430   return from_frame->n_vectors;
431 }
432
433 VLIB_REGISTER_NODE (vxlan_encap_node) = {
434   .function = vxlan_encap,
435   .name = "vxlan-encap",
436   .vector_size = sizeof (u32),
437   .format_trace = format_vxlan_encap_trace,
438   .type = VLIB_NODE_TYPE_INTERNAL,
439
440   .n_errors = ARRAY_LEN(vxlan_encap_error_strings),
441   .error_strings = vxlan_encap_error_strings,
442
443   .n_next_nodes = VXLAN_ENCAP_N_NEXT,
444
445   .next_nodes = {
446         [VXLAN_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
447         [VXLAN_ENCAP_NEXT_DROP] = "error-drop",
448   },
449 };