VPP43 - NSH refactoring: Added nsh-map nodes
[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 _(IP4_INPUT, "ip4-input")                       \
27 _(IP6_INPUT, "ip6-input")                       \
28 _(NSH_INPUT, "nsh-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
62   u32 * sparse_index_by_next_index;
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   gre_input_runtime_t * rt = (void *) node->runtime_data;
72   __attribute__((unused)) u32 n_left_from, next_index, i_next, * from, * to_next;
73   u64 cached_tunnel_key = (u64) ~0;
74   u32 cached_tunnel_sw_if_index = 0, tunnel_sw_if_index;
75   u32 cached_tunnel_fib_index = 0, tunnel_fib_index;
76
77   u32 cpu_index = os_get_cpu_number();
78
79   from = vlib_frame_vector_args (from_frame);
80   n_left_from = from_frame->n_vectors;
81
82   next_index = node->cached_next_index;
83   i_next = vec_elt (rt->sparse_index_by_next_index, next_index);
84
85   while (n_left_from > 0)
86     {
87       u32 n_left_to_next;
88
89       vlib_get_next_frame (vm, node, next_index,
90                            to_next, n_left_to_next);
91
92       while (n_left_from >= 4 && n_left_to_next >= 2)
93         {
94           u32 bi0, bi1;
95           vlib_buffer_t * b0, * b1;
96           gre_header_t * h0, * h1;
97           u16 version0, version1;
98           int verr0, verr1;
99           u32 i0, i1, next0, next1, protocol0, protocol1;
100           ip4_header_t *ip0, *ip1;
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, sizeof (h0[0]), LOAD);
113             CLIB_PREFETCH (p3->data, sizeof (h1[0]), 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           /* ip4_local hands us the ip header, not the gre header */
129           ip0 = vlib_buffer_get_current (b0);
130           ip1 = vlib_buffer_get_current (b1);
131
132           /* Save src + dst ip4 address, e.g. for mpls-o-gre */
133           vnet_buffer(b0)->gre.src = ip0->src_address.as_u32;
134           vnet_buffer(b0)->gre.dst = ip0->dst_address.as_u32;
135           vnet_buffer(b1)->gre.src = ip1->src_address.as_u32;
136           vnet_buffer(b1)->gre.dst = ip1->dst_address.as_u32;
137
138           vlib_buffer_advance (b0, sizeof (*ip0));
139           vlib_buffer_advance (b1, sizeof (*ip1));
140
141           h0 = vlib_buffer_get_current (b0);
142           h1 = vlib_buffer_get_current (b1);
143
144           /* Index sparse array with network byte order. */
145           protocol0 = h0->protocol;
146           protocol1 = h1->protocol;
147           sparse_vec_index2 (rt->next_by_protocol, protocol0, protocol1, 
148                              &i0, &i1);
149           next0 = vec_elt(rt->next_by_protocol, i0);
150           next1 = vec_elt(rt->next_by_protocol, i1);
151
152           b0->error = node->errors[next0 == SPARSE_VEC_INVALID_INDEX ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
153           b1->error = node->errors[next1 == SPARSE_VEC_INVALID_INDEX ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
154           
155           version0 = clib_net_to_host_u16 (h0->flags_and_version);
156           verr0 =  version0 & GRE_VERSION_MASK;
157           version1 = clib_net_to_host_u16 (h1->flags_and_version);
158           verr1 =  version1 & GRE_VERSION_MASK;
159
160           b0->error = verr0 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] 
161               : b0->error;
162           next0 = verr0 ? GRE_INPUT_NEXT_DROP : next0;
163           b1->error = verr1 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] 
164               : b1->error;
165           next1 = verr1 ? GRE_INPUT_NEXT_DROP : next1;
166
167           /* RPF check for ip4/ip6 input */
168           if (PREDICT_FALSE(next0 == GRE_INPUT_NEXT_IP4_INPUT 
169                             || next0 == GRE_INPUT_NEXT_IP6_INPUT))
170             {
171               u64 key = ((u64)(vnet_buffer(b0)->gre.dst) << 32) |
172                          (u64)(vnet_buffer(b0)->gre.src);
173
174               if (cached_tunnel_key != key)
175                 {
176                   vnet_hw_interface_t * hi;
177                   gre_tunnel_t * t;
178                   uword * p;
179
180                   ip4_main_t * ip4m = &ip4_main;
181                   p = hash_get (gm->tunnel_by_key, key);
182                   if (!p)
183                     {
184                       next0 = GRE_INPUT_NEXT_DROP;
185                       b0->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
186                       goto drop0;
187                     }
188                   t = pool_elt_at_index (gm->tunnels, p[0]);
189                   hi = vnet_get_hw_interface (gm->vnet_main,
190                             t->hw_if_index);
191                   tunnel_sw_if_index = hi->sw_if_index;
192                   tunnel_fib_index = vec_elt (ip4m->fib_index_by_sw_if_index,
193                                               tunnel_sw_if_index);
194
195                   cached_tunnel_sw_if_index = tunnel_sw_if_index;
196                   cached_tunnel_fib_index = tunnel_fib_index;
197                 }
198               else
199                 {
200                   tunnel_sw_if_index = cached_tunnel_sw_if_index;
201                   tunnel_fib_index = cached_tunnel_fib_index;
202                 }
203
204               u32 len = vlib_buffer_length_in_chain (vm, b0);
205               vnet_interface_main_t *im = &gm->vnet_main->interface_main;
206               vlib_increment_combined_counter (im->combined_sw_if_counters
207                                                + VNET_INTERFACE_COUNTER_RX,
208                                                cpu_index,
209                                                tunnel_sw_if_index,
210                                                1 /* packets */,
211                                                len /* bytes */);
212
213               vnet_buffer(b0)->sw_if_index[VLIB_TX] = tunnel_fib_index;
214             }
215
216 drop0:
217           if (PREDICT_FALSE(next1 == GRE_INPUT_NEXT_IP4_INPUT 
218                             || next1 == GRE_INPUT_NEXT_IP6_INPUT))
219             {
220               u64 key = ((u64)(vnet_buffer(b1)->gre.dst) << 32) |
221                          (u64)(vnet_buffer(b1)->gre.src);
222
223               if (cached_tunnel_key != key)
224                 {
225                   vnet_hw_interface_t * hi;
226                   gre_tunnel_t * t;
227                   uword * p;
228
229                   ip4_main_t * ip4m = &ip4_main;
230                   p = hash_get (gm->tunnel_by_key, key);
231                   if (!p)
232                     {
233                       next1 = GRE_INPUT_NEXT_DROP;
234                       b1->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
235                       goto drop1;
236                     }
237                   t = pool_elt_at_index (gm->tunnels, p[0]);
238                   hi = vnet_get_hw_interface (gm->vnet_main,
239                             t->hw_if_index);
240                   tunnel_sw_if_index = hi->sw_if_index;
241                   tunnel_fib_index = vec_elt (ip4m->fib_index_by_sw_if_index,
242                                               tunnel_sw_if_index);
243
244                   cached_tunnel_sw_if_index = tunnel_sw_if_index;
245                   cached_tunnel_fib_index = tunnel_fib_index;
246                 }
247               else
248                 {
249                   tunnel_sw_if_index = cached_tunnel_sw_if_index;
250                   tunnel_fib_index = cached_tunnel_fib_index;
251                 }
252
253               u32 len = vlib_buffer_length_in_chain (vm, b1);
254               vnet_interface_main_t *im = &gm->vnet_main->interface_main;
255               vlib_increment_combined_counter (im->combined_sw_if_counters
256                                                + VNET_INTERFACE_COUNTER_RX,
257                                                cpu_index,
258                                                tunnel_sw_if_index,
259                                                1 /* packets */,
260                                                len /* bytes */);
261
262               vnet_buffer(b1)->sw_if_index[VLIB_TX] = tunnel_fib_index;
263             }
264 drop1:
265           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
266             {
267               gre_rx_trace_t *tr = vlib_add_trace (vm, node, 
268                                                    b0, sizeof (*tr));
269               tr->tunnel_id = ~0;
270               tr->length = ip0->length;
271               tr->src.as_u32 = ip0->src_address.as_u32;
272               tr->dst.as_u32 = ip0->dst_address.as_u32;
273             }
274
275           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) 
276             {
277               gre_rx_trace_t *tr = vlib_add_trace (vm, node, 
278                                                    b1, sizeof (*tr));
279               tr->tunnel_id = ~0;
280               tr->length = ip1->length;
281               tr->src.as_u32 = ip1->src_address.as_u32;
282               tr->dst.as_u32 = ip1->dst_address.as_u32;
283             }
284
285           vlib_buffer_advance (b0, sizeof (*h0));
286           vlib_buffer_advance (b1, sizeof (*h1));
287
288           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
289                                            to_next, n_left_to_next,
290                                            bi0, bi1, next0, next1);
291         }
292     
293       while (n_left_from > 0 && n_left_to_next > 0)
294         {
295           u32 bi0;
296           vlib_buffer_t * b0;
297           gre_header_t * h0;
298           ip4_header_t * ip0;
299           u16 version0;
300           int verr0;
301           u32 i0, next0;
302
303           bi0 = from[0];
304           to_next[0] = bi0;
305           from += 1;
306           to_next += 1;
307           n_left_from -= 1;
308           n_left_to_next -= 1;
309
310           b0 = vlib_get_buffer (vm, bi0);
311           ip0 = vlib_buffer_get_current (b0);
312
313           vnet_buffer(b0)->gre.src = ip0->src_address.as_u32;
314           vnet_buffer(b0)->gre.dst = ip0->dst_address.as_u32;
315
316           vlib_buffer_advance (b0, sizeof (*ip0));
317
318           h0 = vlib_buffer_get_current (b0);
319
320           i0 = sparse_vec_index (rt->next_by_protocol, h0->protocol);
321           next0 = vec_elt(rt->next_by_protocol, i0);
322
323           b0->error = 
324               node->errors[next0 == SPARSE_VEC_INVALID_INDEX 
325                            ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
326           
327           version0 = clib_net_to_host_u16 (h0->flags_and_version);
328           verr0 =  version0 & GRE_VERSION_MASK;
329           b0->error = verr0 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] 
330               : b0->error;
331           next0 = verr0 ? GRE_INPUT_NEXT_DROP : next0;
332
333           /* For IP payload we need to find source interface
334              so we can increase counters and help forward node to
335              pick right FIB */
336           if (PREDICT_FALSE(next0 == GRE_INPUT_NEXT_IP4_INPUT 
337                             || next0 == GRE_INPUT_NEXT_IP6_INPUT))
338             {
339               u64 key = ((u64)(vnet_buffer(b0)->gre.dst) << 32) |
340                          (u64)(vnet_buffer(b0)->gre.src);
341
342               if (cached_tunnel_key != key)
343                 {
344                   vnet_hw_interface_t * hi;
345                   gre_tunnel_t * t;
346                   uword * p;
347
348                   ip4_main_t * ip4m = &ip4_main;
349                   p = hash_get (gm->tunnel_by_key, key);
350                   if (!p)
351                     {
352                       next0 = GRE_INPUT_NEXT_DROP;
353                       b0->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
354                       goto drop;
355                     }
356                   t = pool_elt_at_index (gm->tunnels, p[0]);
357                   hi = vnet_get_hw_interface (gm->vnet_main,
358                             t->hw_if_index);
359                   tunnel_sw_if_index = hi->sw_if_index;
360                   tunnel_fib_index = vec_elt (ip4m->fib_index_by_sw_if_index,
361                                               tunnel_sw_if_index);
362
363                   cached_tunnel_sw_if_index = tunnel_sw_if_index;
364                   cached_tunnel_fib_index = tunnel_fib_index;
365                 }
366               else
367                 {
368                   tunnel_sw_if_index = cached_tunnel_sw_if_index;
369                   tunnel_fib_index = cached_tunnel_fib_index;
370                 }
371
372               u32 len = vlib_buffer_length_in_chain (vm, b0);
373               vnet_interface_main_t *im = &gm->vnet_main->interface_main;
374               vlib_increment_combined_counter (im->combined_sw_if_counters
375                                                + VNET_INTERFACE_COUNTER_RX,
376                                                cpu_index,
377                                                tunnel_sw_if_index,
378                                                1 /* packets */,
379                                                len /* bytes */);
380
381               vnet_buffer(b0)->sw_if_index[VLIB_TX] = tunnel_fib_index;
382             }
383
384 drop:
385           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
386             {
387               gre_rx_trace_t *tr = vlib_add_trace (vm, node, 
388                                                    b0, sizeof (*tr));
389               tr->tunnel_id = ~0;
390               tr->length = ip0->length;
391               tr->src.as_u32 = ip0->src_address.as_u32;
392               tr->dst.as_u32 = ip0->dst_address.as_u32;
393             }
394
395           vlib_buffer_advance (b0, sizeof (*h0));
396
397           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
398                                            to_next, n_left_to_next,
399                                            bi0, next0);
400         }
401
402       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
403     }
404   vlib_node_increment_counter (vm, gre_input_node.index,
405                                GRE_ERROR_PKTS_DECAP, from_frame->n_vectors);
406   return from_frame->n_vectors;
407 }
408
409 static char * gre_error_strings[] = {
410 #define gre_error(n,s) s,
411 #include "error.def"
412 #undef gre_error
413 };
414
415 VLIB_REGISTER_NODE (gre_input_node) = {
416   .function = gre_input,
417   .name = "gre-input",
418   /* Takes a vector of packets. */
419   .vector_size = sizeof (u32),
420
421   .runtime_data_bytes = sizeof (gre_input_runtime_t),
422
423   .n_errors = GRE_N_ERROR,
424   .error_strings = gre_error_strings,
425
426   .n_next_nodes = GRE_INPUT_N_NEXT,
427   .next_nodes = {
428 #define _(s,n) [GRE_INPUT_NEXT_##s] = n,
429     foreach_gre_input_next
430 #undef _
431   },
432
433   .format_buffer = format_gre_header_with_length,
434   .format_trace = format_gre_rx_trace,
435   .unformat_buffer = unformat_gre_header,
436 };
437
438 void
439 gre_register_input_protocol (vlib_main_t * vm,
440                              gre_protocol_t protocol,
441                              u32 node_index)
442 {
443   gre_main_t * em = &gre_main;
444   gre_protocol_info_t * pi;
445   gre_input_runtime_t * rt;
446   u16 * n;
447   u32 i;
448
449   {
450     clib_error_t * error = vlib_call_init_function (vm, gre_input_init);
451     if (error)
452       clib_error_report (error);
453   }
454
455   pi = gre_get_protocol_info (em, protocol);
456   pi->node_index = node_index;
457   pi->next_index = vlib_node_add_next (vm, 
458                                        gre_input_node.index,
459                                        node_index);
460
461   /* Setup gre protocol -> next index sparse vector mapping. */
462   rt = vlib_node_get_runtime_data (vm, gre_input_node.index);
463   n = sparse_vec_validate (rt->next_by_protocol, 
464                            clib_host_to_net_u16 (protocol));
465   n[0] = pi->next_index;
466
467   /* Rebuild next index -> sparse index inverse mapping when sparse vector
468      is updated. */
469   vec_validate (rt->sparse_index_by_next_index, pi->next_index);
470   for (i = 1; i < vec_len (rt->next_by_protocol); i++)
471     rt->sparse_index_by_next_index[rt->next_by_protocol[i]] = i;
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 *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   vec_validate (rt->sparse_index_by_next_index, GRE_INPUT_NEXT_DROP);
506   vec_validate (rt->sparse_index_by_next_index, GRE_INPUT_NEXT_PUNT);
507   rt->sparse_index_by_next_index[GRE_INPUT_NEXT_DROP]
508     = SPARSE_VEC_INVALID_INDEX;
509   rt->sparse_index_by_next_index[GRE_INPUT_NEXT_PUNT]
510     = SPARSE_VEC_INVALID_INDEX;
511
512   /* These could be moved to the supported protocol input node defn's */
513   ip4_input = vlib_get_node_by_name (vm, (u8 *)"ip4-input");
514   ASSERT(ip4_input);
515   ip6_input = vlib_get_node_by_name (vm, (u8 *)"ip6-input");
516   ASSERT(ip6_input);
517   mpls_unicast_input = vlib_get_node_by_name (vm, (u8 *)"mpls-gre-input");
518   ASSERT(mpls_unicast_input);
519
520   gre_register_input_protocol (vm, GRE_PROTOCOL_ip4, 
521                                ip4_input->index);
522
523   gre_register_input_protocol (vm, GRE_PROTOCOL_ip6, 
524                                ip6_input->index);
525
526   gre_register_input_protocol (vm, GRE_PROTOCOL_mpls_unicast,
527                                mpls_unicast_input->index);
528
529   ip4_register_protocol (IP_PROTOCOL_GRE, gre_input_node.index);
530
531   return 0;
532 }
533
534 VLIB_INIT_FUNCTION (gre_input_init);