Initial commit of vpp code.
[vpp.git] / vnet / vnet / nsh-gre / 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-gre/nsh_gre.h>
21
22 /* Statistics (not really errors) */
23 #define foreach_nsh_gre_encap_error             \
24 _(ENCAPSULATED, "good packets encapsulated")
25
26 static char * nsh_gre_encap_error_strings[] = {
27 #define _(sym,string) string,
28   foreach_nsh_gre_encap_error
29 #undef _
30 };
31
32 typedef enum {
33 #define _(sym,str) NSH_GRE_ENCAP_ERROR_##sym,
34     foreach_nsh_gre_encap_error
35 #undef _
36     NSH_GRE_ENCAP_N_ERROR,
37 } nsh_gre_encap_error_t;
38
39 typedef enum {
40     NSH_GRE_ENCAP_NEXT_IP4_LOOKUP,
41     NSH_GRE_ENCAP_NEXT_DROP,
42     NSH_GRE_ENCAP_N_NEXT,
43 } nsh_gre_encap_next_t;
44
45 typedef struct {
46   u32 tunnel_index;
47 } nsh_gre_encap_trace_t;
48
49 u8 * format_nsh_gre_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_gre_encap_trace_t * t = va_arg (*args, nsh_gre_encap_trace_t *);
54
55   s = format (s, "NSH-GRE-ENCAP: tunnel %d", t->tunnel_index);
56   return s;
57 }
58
59 static uword
60 nsh_gre_encap (vlib_main_t * vm,
61                vlib_node_runtime_t * node,
62                vlib_frame_t * from_frame)
63 {
64   u32 n_left_from, next_index, * from, * to_next;
65   nsh_gre_main_t * ngm = &nsh_gre_main;
66   vnet_main_t * vnm = ngm->vnet_main;
67   u32 pkts_encapsulated = 0;
68   u16 old_l0 = 0, old_l1 = 0;
69
70   from = vlib_frame_vector_args (from_frame);
71   n_left_from = from_frame->n_vectors;
72
73   next_index = node->cached_next_index;
74
75   while (n_left_from > 0)
76     {
77       u32 n_left_to_next;
78
79       vlib_get_next_frame (vm, node, next_index,
80                            to_next, n_left_to_next);
81
82       while (n_left_from >= 4 && n_left_to_next >= 2)
83         {
84           u32 bi0, bi1;
85           vlib_buffer_t * b0, * b1;
86           u32 next0 = NSH_GRE_ENCAP_NEXT_IP4_LOOKUP;
87           u32 next1 = NSH_GRE_ENCAP_NEXT_IP4_LOOKUP;
88           vnet_hw_interface_t * hi0, * hi1;
89           ip4_header_t * ip0, * ip1;
90           u64 * copy_src0, * copy_dst0;
91           u64 * copy_src1, * copy_dst1;
92           nsh_gre_tunnel_t * t0, * t1;
93           u16 new_l0, new_l1;
94           ip_csum_t sum0, sum1;
95
96           /* Prefetch next iteration. */
97           {
98             vlib_buffer_t * p2, * p3;
99
100             p2 = vlib_get_buffer (vm, from[2]);
101             p3 = vlib_get_buffer (vm, from[3]);
102
103             vlib_prefetch_buffer_header (p2, LOAD);
104             vlib_prefetch_buffer_header (p3, LOAD);
105
106             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
107             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
108           }
109
110           bi0 = from[0];
111           bi1 = from[1];
112           to_next[0] = bi0;
113           to_next[1] = bi1;
114           from += 2;
115           to_next += 2;
116           n_left_to_next -= 2;
117           n_left_from -= 2;
118
119           b0 = vlib_get_buffer (vm, bi0);
120           b1 = vlib_get_buffer (vm, bi1);
121
122           hi0 = vnet_get_sup_hw_interface 
123             (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
124           hi1 = vnet_get_sup_hw_interface 
125             (vnm, vnet_buffer(b1)->sw_if_index[VLIB_TX]);
126
127           t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
128           t1 = pool_elt_at_index (ngm->tunnels, hi1->dev_instance);
129
130           ASSERT(vec_len(t0->rewrite) >= 24);
131           ASSERT(vec_len(t1->rewrite) >= 24);
132
133           /* Apply the rewrite string. $$$$ vnet_rewrite? */
134           vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
135           vlib_buffer_advance (b1, -(word)_vec_len(t1->rewrite));
136
137           ip0 = vlib_buffer_get_current(b0);
138           ip1 = vlib_buffer_get_current(b1);
139           /* Copy the fixed header */
140           copy_dst0 = (u64 *) ip0;
141           copy_src0 = (u64 *) t0->rewrite;
142           copy_dst1 = (u64 *) ip1;
143           copy_src1 = (u64 *) t1->rewrite;
144
145           copy_dst0[0] = copy_src0[0];
146           copy_dst0[1] = copy_src0[1];
147           copy_dst0[2] = copy_src0[2];
148
149           copy_dst1[0] = copy_src1[0];
150           copy_dst1[1] = copy_src1[1];
151           copy_dst1[2] = copy_src1[2];
152           
153           /* If there are TLVs to copy, do so */
154           if (PREDICT_FALSE (_vec_len(t0->rewrite) > 24))
155             memcpy (&copy_dst0[3], t0->rewrite + 24 , 
156                     _vec_len (t0->rewrite)-24);
157
158           if (PREDICT_FALSE (_vec_len(t1->rewrite) > 24))
159             memcpy (&copy_dst1[3], t1->rewrite + 24 , 
160                     _vec_len (t1->rewrite)-24);
161
162           /* fix the <bleep>ing outer-IP checksums */
163           sum0 = ip0->checksum;
164           /* old_l0 always 0, see the rewrite setup */
165           new_l0 = 
166             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
167           
168           sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
169                                  length /* changed member */);
170           ip0->checksum = ip_csum_fold (sum0);
171           ip0->length = new_l0;
172
173           sum1 = ip1->checksum;
174           /* old_l1 always 1, see the rewrite setup */
175           new_l1 = 
176             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
177           
178           sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
179                                  length /* changed member */);
180           ip1->checksum = ip_csum_fold (sum1);
181           ip1->length = new_l1;
182
183           /* Reset to look up tunnel partner in the configured FIB */
184           vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
185           vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
186           pkts_encapsulated += 2;
187
188           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
189             {
190               nsh_gre_encap_trace_t *tr = 
191                 vlib_add_trace (vm, node, b0, sizeof (*tr));
192               tr->tunnel_index = t0 - ngm->tunnels;
193             }
194
195           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) 
196             {
197               nsh_gre_encap_trace_t *tr = 
198                 vlib_add_trace (vm, node, b1, sizeof (*tr));
199               tr->tunnel_index = t1 - ngm->tunnels;
200             }
201
202           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
203                                            to_next, n_left_to_next,
204                                            bi0, bi1, next0, next1);
205         }
206     
207       while (n_left_from > 0 && n_left_to_next > 0)
208         {
209           u32 bi0;
210           vlib_buffer_t * b0;
211           u32 next0 = NSH_GRE_ENCAP_NEXT_IP4_LOOKUP;
212           vnet_hw_interface_t * hi0;
213           ip4_header_t * ip0;
214           u64 * copy_src0, * copy_dst0;
215           nsh_gre_tunnel_t * t0;
216           u16 new_l0;
217           ip_csum_t sum0;
218
219           bi0 = from[0];
220           to_next[0] = bi0;
221           from += 1;
222           to_next += 1;
223           n_left_from -= 1;
224           n_left_to_next -= 1;
225
226           b0 = vlib_get_buffer (vm, bi0);
227
228           /* 1-wide cache? */
229           hi0 = vnet_get_sup_hw_interface 
230             (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
231
232           t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
233
234           ASSERT(vec_len(t0->rewrite) >= 24);
235
236           /* Apply the rewrite string. $$$$ vnet_rewrite? */
237           vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
238
239           ip0 = vlib_buffer_get_current(b0);
240           /* Copy the fixed header */
241           copy_dst0 = (u64 *) ip0;
242           copy_src0 = (u64 *) t0->rewrite;
243           copy_dst0[0] = copy_src0[0];
244           copy_dst0[1] = copy_src0[1];
245           copy_dst0[2] = copy_src0[2];
246           
247           /* If there are TLVs to copy, do so */
248           if (PREDICT_FALSE (_vec_len(t0->rewrite) > 24))
249             memcpy (&copy_dst0[3], t0->rewrite + 24 , 
250                     _vec_len (t0->rewrite)-24);
251
252           /* fix the <bleep>ing outer-IP checksum */
253           sum0 = ip0->checksum;
254           /* old_l0 always 0, see the rewrite setup */
255           new_l0 = 
256             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
257           
258           sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
259                                  length /* changed member */);
260           ip0->checksum = ip_csum_fold (sum0);
261           ip0->length = new_l0;
262
263           /* Reset to look up tunnel partner in the configured FIB */
264           vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
265           pkts_encapsulated ++;
266
267           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
268             {
269               nsh_gre_encap_trace_t *tr = 
270                 vlib_add_trace (vm, node, b0, sizeof (*tr));
271               tr->tunnel_index = t0 - ngm->tunnels;
272             }
273           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
274                                            to_next, n_left_to_next,
275                                            bi0, next0);
276         }
277
278       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
279     }
280   vlib_node_increment_counter (vm, node->node_index, 
281                                NSH_GRE_ENCAP_ERROR_ENCAPSULATED, 
282                                pkts_encapsulated);
283   return from_frame->n_vectors;
284 }
285
286 VLIB_REGISTER_NODE (nsh_gre_encap_node) = {
287   .function = nsh_gre_encap,
288   .name = "nsh-gre-encap",
289   .vector_size = sizeof (u32),
290   .format_trace = format_nsh_gre_encap_trace,
291   .type = VLIB_NODE_TYPE_INTERNAL,
292
293   .n_errors = ARRAY_LEN(nsh_gre_encap_error_strings),
294   .error_strings = nsh_gre_encap_error_strings,
295
296   .n_next_nodes = NSH_GRE_ENCAP_N_NEXT,
297
298   //  add dispositions here
299   .next_nodes = {
300         [NSH_GRE_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
301         [NSH_GRE_ENCAP_NEXT_DROP] = "error-drop",
302   },
303 };