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