d5ea4b65ddb2f6f74ca4b063bfd62a5fb5981f6f
[vpp.git] / vnet / vnet / gre / node.c
1 /*
2  * node.c: gre packet processing
3  *
4  * Copyright (c) 2012 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/gre/gre.h>
21 #include <vppinfra/sparse_vec.h>
22
23 #define foreach_gre_input_next                  \
24 _(PUNT, "error-punt")                           \
25 _(DROP, "error-drop")                           \
26 _(ETHERNET_INPUT, "ethernet-input")             \
27 _(IP4_INPUT, "ip4-input")                       \
28 _(IP6_INPUT, "ip6-input")                       
29
30 typedef enum {
31 #define _(s,n) GRE_INPUT_NEXT_##s,
32   foreach_gre_input_next
33 #undef _
34   GRE_INPUT_N_NEXT,
35 } gre_input_next_t;
36
37 typedef struct {
38   u32 tunnel_id;
39   u32 length;
40   ip4_address_t src;
41   ip4_address_t dst;
42 } gre_rx_trace_t;
43
44 u8 * format_gre_rx_trace (u8 * s, va_list * args)
45 {
46   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
47   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
48   gre_rx_trace_t * t = va_arg (*args, gre_rx_trace_t *);
49     
50   s = format (s, "GRE: tunnel %d len %d src %U dst %U",
51               t->tunnel_id, clib_net_to_host_u16(t->length),
52               format_ip4_address, &t->src.as_u8,
53               format_ip4_address, &t->dst.as_u8);
54   return s;
55 }
56
57 typedef struct {
58   /* Sparse vector mapping gre protocol in network byte order
59      to next index. */
60   u16 * next_by_protocol;
61 } gre_input_runtime_t;
62
63 static uword
64 gre_input (vlib_main_t * vm,
65            vlib_node_runtime_t * node,
66            vlib_frame_t * from_frame)
67 {
68   gre_main_t * gm = &gre_main;
69   gre_input_runtime_t * rt = (void *) node->runtime_data;
70   __attribute__((unused)) u32 n_left_from, next_index, * from, * to_next;
71   u64 cached_tunnel_key = (u64) ~0;
72   u32 cached_tunnel_sw_if_index = 0, tunnel_sw_if_index;
73   u32 cached_tunnel_fib_index = 0, tunnel_fib_index;
74
75   u32 cpu_index = os_get_cpu_number();
76
77   from = vlib_frame_vector_args (from_frame);
78   n_left_from = from_frame->n_vectors;
79
80   next_index = node->cached_next_index;
81
82   while (n_left_from > 0)
83     {
84       u32 n_left_to_next;
85
86       vlib_get_next_frame (vm, node, next_index,
87                            to_next, n_left_to_next);
88
89       while (n_left_from >= 4 && n_left_to_next >= 2)
90         {
91           u32 bi0, bi1;
92           vlib_buffer_t * b0, * b1;
93           gre_header_t * h0, * h1;
94           u16 version0, version1;
95           int verr0, verr1;
96           u32 i0, i1, next0, next1, protocol0, protocol1;
97           ip4_header_t *ip0, *ip1;
98
99           /* Prefetch next iteration. */
100           {
101             vlib_buffer_t * p2, * p3;
102
103             p2 = vlib_get_buffer (vm, from[2]);
104             p3 = vlib_get_buffer (vm, from[3]);
105
106             vlib_prefetch_buffer_header (p2, LOAD);
107             vlib_prefetch_buffer_header (p3, LOAD);
108
109             CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
110             CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
111           }
112
113           bi0 = from[0];
114           bi1 = from[1];
115           to_next[0] = bi0;
116           to_next[1] = bi1;
117           from += 2;
118           to_next += 2;
119           n_left_to_next -= 2;
120           n_left_from -= 2;
121
122           b0 = vlib_get_buffer (vm, bi0);
123           b1 = vlib_get_buffer (vm, bi1);
124
125           /* ip4_local hands us the ip header, not the gre header */
126           ip0 = vlib_buffer_get_current (b0);
127           ip1 = vlib_buffer_get_current (b1);
128
129           /* Save src + dst ip4 address, e.g. for mpls-o-gre */
130           vnet_buffer(b0)->gre.src = ip0->src_address.as_u32;
131           vnet_buffer(b0)->gre.dst = ip0->dst_address.as_u32;
132           vnet_buffer(b1)->gre.src = ip1->src_address.as_u32;
133           vnet_buffer(b1)->gre.dst = ip1->dst_address.as_u32;
134
135           vlib_buffer_advance (b0, sizeof (*ip0));
136           vlib_buffer_advance (b1, sizeof (*ip1));
137
138           h0 = vlib_buffer_get_current (b0);
139           h1 = vlib_buffer_get_current (b1);
140
141           /* Index sparse array with network byte order. */
142           protocol0 = h0->protocol;
143           protocol1 = h1->protocol;
144           sparse_vec_index2 (rt->next_by_protocol, protocol0, protocol1, 
145                              &i0, &i1);
146           next0 = vec_elt(rt->next_by_protocol, i0);
147           next1 = vec_elt(rt->next_by_protocol, i1);
148
149           b0->error = node->errors[i0 == SPARSE_VEC_INVALID_INDEX ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
150           b1->error = node->errors[i1 == SPARSE_VEC_INVALID_INDEX ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
151           
152           version0 = clib_net_to_host_u16 (h0->flags_and_version);
153           verr0 =  version0 & GRE_VERSION_MASK;
154           version1 = clib_net_to_host_u16 (h1->flags_and_version);
155           verr1 =  version1 & GRE_VERSION_MASK;
156
157           b0->error = verr0 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] 
158               : b0->error;
159           next0 = verr0 ? GRE_INPUT_NEXT_DROP : next0;
160           b1->error = verr1 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] 
161               : b1->error;
162           next1 = verr1 ? GRE_INPUT_NEXT_DROP : next1;
163
164
165           /* RPF check for ip4/ip6 input */
166           if (PREDICT_FALSE(next0 == GRE_INPUT_NEXT_IP4_INPUT
167                             || next0 == GRE_INPUT_NEXT_IP6_INPUT
168                             || next0 == GRE_INPUT_NEXT_ETHERNET_INPUT))
169             {
170               u64 key = ((u64)(vnet_buffer(b0)->gre.dst) << 32) |
171                          (u64)(vnet_buffer(b0)->gre.src);
172
173               if (cached_tunnel_key != key)
174                 {
175                   vnet_hw_interface_t * hi;
176                   gre_tunnel_t * t;
177                   uword * p;
178
179                   ip4_main_t * ip4m = &ip4_main;
180                   p = hash_get (gm->tunnel_by_key, key);
181                   if (!p)
182                     {
183                       next0 = GRE_INPUT_NEXT_DROP;
184                       b0->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
185                       goto drop0;
186                     }
187                   t = pool_elt_at_index (gm->tunnels, p[0]);
188                   hi = vnet_get_hw_interface (gm->vnet_main,
189                             t->hw_if_index);
190                   tunnel_sw_if_index = hi->sw_if_index;
191                   tunnel_fib_index = vec_elt (ip4m->fib_index_by_sw_if_index,
192                                               tunnel_sw_if_index);
193
194                   cached_tunnel_sw_if_index = tunnel_sw_if_index;
195                   cached_tunnel_fib_index = tunnel_fib_index;
196                 }
197               else
198                 {
199                   tunnel_sw_if_index = cached_tunnel_sw_if_index;
200                   tunnel_fib_index = cached_tunnel_fib_index;
201                 }
202
203               u32 len = vlib_buffer_length_in_chain (vm, b0);
204               vnet_interface_main_t *im = &gm->vnet_main->interface_main;
205               vlib_increment_combined_counter (im->combined_sw_if_counters
206                                                + VNET_INTERFACE_COUNTER_RX,
207                                                cpu_index,
208                                                tunnel_sw_if_index,
209                                                1 /* packets */,
210                                                len /* bytes */);
211
212               vnet_buffer(b0)->sw_if_index[VLIB_TX] = tunnel_fib_index;
213               vnet_buffer(b0)->sw_if_index[VLIB_RX] = tunnel_sw_if_index;
214             }
215
216 drop0:
217           if (PREDICT_FALSE(next1 == GRE_INPUT_NEXT_IP4_INPUT
218                             || next1 == GRE_INPUT_NEXT_IP6_INPUT
219                             || next1 == GRE_INPUT_NEXT_ETHERNET_INPUT))
220             {
221               u64 key = ((u64)(vnet_buffer(b1)->gre.dst) << 32) |
222                          (u64)(vnet_buffer(b1)->gre.src);
223
224               if (cached_tunnel_key != key)
225                 {
226                   vnet_hw_interface_t * hi;
227                   gre_tunnel_t * t;
228                   uword * p;
229
230                   ip4_main_t * ip4m = &ip4_main;
231                   p = hash_get (gm->tunnel_by_key, key);
232                   if (!p)
233                     {
234                       next1 = GRE_INPUT_NEXT_DROP;
235                       b1->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
236                       goto drop1;
237                     }
238                   t = pool_elt_at_index (gm->tunnels, p[0]);
239                   hi = vnet_get_hw_interface (gm->vnet_main,
240                             t->hw_if_index);
241                   tunnel_sw_if_index = hi->sw_if_index;
242                   tunnel_fib_index = vec_elt (ip4m->fib_index_by_sw_if_index,
243                                               tunnel_sw_if_index);
244
245                   cached_tunnel_sw_if_index = tunnel_sw_if_index;
246                   cached_tunnel_fib_index = tunnel_fib_index;
247                 }
248               else
249                 {
250                   tunnel_sw_if_index = cached_tunnel_sw_if_index;
251                   tunnel_fib_index = cached_tunnel_fib_index;
252                 }
253
254               u32 len = vlib_buffer_length_in_chain (vm, b1);
255               vnet_interface_main_t *im = &gm->vnet_main->interface_main;
256               vlib_increment_combined_counter (im->combined_sw_if_counters
257                                                + VNET_INTERFACE_COUNTER_RX,
258                                                cpu_index,
259                                                tunnel_sw_if_index,
260                                                1 /* packets */,
261                                                len /* bytes */);
262
263               vnet_buffer(b1)->sw_if_index[VLIB_TX] = tunnel_fib_index;
264               vnet_buffer(b1)->sw_if_index[VLIB_RX] = tunnel_sw_if_index;
265             }
266 drop1:
267           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
268             {
269               gre_rx_trace_t *tr = vlib_add_trace (vm, node, 
270                                                    b0, sizeof (*tr));
271               tr->tunnel_id = ~0;
272               tr->length = ip0->length;
273               tr->src.as_u32 = ip0->src_address.as_u32;
274               tr->dst.as_u32 = ip0->dst_address.as_u32;
275             }
276
277           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) 
278             {
279               gre_rx_trace_t *tr = vlib_add_trace (vm, node, 
280                                                    b1, sizeof (*tr));
281               tr->tunnel_id = ~0;
282               tr->length = ip1->length;
283               tr->src.as_u32 = ip1->src_address.as_u32;
284               tr->dst.as_u32 = ip1->dst_address.as_u32;
285             }
286
287           vlib_buffer_advance (b0, sizeof (*h0));
288           vlib_buffer_advance (b1, sizeof (*h1));
289
290           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
291                                            to_next, n_left_to_next,
292                                            bi0, bi1, next0, next1);
293         }
294     
295       while (n_left_from > 0 && n_left_to_next > 0)
296         {
297           u32 bi0;
298           vlib_buffer_t * b0;
299           gre_header_t * h0;
300           ip4_header_t * ip0;
301           u16 version0;
302           int verr0;
303           u32 i0, next0;
304
305           bi0 = from[0];
306           to_next[0] = bi0;
307           from += 1;
308           to_next += 1;
309           n_left_from -= 1;
310           n_left_to_next -= 1;
311
312           b0 = vlib_get_buffer (vm, bi0);
313           ip0 = vlib_buffer_get_current (b0);
314
315           vnet_buffer(b0)->gre.src = ip0->src_address.as_u32;
316           vnet_buffer(b0)->gre.dst = ip0->dst_address.as_u32;
317
318           vlib_buffer_advance (b0, sizeof (*ip0));
319
320           h0 = vlib_buffer_get_current (b0);
321
322           i0 = sparse_vec_index (rt->next_by_protocol, h0->protocol);
323           next0 = vec_elt(rt->next_by_protocol, i0);
324
325           b0->error = 
326               node->errors[i0 == SPARSE_VEC_INVALID_INDEX 
327                            ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
328           
329           version0 = clib_net_to_host_u16 (h0->flags_and_version);
330           verr0 =  version0 & GRE_VERSION_MASK;
331           b0->error = verr0 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] 
332               : b0->error;
333           next0 = verr0 ? GRE_INPUT_NEXT_DROP : next0;
334
335
336           /* For IP payload we need to find source interface
337              so we can increase counters and help forward node to
338              pick right FIB */
339           if (PREDICT_FALSE(next0 == GRE_INPUT_NEXT_IP4_INPUT
340                             || next0 == GRE_INPUT_NEXT_IP6_INPUT
341                             || next0 == GRE_INPUT_NEXT_ETHERNET_INPUT))
342             {
343               u64 key = ((u64)(vnet_buffer(b0)->gre.dst) << 32) |
344                          (u64)(vnet_buffer(b0)->gre.src);
345
346               if (cached_tunnel_key != key)
347                 {
348                   vnet_hw_interface_t * hi;
349                   gre_tunnel_t * t;
350                   uword * p;
351
352                   ip4_main_t * ip4m = &ip4_main;
353                   p = hash_get (gm->tunnel_by_key, key);
354                   if (!p)
355                     {
356                       next0 = GRE_INPUT_NEXT_DROP;
357                       b0->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
358                       goto drop;
359                     }
360                   t = pool_elt_at_index (gm->tunnels, p[0]);
361                   hi = vnet_get_hw_interface (gm->vnet_main,
362                             t->hw_if_index);
363                   tunnel_sw_if_index = hi->sw_if_index;
364                   tunnel_fib_index = vec_elt (ip4m->fib_index_by_sw_if_index,
365                                               tunnel_sw_if_index);
366
367                   cached_tunnel_sw_if_index = tunnel_sw_if_index;
368                   cached_tunnel_fib_index = tunnel_fib_index;
369                 }
370               else
371                 {
372                   tunnel_sw_if_index = cached_tunnel_sw_if_index;
373                   tunnel_fib_index = cached_tunnel_fib_index;
374                 }
375
376               u32 len = vlib_buffer_length_in_chain (vm, b0);
377               vnet_interface_main_t *im = &gm->vnet_main->interface_main;
378               vlib_increment_combined_counter (im->combined_sw_if_counters
379                                                + VNET_INTERFACE_COUNTER_RX,
380                                                cpu_index,
381                                                tunnel_sw_if_index,
382                                                1 /* packets */,
383                                                len /* bytes */);
384
385               vnet_buffer(b0)->sw_if_index[VLIB_TX] = tunnel_fib_index;
386               vnet_buffer(b0)->sw_if_index[VLIB_RX] = tunnel_sw_if_index;
387             }
388
389 drop:
390           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
391             {
392               gre_rx_trace_t *tr = vlib_add_trace (vm, node, 
393                                                    b0, sizeof (*tr));
394               tr->tunnel_id = ~0;
395               tr->length = ip0->length;
396               tr->src.as_u32 = ip0->src_address.as_u32;
397               tr->dst.as_u32 = ip0->dst_address.as_u32;
398             }
399
400           vlib_buffer_advance (b0, sizeof (*h0));
401
402           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
403                                            to_next, n_left_to_next,
404                                            bi0, next0);
405         }
406
407       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
408     }
409   vlib_node_increment_counter (vm, gre_input_node.index,
410                                GRE_ERROR_PKTS_DECAP, from_frame->n_vectors);
411   return from_frame->n_vectors;
412 }
413
414 static char * gre_error_strings[] = {
415 #define gre_error(n,s) s,
416 #include "error.def"
417 #undef gre_error
418 };
419
420 VLIB_REGISTER_NODE (gre_input_node) = {
421   .function = gre_input,
422   .name = "gre-input",
423   /* Takes a vector of packets. */
424   .vector_size = sizeof (u32),
425
426   .runtime_data_bytes = sizeof (gre_input_runtime_t),
427
428   .n_errors = GRE_N_ERROR,
429   .error_strings = gre_error_strings,
430
431   .n_next_nodes = GRE_INPUT_N_NEXT,
432   .next_nodes = {
433 #define _(s,n) [GRE_INPUT_NEXT_##s] = n,
434     foreach_gre_input_next
435 #undef _
436   },
437
438   .format_buffer = format_gre_header_with_length,
439   .format_trace = format_gre_rx_trace,
440   .unformat_buffer = unformat_gre_header,
441 };
442
443 VLIB_NODE_FUNCTION_MULTIARCH (gre_input_node, gre_input)
444
445 void
446 gre_register_input_protocol (vlib_main_t * vm,
447                              gre_protocol_t protocol,
448                              u32 node_index)
449 {
450   gre_main_t * em = &gre_main;
451   gre_protocol_info_t * pi;
452   gre_input_runtime_t * rt;
453   u16 * n;
454
455   {
456     clib_error_t * error = vlib_call_init_function (vm, gre_input_init);
457     if (error)
458       clib_error_report (error);
459   }
460
461   pi = gre_get_protocol_info (em, protocol);
462   pi->node_index = node_index;
463   pi->next_index = vlib_node_add_next (vm, 
464                                        gre_input_node.index,
465                                        node_index);
466
467   /* Setup gre protocol -> next index sparse vector mapping. */
468   rt = vlib_node_get_runtime_data (vm, gre_input_node.index);
469   n = sparse_vec_validate (rt->next_by_protocol, 
470                            clib_host_to_net_u16 (protocol));
471   n[0] = pi->next_index;
472 }
473
474 static void
475 gre_setup_node (vlib_main_t * vm, u32 node_index)
476 {
477   vlib_node_t * n = vlib_get_node (vm, node_index);
478   pg_node_t * pn = pg_get_node (node_index);
479
480   n->format_buffer = format_gre_header_with_length;
481   n->unformat_buffer = unformat_gre_header;
482   pn->unformat_edit = unformat_pg_gre_header;
483 }
484
485 static clib_error_t * gre_input_init (vlib_main_t * vm)
486 {
487   gre_input_runtime_t * rt;
488   vlib_node_t *ethernet_input, *ip4_input, *ip6_input, *mpls_unicast_input;
489
490   {
491     clib_error_t * error; 
492     error = vlib_call_init_function (vm, gre_init);
493     if (error)
494       clib_error_report (error);
495   }
496
497   gre_setup_node (vm, gre_input_node.index);
498
499   rt = vlib_node_get_runtime_data (vm, gre_input_node.index);
500
501   rt->next_by_protocol = sparse_vec_new
502     (/* elt bytes */ sizeof (rt->next_by_protocol[0]),
503      /* bits in index */ BITS (((gre_header_t *) 0)->protocol));
504
505   /* These could be moved to the supported protocol input node defn's */
506   ethernet_input = vlib_get_node_by_name (vm, (u8 *)"ethernet-input");
507   ASSERT(ethernet_input);
508   ip4_input = vlib_get_node_by_name (vm, (u8 *)"ip4-input");
509   ASSERT(ip4_input);
510   ip6_input = vlib_get_node_by_name (vm, (u8 *)"ip6-input");
511   ASSERT(ip6_input);
512   mpls_unicast_input = vlib_get_node_by_name (vm, (u8 *)"mpls-gre-input");
513   ASSERT(mpls_unicast_input);
514
515   gre_register_input_protocol (vm, GRE_PROTOCOL_teb,
516                                ethernet_input->index);
517
518   gre_register_input_protocol (vm, GRE_PROTOCOL_ip4, 
519                                ip4_input->index);
520
521   gre_register_input_protocol (vm, GRE_PROTOCOL_ip6, 
522                                ip6_input->index);
523
524   gre_register_input_protocol (vm, GRE_PROTOCOL_mpls_unicast,
525                                mpls_unicast_input->index);
526
527   ip4_register_protocol (IP_PROTOCOL_GRE, gre_input_node.index);
528
529   return 0;
530 }
531
532 VLIB_INIT_FUNCTION (gre_input_init);