ONE-11: Fix bugs in LISP API
[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   vnet_interface_main_t * im = &vnm->interface_main;
68   u32 pkts_encapsulated = 0;
69   u16 old_l0 = 0, old_l1 = 0;
70   u32 cpu_index = os_get_cpu_number();
71   u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
72
73   from = vlib_frame_vector_args (from_frame);
74   n_left_from = from_frame->n_vectors;
75
76   next_index = node->cached_next_index;
77   stats_sw_if_index = node->runtime_data[0];
78   stats_n_packets = stats_n_bytes = 0;
79
80   while (n_left_from > 0)
81     {
82       u32 n_left_to_next;
83
84       vlib_get_next_frame (vm, node, next_index,
85                            to_next, n_left_to_next);
86
87       while (n_left_from >= 4 && n_left_to_next >= 2)
88         {
89           u32 bi0, bi1;
90           vlib_buffer_t * b0, * b1;
91           u32 next0 = NSH_GRE_ENCAP_NEXT_IP4_LOOKUP;
92           u32 next1 = NSH_GRE_ENCAP_NEXT_IP4_LOOKUP;
93           u32 sw_if_index0, sw_if_index1, len0, len1;
94           vnet_hw_interface_t * hi0, * hi1;
95           ip4_header_t * ip0, * ip1;
96           u64 * copy_src0, * copy_dst0;
97           u64 * copy_src1, * copy_dst1;
98           nsh_gre_tunnel_t * t0, * t1;
99           u16 new_l0, new_l1;
100           ip_csum_t sum0, sum1;
101
102           /* Prefetch next iteration. */
103           {
104             vlib_buffer_t * p2, * p3;
105
106             p2 = vlib_get_buffer (vm, from[2]);
107             p3 = vlib_get_buffer (vm, from[3]);
108
109             vlib_prefetch_buffer_header (p2, LOAD);
110             vlib_prefetch_buffer_header (p3, LOAD);
111
112             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
113             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
114           }
115
116           bi0 = from[0];
117           bi1 = from[1];
118           to_next[0] = bi0;
119           to_next[1] = bi1;
120           from += 2;
121           to_next += 2;
122           n_left_to_next -= 2;
123           n_left_from -= 2;
124
125           b0 = vlib_get_buffer (vm, bi0);
126           b1 = vlib_get_buffer (vm, bi1);
127
128           /* 1-wide cache? */
129           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
130           sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_TX];
131           hi0 = vnet_get_sup_hw_interface
132             (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
133           hi1 = vnet_get_sup_hw_interface 
134             (vnm, vnet_buffer(b1)->sw_if_index[VLIB_TX]);
135
136           t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
137           t1 = pool_elt_at_index (ngm->tunnels, hi1->dev_instance);
138
139           ASSERT(vec_len(t0->rewrite) >= 24);
140           ASSERT(vec_len(t1->rewrite) >= 24);
141
142           /* Apply the rewrite string. $$$$ vnet_rewrite? */
143           vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
144           vlib_buffer_advance (b1, -(word)_vec_len(t1->rewrite));
145
146           ip0 = vlib_buffer_get_current(b0);
147           ip1 = vlib_buffer_get_current(b1);
148           /* Copy the fixed header */
149           copy_dst0 = (u64 *) ip0;
150           copy_src0 = (u64 *) t0->rewrite;
151           copy_dst1 = (u64 *) ip1;
152           copy_src1 = (u64 *) t1->rewrite;
153
154           copy_dst0[0] = copy_src0[0];
155           copy_dst0[1] = copy_src0[1];
156           copy_dst0[2] = copy_src0[2];
157
158           copy_dst1[0] = copy_src1[0];
159           copy_dst1[1] = copy_src1[1];
160           copy_dst1[2] = copy_src1[2];
161           
162           /* If there are TLVs to copy, do so */
163           if (PREDICT_FALSE (_vec_len(t0->rewrite) > 24))
164             clib_memcpy (&copy_dst0[3], t0->rewrite + 24 , 
165                     _vec_len (t0->rewrite)-24);
166
167           if (PREDICT_FALSE (_vec_len(t1->rewrite) > 24))
168             clib_memcpy (&copy_dst1[3], t1->rewrite + 24 , 
169                     _vec_len (t1->rewrite)-24);
170
171           /* fix the <bleep>ing outer-IP checksums */
172           sum0 = ip0->checksum;
173           /* old_l0 always 0, see the rewrite setup */
174           new_l0 = 
175             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
176           
177           sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
178                                  length /* changed member */);
179           ip0->checksum = ip_csum_fold (sum0);
180           ip0->length = new_l0;
181
182           sum1 = ip1->checksum;
183           /* old_l1 always 1, see the rewrite setup */
184           new_l1 = 
185             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
186           
187           sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
188                                  length /* changed member */);
189           ip1->checksum = ip_csum_fold (sum1);
190           ip1->length = new_l1;
191
192           /* Reset to look up tunnel partner in the configured FIB */
193           vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
194           vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
195           vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
196           vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
197           pkts_encapsulated += 2;
198
199           len0 = vlib_buffer_length_in_chain(vm, b0);
200           len1 = vlib_buffer_length_in_chain(vm, b0);
201           stats_n_packets += 2;
202           stats_n_bytes += len0 + len1;
203
204           /* Batch stats increment on the same vxlan tunnel so counter is not
205            incremented per packet. Note stats are still incremented for deleted
206            and admin-down tunnel where packets are dropped. It is not worthwhile
207            to check for this rare case and affect normal path performance. */
208           if (PREDICT_FALSE(
209               (sw_if_index0 != stats_sw_if_index)
210                   || (sw_if_index1 != stats_sw_if_index))) {
211             stats_n_packets -= 2;
212             stats_n_bytes -= len0 + len1;
213             if (sw_if_index0 == sw_if_index1) {
214               if (stats_n_packets)
215                 vlib_increment_combined_counter(
216                     im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
217                     cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
218               stats_sw_if_index = sw_if_index0;
219               stats_n_packets = 2;
220               stats_n_bytes = len0 + len1;
221             } else {
222               vlib_increment_combined_counter(
223                   im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
224                   cpu_index, sw_if_index0, 1, len0);
225               vlib_increment_combined_counter(
226                   im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
227                   cpu_index, sw_if_index1, 1, len1);
228             }
229           }
230
231           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
232             {
233               nsh_gre_encap_trace_t *tr = 
234                 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               nsh_gre_encap_trace_t *tr = 
241                 vlib_add_trace (vm, node, b1, sizeof (*tr));
242               tr->tunnel_index = t1 - ngm->tunnels;
243             }
244
245           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
246                                            to_next, n_left_to_next,
247                                            bi0, bi1, next0, next1);
248         }
249     
250       while (n_left_from > 0 && n_left_to_next > 0)
251         {
252           u32 bi0;
253           vlib_buffer_t * b0;
254           u32 next0 = NSH_GRE_ENCAP_NEXT_IP4_LOOKUP;
255       u32 sw_if_index0, len0;
256           vnet_hw_interface_t * hi0;
257           ip4_header_t * ip0;
258           u64 * copy_src0, * copy_dst0;
259           nsh_gre_tunnel_t * t0;
260           u16 new_l0;
261           ip_csum_t sum0;
262
263           bi0 = from[0];
264           to_next[0] = bi0;
265           from += 1;
266           to_next += 1;
267           n_left_from -= 1;
268           n_left_to_next -= 1;
269
270           b0 = vlib_get_buffer (vm, bi0);
271
272           /* 1-wide cache? */
273           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
274           hi0 = vnet_get_sup_hw_interface
275             (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
276
277           t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
278
279           ASSERT(vec_len(t0->rewrite) >= 24);
280
281           /* Apply the rewrite string. $$$$ vnet_rewrite? */
282           vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
283
284           ip0 = vlib_buffer_get_current(b0);
285           /* Copy the fixed header */
286           copy_dst0 = (u64 *) ip0;
287           copy_src0 = (u64 *) t0->rewrite;
288           copy_dst0[0] = copy_src0[0];
289           copy_dst0[1] = copy_src0[1];
290           copy_dst0[2] = copy_src0[2];
291           
292           /* If there are TLVs to copy, do so */
293           if (PREDICT_FALSE (_vec_len(t0->rewrite) > 24))
294             clib_memcpy (&copy_dst0[3], t0->rewrite + 24 , 
295                     _vec_len (t0->rewrite)-24);
296
297           /* fix the <bleep>ing outer-IP checksum */
298           sum0 = ip0->checksum;
299           /* old_l0 always 0, see the rewrite setup */
300           new_l0 = 
301             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
302           
303           sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
304                                  length /* changed member */);
305           ip0->checksum = ip_csum_fold (sum0);
306           ip0->length = new_l0;
307
308           /* Reset to look up tunnel partner in the configured FIB */
309           vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
310           vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
311           pkts_encapsulated ++;
312
313           len0 = vlib_buffer_length_in_chain(vm, b0);
314           stats_n_packets += 1;
315           stats_n_bytes += len0;
316
317           /* Batch stats increment on the same vxlan tunnel so counter is not
318            incremented per packet. Note stats are still incremented for deleted
319            and admin-down tunnel where packets are dropped. It is not worthwhile
320            to check for this rare case and affect normal path performance. */
321           if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index)) {
322             stats_n_packets -= 1;
323             stats_n_bytes -= len0;
324             if (stats_n_packets)
325               vlib_increment_combined_counter(
326                   im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
327                   cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
328             stats_n_packets = 1;
329             stats_n_bytes = len0;
330             stats_sw_if_index = sw_if_index0;
331           }
332           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
333             {
334               nsh_gre_encap_trace_t *tr = 
335                 vlib_add_trace (vm, node, b0, sizeof (*tr));
336               tr->tunnel_index = t0 - ngm->tunnels;
337             }
338           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
339                                            to_next, n_left_to_next,
340                                            bi0, next0);
341         }
342
343       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
344     }
345   vlib_node_increment_counter (vm, node->node_index, 
346                                NSH_GRE_ENCAP_ERROR_ENCAPSULATED, 
347                                pkts_encapsulated);
348   /* Increment any remaining batch stats */
349   if (stats_n_packets) {
350     vlib_increment_combined_counter(
351         im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, cpu_index,
352         stats_sw_if_index, stats_n_packets, stats_n_bytes);
353     node->runtime_data[0] = stats_sw_if_index;
354   }
355
356   return from_frame->n_vectors;
357 }
358
359 VLIB_REGISTER_NODE (nsh_gre_encap_node) = {
360   .function = nsh_gre_encap,
361   .name = "nsh-gre-encap",
362   .vector_size = sizeof (u32),
363   .format_trace = format_nsh_gre_encap_trace,
364   .type = VLIB_NODE_TYPE_INTERNAL,
365
366   .n_errors = ARRAY_LEN(nsh_gre_encap_error_strings),
367   .error_strings = nsh_gre_encap_error_strings,
368
369   .n_next_nodes = NSH_GRE_ENCAP_N_NEXT,
370
371   //  add dispositions here
372   .next_nodes = {
373         [NSH_GRE_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
374         [NSH_GRE_ENCAP_NEXT_DROP] = "error-drop",
375   },
376 };