Add Vxlan-Gpe over IPv6
[vpp.git] / vnet / vnet / vxlan-gpe / 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-gpe/vxlan_gpe.h>
21
22 /* Statistics (not really errors) */
23 #define foreach_vxlan_gpe_encap_error    \
24 _(ENCAPSULATED, "good packets encapsulated")
25
26 static char * vxlan_gpe_encap_error_strings[] = {
27 #define _(sym,string) string,
28   foreach_vxlan_gpe_encap_error
29 #undef _
30 };
31
32 typedef enum {
33 #define _(sym,str) VXLAN_GPE_ENCAP_ERROR_##sym,
34     foreach_vxlan_gpe_encap_error
35 #undef _
36     VXLAN_GPE_ENCAP_N_ERROR,
37 } vxlan_gpe_encap_error_t;
38
39 typedef enum {
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;
45
46 typedef struct {
47   u32 tunnel_index;
48 } vxlan_gpe_encap_trace_t;
49
50
51 u8 * format_vxlan_gpe_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_gpe_encap_trace_t * t 
56       = va_arg (*args, vxlan_gpe_encap_trace_t *);
57
58   s = format (s, "VXLAN-GPE-ENCAP: tunnel %d", t->tunnel_index);
59   return s;
60 }
61
62 always_inline void
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)
65 {
66   ASSERT(sizeof(ip4_vxlan_gpe_header_t) == 36);
67   ASSERT(sizeof(ip6_vxlan_gpe_header_t) == 56);
68
69   if (is_v4)
70     {
71       ip_udp_encap_one (ngm->vlib_main, b0, t0->rewrite, 36, 1);
72       next0[0] = VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
73
74     }
75   else
76     {
77       ip_udp_encap_one (ngm->vlib_main, b0, t0->rewrite, 56, 0);
78       next0[0] = VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP;
79     }
80 }
81
82 always_inline void
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)
86 {
87   ASSERT(sizeof(ip4_vxlan_gpe_header_t) == 36);
88   ASSERT(sizeof(ip6_vxlan_gpe_header_t) == 56);
89
90   if (is_v4)
91     {
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;
95     }
96   else
97     {
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;
101     }
102 }
103
104 static uword
105 vxlan_gpe_encap (vlib_main_t * vm,
106                vlib_node_runtime_t * node,
107                vlib_frame_t * from_frame)
108 {
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;
116
117   from = vlib_frame_vector_args (from_frame);
118   n_left_from = from_frame->n_vectors;
119
120   next_index = node->cached_next_index;
121   stats_sw_if_index = node->runtime_data[0];
122   stats_n_packets = stats_n_bytes = 0;
123
124   while (n_left_from > 0)
125   {
126     u32 n_left_to_next;
127
128     vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
129
130     while (n_left_from >= 4 && n_left_to_next >= 2)
131     {
132       u32 bi0, bi1;
133       vlib_buffer_t * b0, *b1;
134       u32 next0, next1;
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;
139
140       next0 = next1 = VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
141
142       /* Prefetch next iteration. */
143       {
144         vlib_buffer_t * p2, *p3;
145
146         p2 = vlib_get_buffer (vm, from[2]);
147         p3 = vlib_get_buffer (vm, from[3]);
148
149         vlib_prefetch_buffer_header(p2, LOAD);
150         vlib_prefetch_buffer_header(p3, LOAD);
151
152         CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
153         CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
154       }
155
156       bi0 = from[0];
157       bi1 = from[1];
158       to_next[0] = bi0;
159       to_next[1] = bi1;
160       from += 2;
161       to_next += 2;
162       n_left_to_next -= 2;
163       n_left_from -= 2;
164
165       b0 = vlib_get_buffer (vm, bi0);
166       b1 = vlib_get_buffer (vm, bi1);
167
168       /* 1-wide cache? */
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]);
173
174       t0 = pool_elt_at_index(ngm->tunnels, hi0->dev_instance);
175       t1 = pool_elt_at_index(ngm->tunnels, hi1->dev_instance);
176
177       is_ip4_0 = (t0->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
178       is_ip4_1 = (t1->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
179
180       if (PREDICT_TRUE(is_ip4_0 == is_ip4_1))
181       {
182         vxlan_gpe_encap_two_inline (ngm, b0, b1, t0, t1, &next0, &next1,is_ip4_0);
183       }
184       else
185       {
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);
188       }
189
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;
196
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;
201
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)))
208       {
209         stats_n_packets -= 2;
210         stats_n_bytes -= len0 + len1;
211         if (sw_if_index0 == sw_if_index1)
212         {
213           if (stats_n_packets)
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;
218           stats_n_packets = 2;
219           stats_n_bytes = len0 + len1;
220         }
221         else
222         {
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);
229         }
230       }
231
232       if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
233       {
234         vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof(*tr));
235         tr->tunnel_index = t0 - ngm->tunnels;
236       }
237
238       if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
239       {
240         vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b1,
241                                                       sizeof(*tr));
242         tr->tunnel_index = t1 - ngm->tunnels;
243       }
244
245       vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
246                                       n_left_to_next, bi0, bi1, next0, next1);
247     }
248
249     while (n_left_from > 0 && n_left_to_next > 0)
250     {
251       u32 bi0;
252       vlib_buffer_t * b0;
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;
257       u8 is_ip4_0;
258
259       bi0 = from[0];
260       to_next[0] = bi0;
261       from += 1;
262       to_next += 1;
263       n_left_from -= 1;
264       n_left_to_next -= 1;
265
266       b0 = vlib_get_buffer (vm, bi0);
267
268       /* 1-wide cache? */
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]);
271
272       t0 = pool_elt_at_index(ngm->tunnels, hi0->dev_instance);
273
274       is_ip4_0 = (t0->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
275
276       vxlan_gpe_encap_one_inline (ngm, b0, t0, &next0, is_ip4_0);
277
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;
281       pkts_encapsulated++;
282
283       len0 = vlib_buffer_length_in_chain (vm, b0);
284       stats_n_packets += 1;
285       stats_n_bytes += len0;
286
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))
292       {
293         stats_n_packets -= 1;
294         stats_n_bytes -= len0;
295         if (stats_n_packets)
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);
299         stats_n_packets = 1;
300         stats_n_bytes = len0;
301         stats_sw_if_index = sw_if_index0;
302       }
303       if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
304       {
305         vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b0,
306                                                       sizeof(*tr));
307         tr->tunnel_index = t0 - ngm->tunnels;
308       }
309       vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
310                                       n_left_to_next, bi0, next0);
311     }
312
313     vlib_put_next_frame (vm, node, next_index, n_left_to_next);
314   }
315   vlib_node_increment_counter (vm, node->node_index,
316                                VXLAN_GPE_ENCAP_ERROR_ENCAPSULATED,
317                                pkts_encapsulated);
318   /* Increment any remaining batch stats */
319   if (stats_n_packets)
320   {
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;
325   }
326
327   return from_frame->n_vectors;
328 }
329
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,
336
337   .n_errors = ARRAY_LEN(vxlan_gpe_encap_error_strings),
338   .error_strings = vxlan_gpe_encap_error_strings,
339
340   .n_next_nodes = VXLAN_GPE_ENCAP_N_NEXT,
341
342   .next_nodes = {
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",
346   },
347 };
348