LISP GPE: initial CP commit and DP improvements
[vpp.git] / vnet / vnet / lisp-gpe / encap.c
1 /*
2  * Copyright (c) 2016 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
16 #include <vppinfra/error.h>
17 #include <vppinfra/hash.h>
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/udp.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/lisp-gpe/lisp_gpe.h>
23
24 /* Statistics (not really errors) */
25 #define foreach_lisp_gpe_encap_error    \
26 _(ENCAPSULATED, "good packets encapsulated")
27
28 static char * lisp_gpe_encap_error_strings[] = {
29 #define _(sym,string) string,
30   foreach_lisp_gpe_encap_error
31 #undef _
32 };
33
34 typedef enum {
35 #define _(sym,str) LISP_GPE_ENCAP_ERROR_##sym,
36   foreach_lisp_gpe_encap_error
37 #undef _
38   LISP_GPE_ENCAP_N_ERROR,
39 } lisp_gpe_encap_error_t;
40
41 typedef enum
42 {
43   LISP_GPE_ENCAP_NEXT_DROP,
44   LISP_GPE_ENCAP_NEXT_IP4_LOOKUP,
45   LISP_GPE_ENCAP_N_NEXT,
46 } lisp_gpe_encap_next_t;
47
48 typedef struct
49 {
50   u32 tunnel_index;
51 } lisp_gpe_encap_trace_t;
52
53 u8 *
54 format_lisp_gpe_encap_trace (u8 * s, va_list * args)
55 {
56   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
57   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
58   lisp_gpe_encap_trace_t * t = va_arg (*args, lisp_gpe_encap_trace_t *);
59
60   s = format (s, "LISP-GPE-ENCAP: tunnel %d", t->tunnel_index);
61   return s;
62 }
63
64 static uword
65 lisp_gpe_encap (vlib_main_t * vm, vlib_node_runtime_t * node,
66                 vlib_frame_t * from_frame)
67 {
68   u32 n_left_from, next_index, * from, * to_next;
69   lisp_gpe_main_t * lgm = &lisp_gpe_main;
70   u32 pkts_encapsulated = 0;
71
72   from = vlib_frame_vector_args (from_frame);
73   n_left_from = from_frame->n_vectors;
74
75   next_index = node->cached_next_index;
76
77   while (n_left_from > 0)
78     {
79       u32 n_left_to_next;
80
81       vlib_get_next_frame (vm, node, next_index,
82                            to_next, n_left_to_next);
83
84       while (n_left_from >= 4 && n_left_to_next >= 2)
85         {
86           u32 bi0, bi1;
87           vlib_buffer_t * b0, * b1;
88           u32 next0, next1;
89           u32 adj_index0, adj_index1, tunnel_index0, tunnel_index1;
90           ip_adjacency_t * adj0, * adj1;
91           lisp_gpe_tunnel_t * t0, * t1;
92
93           next0 = next1 = LISP_GPE_ENCAP_NEXT_IP4_LOOKUP;
94
95           /* Prefetch next iteration. */
96             {
97               vlib_buffer_t * p2, *p3;
98
99               p2 = vlib_get_buffer (vm, from[2]);
100               p3 = vlib_get_buffer (vm, from[3]);
101
102               vlib_prefetch_buffer_header(p2, LOAD);
103               vlib_prefetch_buffer_header(p3, LOAD);
104
105               CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
106               CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
107             }
108
109           bi0 = from[0];
110           bi1 = from[1];
111           to_next[0] = bi0;
112           to_next[1] = bi1;
113           from += 2;
114           to_next += 2;
115           n_left_to_next -= 2;
116           n_left_from -= 2;
117
118           b0 = vlib_get_buffer (vm, bi0);
119           b1 = vlib_get_buffer (vm, bi1);
120
121           /* Get adjacency and from it the tunnel_index */
122           adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
123           adj_index1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
124
125           adj0 = ip_get_adjacency (lgm->lookup_main, adj_index0);
126           adj1 = ip_get_adjacency (lgm->lookup_main, adj_index1);
127
128           tunnel_index0 = adj0->rewrite_header.sw_if_index;
129           tunnel_index1 = adj1->rewrite_header.sw_if_index;
130
131           t0 = pool_elt_at_index (lgm->tunnels, tunnel_index0);
132           t1 = pool_elt_at_index (lgm->tunnels, tunnel_index1);
133
134           ASSERT(t0 != 0);
135           ASSERT(t1 != 0);
136
137           ASSERT (sizeof(ip4_udp_lisp_gpe_header_t) == 36);
138           ip4_udp_encap_two (vm, b0, b1, t0->rewrite, t1->rewrite, 36);
139
140           /* Reset to look up tunnel partner in the configured FIB */
141           vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
142           vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
143
144           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
145             {
146               lisp_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b0,
147                                                            sizeof(*tr));
148               tr->tunnel_index = t0 - lgm->tunnels;
149             }
150           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
151             {
152               lisp_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b1,
153                                                            sizeof(*tr));
154               tr->tunnel_index = t1 - lgm->tunnels;
155             }
156
157           pkts_encapsulated += 2;
158
159           vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
160                                           n_left_to_next, bi0, bi1, next0,
161                                           next1);
162         }
163
164       while (n_left_from > 0 && n_left_to_next > 0)
165         {
166           vlib_buffer_t * b0;
167           u32 bi0, adj_index0, tunnel_index0;
168           u32 next0 = LISP_GPE_ENCAP_NEXT_IP4_LOOKUP;
169           lisp_gpe_tunnel_t * t0 = 0;
170           ip_adjacency_t * adj0;
171
172           bi0 = from[0];
173           to_next[0] = bi0;
174           from += 1;
175           to_next += 1;
176           n_left_from -= 1;
177           n_left_to_next -= 1;
178
179           b0 = vlib_get_buffer (vm, bi0);
180
181           /* Get adjacency and from it the tunnel_index */
182           adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
183           adj0 = ip_get_adjacency (lgm->lookup_main, adj_index0);
184
185           tunnel_index0 = adj0->rewrite_header.sw_if_index;
186           t0 = pool_elt_at_index (lgm->tunnels, tunnel_index0);
187
188           ASSERT(t0 != 0);
189
190           ASSERT (sizeof(ip4_udp_lisp_gpe_header_t) == 36);
191           ip4_udp_encap_one (vm, b0, t0->rewrite, 36);
192
193           /* Reset to look up tunnel partner in the configured FIB */
194           vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
195
196           pkts_encapsulated++;
197
198           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
199             {
200               lisp_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b0,
201                                                            sizeof(*tr));
202               tr->tunnel_index = t0 - lgm->tunnels;
203             }
204           vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
205                                           n_left_to_next, bi0, next0);
206         }
207
208       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
209     }
210   vlib_node_increment_counter (vm, node->node_index, 
211                                LISP_GPE_ENCAP_ERROR_ENCAPSULATED, 
212                                pkts_encapsulated);
213   return from_frame->n_vectors;
214 }
215
216 VLIB_REGISTER_NODE (lisp_gpe_encap_node) = {
217   .function = lisp_gpe_encap,
218   .name = "lisp-gpe-encap",
219   .vector_size = sizeof (u32),
220   .format_trace = format_lisp_gpe_encap_trace,
221   .type = VLIB_NODE_TYPE_INTERNAL,
222
223   .n_errors = ARRAY_LEN(lisp_gpe_encap_error_strings),
224   .error_strings = lisp_gpe_encap_error_strings,
225
226   .n_next_nodes = LISP_GPE_ENCAP_N_NEXT,
227
228   .next_nodes = {
229       [LISP_GPE_ENCAP_NEXT_DROP] = "error-drop",
230       [LISP_GPE_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
231   },
232 };