Initial commit of vpp code.
[vpp.git] / vnet / vnet / nsh-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/nsh-vxlan-gpe/nsh_vxlan_gpe.h>
21
22 /* Statistics (not really errors) */
23 #define foreach_nsh_vxlan_gpe_encap_error    \
24 _(ENCAPSULATED, "good packets encapsulated")
25
26 static char * nsh_vxlan_gpe_encap_error_strings[] = {
27 #define _(sym,string) string,
28   foreach_nsh_vxlan_gpe_encap_error
29 #undef _
30 };
31
32 typedef enum {
33 #define _(sym,str) NSH_VXLAN_GPE_ENCAP_ERROR_##sym,
34     foreach_nsh_vxlan_gpe_encap_error
35 #undef _
36     NSH_VXLAN_GPE_ENCAP_N_ERROR,
37 } nsh_vxlan_gpe_encap_error_t;
38
39 typedef enum {
40     NSH_VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP,
41     NSH_VXLAN_GPE_ENCAP_NEXT_DROP,
42     NSH_VXLAN_GPE_ENCAP_N_NEXT,
43 } nsh_vxlan_gpe_encap_next_t;
44
45 typedef struct {
46   u32 tunnel_index;
47 } nsh_vxlan_gpe_encap_trace_t;
48
49 u8 * format_nsh_vxlan_gpe_encap_trace (u8 * s, va_list * args)
50 {
51   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
52   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
53   nsh_vxlan_gpe_encap_trace_t * t 
54       = va_arg (*args, nsh_vxlan_gpe_encap_trace_t *);
55
56   s = format (s, "NSH-VXLAN-ENCAP: tunnel %d", t->tunnel_index);
57   return s;
58 }
59
60 #define foreach_fixed_header_offset             \
61 _(0) _(1) _(2) _(3) _(4) _(5) _(6) 
62
63 static uword
64 nsh_vxlan_gpe_encap (vlib_main_t * vm,
65                vlib_node_runtime_t * node,
66                vlib_frame_t * from_frame)
67 {
68   u32 n_left_from, next_index, * from, * to_next;
69   nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
70   vnet_main_t * vnm = ngm->vnet_main;
71   u32 pkts_encapsulated = 0;
72   u16 old_l0 = 0, old_l1 = 0;
73
74   from = vlib_frame_vector_args (from_frame);
75   n_left_from = from_frame->n_vectors;
76
77   next_index = node->cached_next_index;
78
79   while (n_left_from > 0)
80     {
81       u32 n_left_to_next;
82
83       vlib_get_next_frame (vm, node, next_index,
84                            to_next, n_left_to_next);
85
86       while (n_left_from >= 4 && n_left_to_next >= 2)
87         {
88           u32 bi0, bi1;
89           vlib_buffer_t * b0, * b1;
90           u32 next0 = NSH_VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
91           u32 next1 = NSH_VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
92           vnet_hw_interface_t * hi0, * hi1;
93           ip4_header_t * ip0, * ip1;
94           udp_header_t * udp0, * udp1;
95           u64 * copy_src0, * copy_dst0;
96           u64 * copy_src1, * copy_dst1;
97           u32 * copy_src_last0, * copy_dst_last0;
98           u32 * copy_src_last1, * copy_dst_last1;
99           nsh_vxlan_gpe_tunnel_t * t0, * t1;
100           u16 new_l0, new_l1;
101           ip_csum_t sum0, sum1;
102
103           /* Prefetch next iteration. */
104           {
105             vlib_buffer_t * p2, * p3;
106
107             p2 = vlib_get_buffer (vm, from[2]);
108             p3 = vlib_get_buffer (vm, from[3]);
109
110             vlib_prefetch_buffer_header (p2, LOAD);
111             vlib_prefetch_buffer_header (p3, LOAD);
112
113             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
114             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
115           }
116
117           bi0 = from[0];
118           bi1 = from[1];
119           to_next[0] = bi0;
120           to_next[1] = bi1;
121           from += 2;
122           to_next += 2;
123           n_left_to_next -= 2;
124           n_left_from -= 2;
125
126           b0 = vlib_get_buffer (vm, bi0);
127           b1 = vlib_get_buffer (vm, bi1);
128
129           /* 1-wide cache? */
130           hi0 = vnet_get_sup_hw_interface 
131             (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
132           hi1 = vnet_get_sup_hw_interface 
133             (vnm, vnet_buffer(b1)->sw_if_index[VLIB_TX]);
134
135           t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
136           t1 = pool_elt_at_index (ngm->tunnels, hi1->dev_instance);
137
138           ASSERT(vec_len(t0->rewrite) >= 24);
139           ASSERT(vec_len(t1->rewrite) >= 24);
140
141           /* Apply the rewrite string. $$$$ vnet_rewrite? */
142           vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
143           vlib_buffer_advance (b1, -(word)_vec_len(t1->rewrite));
144
145           ip0 = vlib_buffer_get_current(b0);
146           ip1 = vlib_buffer_get_current(b1);
147           /* Copy the fixed header */
148           copy_dst0 = (u64 *) ip0;
149           copy_src0 = (u64 *) t0->rewrite;
150           copy_dst1 = (u64 *) ip1;
151           copy_src1 = (u64 *) t1->rewrite;
152
153           ASSERT (sizeof (ip4_vxlan_gpe_and_nsh_header_t) == 60);
154
155           /* Copy first 56 octets 8-bytes at a time */
156 #define _(offs) copy_dst0[offs] = copy_src0[offs];
157           foreach_fixed_header_offset;
158 #undef _
159 #define _(offs) copy_dst1[offs] = copy_src1[offs];
160           foreach_fixed_header_offset;
161 #undef _
162
163           /* Last 4 octets. Hopefully gcc will be our friend */
164           copy_dst_last0 = (u32 *)(&copy_dst0[7]);
165           copy_src_last0 = (u32 *)(&copy_src0[7]);
166           copy_dst_last1 = (u32 *)(&copy_dst1[7]);
167           copy_src_last1 = (u32 *)(&copy_src1[7]);
168           
169           copy_dst_last0[0] = copy_src_last0[0];
170           copy_dst_last1[0] = copy_src_last1[0];
171
172           /* If there are TLVs to copy, do so */
173           if (PREDICT_FALSE (_vec_len(t0->rewrite) > 64))
174             memcpy (&copy_dst0[3], t0->rewrite + 64 , 
175                     _vec_len (t0->rewrite)-64);
176
177           if (PREDICT_FALSE (_vec_len(t1->rewrite) > 64))
178             memcpy (&copy_dst0[3], t1->rewrite + 64 , 
179                     _vec_len (t1->rewrite)-64);
180
181           /* fix the <bleep>ing outer-IP checksum */
182           sum0 = ip0->checksum;
183           /* old_l0 always 0, see the rewrite setup */
184           new_l0 = 
185             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
186           
187           sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
188                                  length /* changed member */);
189           ip0->checksum = ip_csum_fold (sum0);
190           ip0->length = new_l0;
191
192           sum1 = ip1->checksum;
193           /* old_l1 always 0, see the rewrite setup */
194           new_l1 = 
195             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
196           
197           sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
198                                  length /* changed member */);
199           ip1->checksum = ip_csum_fold (sum1);
200           ip1->length = new_l1;
201           
202           /* Fix UDP length */
203           udp0 = (udp_header_t *)(ip0+1);
204           new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
205                                          - sizeof (*ip0));
206           udp1 = (udp_header_t *)(ip1+1);
207           new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
208                                          - sizeof (*ip1));
209           
210           udp0->length = new_l0;
211           udp1->length = new_l1;
212
213           /* Reset to look up tunnel partner in the configured FIB */
214           vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
215           vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
216
217           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
218             {
219               nsh_vxlan_gpe_encap_trace_t *tr = 
220                 vlib_add_trace (vm, node, b0, sizeof (*tr));
221               tr->tunnel_index = t0 - ngm->tunnels;
222             }
223
224           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) 
225             {
226               nsh_vxlan_gpe_encap_trace_t *tr = 
227                 vlib_add_trace (vm, node, b1, sizeof (*tr));
228               tr->tunnel_index = t1 - ngm->tunnels;
229             }
230
231           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
232                                            to_next, n_left_to_next,
233                                            bi0, bi1, next0, next1);
234         }
235
236       while (n_left_from > 0 && n_left_to_next > 0)
237         {
238           u32 bi0;
239           vlib_buffer_t * b0;
240           u32 next0 = NSH_VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
241           vnet_hw_interface_t * hi0;
242           ip4_header_t * ip0;
243           udp_header_t * udp0;
244           u64 * copy_src0, * copy_dst0;
245           u32 * copy_src_last0, * copy_dst_last0;
246           nsh_vxlan_gpe_tunnel_t * t0;
247           u16 new_l0;
248           ip_csum_t sum0;
249
250           bi0 = from[0];
251           to_next[0] = bi0;
252           from += 1;
253           to_next += 1;
254           n_left_from -= 1;
255           n_left_to_next -= 1;
256
257           b0 = vlib_get_buffer (vm, bi0);
258
259           /* 1-wide cache? */
260           hi0 = vnet_get_sup_hw_interface 
261             (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
262
263           t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
264
265           ASSERT(vec_len(t0->rewrite) >= 24);
266
267           /* Apply the rewrite string. $$$$ vnet_rewrite? */
268           vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
269
270           ip0 = vlib_buffer_get_current(b0);
271           /* Copy the fixed header */
272           copy_dst0 = (u64 *) ip0;
273           copy_src0 = (u64 *) t0->rewrite;
274
275           ASSERT (sizeof (ip4_vxlan_gpe_and_nsh_header_t) == 60);
276
277           /* Copy first 56 octets 8-bytes at a time */
278 #define _(offs) copy_dst0[offs] = copy_src0[offs];
279           foreach_fixed_header_offset;
280 #undef _
281           /* Last 4 octets. Hopefully gcc will be our friend */
282           copy_dst_last0 = (u32 *)(&copy_dst0[7]);
283           copy_src_last0 = (u32 *)(&copy_src0[7]);
284           
285           copy_dst_last0[0] = copy_src_last0[0];
286
287           /* If there are TLVs to copy, do so */
288           if (PREDICT_FALSE (_vec_len(t0->rewrite) > 64))
289             memcpy (&copy_dst0[3], t0->rewrite + 64 , 
290                     _vec_len (t0->rewrite)-64);
291
292           /* fix the <bleep>ing outer-IP checksum */
293           sum0 = ip0->checksum;
294           /* old_l0 always 0, see the rewrite setup */
295           new_l0 = 
296             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
297           
298           sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
299                                  length /* changed member */);
300           ip0->checksum = ip_csum_fold (sum0);
301           ip0->length = new_l0;
302           
303           /* Fix UDP length */
304           udp0 = (udp_header_t *)(ip0+1);
305           new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
306                                          - sizeof (*ip0));
307           
308           udp0->length = new_l0;
309
310           /* Reset to look up tunnel partner in the configured FIB */
311           vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
312           pkts_encapsulated ++;
313
314           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
315             {
316               nsh_vxlan_gpe_encap_trace_t *tr = 
317                 vlib_add_trace (vm, node, b0, sizeof (*tr));
318               tr->tunnel_index = t0 - ngm->tunnels;
319             }
320           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
321                                            to_next, n_left_to_next,
322                                            bi0, next0);
323         }
324
325       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
326     }
327   vlib_node_increment_counter (vm, node->node_index, 
328                                NSH_VXLAN_GPE_ENCAP_ERROR_ENCAPSULATED, 
329                                pkts_encapsulated);
330   return from_frame->n_vectors;
331 }
332
333 VLIB_REGISTER_NODE (nsh_vxlan_gpe_encap_node) = {
334   .function = nsh_vxlan_gpe_encap,
335   .name = "nsh-vxlan-gpe-encap",
336   .vector_size = sizeof (u32),
337   .format_trace = format_nsh_vxlan_gpe_encap_trace,
338   .type = VLIB_NODE_TYPE_INTERNAL,
339
340   .n_errors = ARRAY_LEN(nsh_vxlan_gpe_encap_error_strings),
341   .error_strings = nsh_vxlan_gpe_encap_error_strings,
342
343   .n_next_nodes = NSH_VXLAN_GPE_ENCAP_N_NEXT,
344
345   .next_nodes = {
346         [NSH_VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
347         [NSH_VXLAN_GPE_ENCAP_NEXT_DROP] = "error-drop",
348   },
349 };