2 * node.c: gre packet processing
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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")
33 #define _(s,n) GRE_INPUT_NEXT_##s,
34 foreach_gre_input_next
47 u8 * format_gre_rx_trace (u8 * s, va_list * args)
49 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
50 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
51 gre_rx_trace_t * t = va_arg (*args, gre_rx_trace_t *);
53 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
54 t->tunnel_id, clib_net_to_host_u16(t->length),
55 format_ip46_address, &t->src, IP46_TYPE_ANY,
56 format_ip46_address, &t->dst, IP46_TYPE_ANY);
61 /* Sparse vector mapping gre protocol in network byte order
63 u16 * next_by_protocol;
64 } gre_input_runtime_t;
67 gre_input (vlib_main_t * vm,
68 vlib_node_runtime_t * node,
69 vlib_frame_t * from_frame,
72 gre_main_t * gm = &gre_main;
73 __attribute__((unused)) u32 n_left_from, next_index, * from, * to_next;
74 u64 cached_tunnel_key4;
75 u64 cached_tunnel_key6[4];
76 u32 cached_tunnel_sw_if_index = 0, tunnel_sw_if_index = 0;
78 u32 thread_index = vlib_get_thread_index();
80 vnet_interface_main_t *im = &gm->vnet_main->interface_main;
83 memset (&cached_tunnel_key4, 0xff, sizeof(cached_tunnel_key4));
85 memset (&cached_tunnel_key6, 0xff, sizeof(cached_tunnel_key6));
87 from = vlib_frame_vector_args (from_frame);
88 n_left_from = from_frame->n_vectors;
90 next_index = node->cached_next_index;
92 while (n_left_from > 0)
96 vlib_get_next_frame (vm, node, next_index,
97 to_next, n_left_to_next);
99 while (n_left_from >= 4 && n_left_to_next >= 2)
102 vlib_buffer_t * b0, * b1;
103 gre_header_t * h0, * h1;
104 u16 version0, version1;
106 u32 i0, i1, next0, next1, protocol0, protocol1;
107 ip4_header_t *ip4_0, *ip4_1;
108 ip6_header_t *ip6_0, *ip6_1;
109 u32 ip4_tun_src0, ip4_tun_dst0;
110 u32 ip4_tun_src1, ip4_tun_dst1;
111 u64 ip6_tun_src0[2], ip6_tun_dst0[2];
112 u64 ip6_tun_src1[2], ip6_tun_dst1[2];
114 /* Prefetch next iteration. */
116 vlib_buffer_t * p2, * p3;
118 p2 = vlib_get_buffer (vm, from[2]);
119 p3 = vlib_get_buffer (vm, from[3]);
121 vlib_prefetch_buffer_header (p2, LOAD);
122 vlib_prefetch_buffer_header (p3, LOAD);
124 CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
125 CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
137 b0 = vlib_get_buffer (vm, bi0);
138 b1 = vlib_get_buffer (vm, bi1);
142 /* ip4_local hands us the ip header, not the gre header */
143 ip4_0 = vlib_buffer_get_current (b0);
144 ip4_1 = vlib_buffer_get_current (b1);
145 /* Save src + dst ip4 address, e.g. for mpls-o-gre */
146 ip4_tun_src0 = ip4_0->src_address.as_u32;
147 ip4_tun_dst0 = ip4_0->dst_address.as_u32;
148 ip4_tun_src1 = ip4_1->src_address.as_u32;
149 ip4_tun_dst1 = ip4_1->dst_address.as_u32;
151 vlib_buffer_advance (b0, sizeof (*ip4_0));
152 vlib_buffer_advance (b1, sizeof (*ip4_1));
156 /* ip6_local hands us the ip header, not the gre header */
157 ip6_0 = vlib_buffer_get_current (b0);
158 ip6_1 = vlib_buffer_get_current (b1);
159 /* Save src + dst ip6 address, e.g. for mpls-o-gre */
160 ip6_tun_src0[0] = ip6_0->src_address.as_u64[0];
161 ip6_tun_src0[1] = ip6_0->src_address.as_u64[1];
162 ip6_tun_dst0[0] = ip6_0->dst_address.as_u64[0];
163 ip6_tun_dst0[1] = ip6_0->dst_address.as_u64[1];
164 ip6_tun_src1[0] = ip6_1->src_address.as_u64[0];
165 ip6_tun_src1[1] = ip6_1->src_address.as_u64[1];
166 ip6_tun_dst1[0] = ip6_1->dst_address.as_u64[0];
167 ip6_tun_dst1[1] = ip6_1->dst_address.as_u64[1];
169 vlib_buffer_advance (b0, sizeof (*ip6_0));
170 vlib_buffer_advance (b1, sizeof (*ip6_1));
173 h0 = vlib_buffer_get_current (b0);
174 h1 = vlib_buffer_get_current (b1);
176 /* Index sparse array with network byte order. */
177 protocol0 = h0->protocol;
178 protocol1 = h1->protocol;
179 sparse_vec_index2 (gm->next_by_protocol, protocol0, protocol1,
181 next0 = vec_elt(gm->next_by_protocol, i0);
182 next1 = vec_elt(gm->next_by_protocol, i1);
184 b0->error = node->errors[i0 == SPARSE_VEC_INVALID_INDEX ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
185 b1->error = node->errors[i1 == SPARSE_VEC_INVALID_INDEX ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
187 version0 = clib_net_to_host_u16 (h0->flags_and_version);
188 verr0 = version0 & GRE_VERSION_MASK;
189 version1 = clib_net_to_host_u16 (h1->flags_and_version);
190 verr1 = version1 & GRE_VERSION_MASK;
192 b0->error = verr0 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION]
194 next0 = verr0 ? GRE_INPUT_NEXT_DROP : next0;
195 b1->error = verr1 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION]
197 next1 = verr1 ? GRE_INPUT_NEXT_DROP : next1;
200 /* RPF check for ip4/ip6 input */
201 if (PREDICT_TRUE(next0 == GRE_INPUT_NEXT_IP4_INPUT
202 || next0 == GRE_INPUT_NEXT_IP6_INPUT
203 || next0 == GRE_INPUT_NEXT_ETHERNET_INPUT
204 || next0 == GRE_INPUT_NEXT_MPLS_INPUT))
210 key4 = ((u64)(ip4_tun_dst0) << 32) | (u64)(ip4_tun_src0);
214 key6[0] = ip6_tun_dst0[0];
215 key6[1] = ip6_tun_dst0[1];
216 key6[2] = ip6_tun_src0[0];
217 key6[3] = ip6_tun_src0[1];
220 if ((!is_ipv6 && cached_tunnel_key4 != key4) ||
221 (is_ipv6 && cached_tunnel_key6[0] != key6[0] &&
222 cached_tunnel_key6[1] != key6[1] &&
223 cached_tunnel_key6[2] != key6[2] &&
224 cached_tunnel_key6[3] != key6[3]))
226 vnet_hw_interface_t * hi;
231 p = hash_get (gm->tunnel_by_key4, key4);
233 p = hash_get_mem (gm->tunnel_by_key6, key6);
236 next0 = GRE_INPUT_NEXT_DROP;
237 b0->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
240 t = pool_elt_at_index (gm->tunnels, p[0]);
241 hi = vnet_get_hw_interface (gm->vnet_main,
243 tunnel_sw_if_index = hi->sw_if_index;
245 cached_tunnel_sw_if_index = tunnel_sw_if_index;
249 tunnel_sw_if_index = cached_tunnel_sw_if_index;
254 next0 = GRE_INPUT_NEXT_DROP;
257 len = vlib_buffer_length_in_chain (vm, b0);
258 vlib_increment_combined_counter (im->combined_sw_if_counters
259 + VNET_INTERFACE_COUNTER_RX,
265 vnet_buffer(b0)->sw_if_index[VLIB_RX] = tunnel_sw_if_index;
268 if (PREDICT_TRUE(next1 == GRE_INPUT_NEXT_IP4_INPUT
269 || next1 == GRE_INPUT_NEXT_IP6_INPUT
270 || next1 == GRE_INPUT_NEXT_ETHERNET_INPUT
271 || next1 == GRE_INPUT_NEXT_MPLS_INPUT))
276 key4 = ((u64)(ip4_tun_dst1) << 32) | (u64)(ip4_tun_src1);
280 key6[0] = ip6_tun_dst1[0];
281 key6[1] = ip6_tun_dst1[1];
282 key6[2] = ip6_tun_src1[0];
283 key6[3] = ip6_tun_src1[1];
286 if ((!is_ipv6 && cached_tunnel_key4 != key4) ||
287 (is_ipv6 && cached_tunnel_key6[0] != key6[0] &&
288 cached_tunnel_key6[1] != key6[1] &&
289 cached_tunnel_key6[2] != key6[2] &&
290 cached_tunnel_key6[3] != key6[3]))
292 vnet_hw_interface_t * hi;
297 p = hash_get (gm->tunnel_by_key4, key4);
299 p = hash_get_mem (gm->tunnel_by_key6, key6);
303 next1 = GRE_INPUT_NEXT_DROP;
304 b1->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
307 t = pool_elt_at_index (gm->tunnels, p[0]);
308 hi = vnet_get_hw_interface (gm->vnet_main,
310 tunnel_sw_if_index = hi->sw_if_index;
312 cached_tunnel_sw_if_index = tunnel_sw_if_index;
316 tunnel_sw_if_index = cached_tunnel_sw_if_index;
321 next1 = GRE_INPUT_NEXT_DROP;
324 len = vlib_buffer_length_in_chain (vm, b1);
325 vlib_increment_combined_counter (im->combined_sw_if_counters
326 + VNET_INTERFACE_COUNTER_RX,
332 vnet_buffer(b1)->sw_if_index[VLIB_RX] = tunnel_sw_if_index;
335 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
337 gre_rx_trace_t *tr = vlib_add_trace (vm, node,
339 tr->tunnel_id = tunnel_sw_if_index;
342 tr->length = ip4_0->length;
343 tr->src.ip4.as_u32 = ip4_0->src_address.as_u32;
344 tr->dst.ip4.as_u32 = ip4_0->dst_address.as_u32;
348 tr->length = ip6_0->payload_length;
349 tr->src.ip6.as_u64[0] = ip6_0->src_address.as_u64[0];
350 tr->src.ip6.as_u64[1] = ip6_0->src_address.as_u64[1];
351 tr->dst.ip6.as_u64[0] = ip6_0->dst_address.as_u64[0];
352 tr->dst.ip6.as_u64[1] = ip6_0->dst_address.as_u64[1];
356 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
358 gre_rx_trace_t *tr = vlib_add_trace (vm, node,
360 tr->tunnel_id = tunnel_sw_if_index;
363 tr->length = ip4_1->length;
364 tr->src.ip4.as_u32 = ip4_1->src_address.as_u32;
365 tr->dst.ip4.as_u32 = ip4_1->dst_address.as_u32;
369 tr->length = ip6_1->payload_length;
370 tr->src.ip6.as_u64[0] = ip6_1->src_address.as_u64[0];
371 tr->src.ip6.as_u64[1] = ip6_1->src_address.as_u64[1];
372 tr->dst.ip6.as_u64[0] = ip6_1->dst_address.as_u64[0];
373 tr->dst.ip6.as_u64[1] = ip6_1->dst_address.as_u64[1];
377 vlib_buffer_advance (b0, sizeof (*h0));
378 vlib_buffer_advance (b1, sizeof (*h1));
380 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
381 to_next, n_left_to_next,
382 bi0, bi1, next0, next1);
385 while (n_left_from > 0 && n_left_to_next > 0)
390 ip4_header_t * ip4_0;
391 ip6_header_t * ip6_0;
395 u32 ip4_tun_src0, ip4_tun_dst0;
396 u32 ip6_tun_src0[4], ip6_tun_dst0[4];
405 b0 = vlib_get_buffer (vm, bi0);
406 ip4_0 = vlib_buffer_get_current (b0);
407 ip6_0 = (void *)ip4_0;
411 ip4_tun_src0 = ip4_0->src_address.as_u32;
412 ip4_tun_dst0 = ip4_0->dst_address.as_u32;
414 vlib_buffer_advance (b0, sizeof (*ip4_0));
418 ip6_tun_src0[0] = ip6_0->src_address.as_u64[0];
419 ip6_tun_src0[1] = ip6_0->src_address.as_u64[1];
420 ip6_tun_dst0[0] = ip6_0->dst_address.as_u64[0];
421 ip6_tun_dst0[1] = ip6_0->dst_address.as_u64[1];
423 vlib_buffer_advance (b0, sizeof (*ip6_0));
426 h0 = vlib_buffer_get_current (b0);
428 i0 = sparse_vec_index (gm->next_by_protocol, h0->protocol);
429 next0 = vec_elt(gm->next_by_protocol, i0);
432 node->errors[i0 == SPARSE_VEC_INVALID_INDEX
433 ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
435 version0 = clib_net_to_host_u16 (h0->flags_and_version);
436 verr0 = version0 & GRE_VERSION_MASK;
437 b0->error = verr0 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION]
439 next0 = verr0 ? GRE_INPUT_NEXT_DROP : next0;
442 /* For IP payload we need to find source interface
443 so we can increase counters and help forward node to
445 /* RPF check for ip4/ip6 input */
446 if (PREDICT_TRUE(next0 == GRE_INPUT_NEXT_IP4_INPUT
447 || next0 == GRE_INPUT_NEXT_IP6_INPUT
448 || next0 == GRE_INPUT_NEXT_ETHERNET_INPUT
449 || next0 == GRE_INPUT_NEXT_MPLS_INPUT))
454 key4 = ((u64)(ip4_tun_dst0) << 32) | (u64)(ip4_tun_src0);
458 key6[0] = ip6_tun_dst0[0];
459 key6[1] = ip6_tun_dst0[1];
460 key6[2] = ip6_tun_src0[0];
461 key6[3] = ip6_tun_src0[1];
464 if ((!is_ipv6 && cached_tunnel_key4 != key4) ||
465 (is_ipv6 && cached_tunnel_key6[0] != key6[0] &&
466 cached_tunnel_key6[1] != key6[1] &&
467 cached_tunnel_key6[2] != key6[2] &&
468 cached_tunnel_key6[3] != key6[3]))
470 vnet_hw_interface_t * hi;
475 p = hash_get (gm->tunnel_by_key4, key4);
477 p = hash_get_mem (gm->tunnel_by_key6, key6);
481 next0 = GRE_INPUT_NEXT_DROP;
482 b0->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
485 t = pool_elt_at_index (gm->tunnels, p[0]);
486 hi = vnet_get_hw_interface (gm->vnet_main,
488 tunnel_sw_if_index = hi->sw_if_index;
490 cached_tunnel_sw_if_index = tunnel_sw_if_index;
494 tunnel_sw_if_index = cached_tunnel_sw_if_index;
499 next0 = GRE_INPUT_NEXT_DROP;
502 len = vlib_buffer_length_in_chain (vm, b0);
503 vlib_increment_combined_counter (im->combined_sw_if_counters
504 + VNET_INTERFACE_COUNTER_RX,
510 vnet_buffer(b0)->sw_if_index[VLIB_RX] = tunnel_sw_if_index;
513 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
515 gre_rx_trace_t *tr = vlib_add_trace (vm, node,
517 tr->tunnel_id = tunnel_sw_if_index;
520 tr->length = ip4_0->length;
521 tr->src.ip4.as_u32 = ip4_0->src_address.as_u32;
522 tr->dst.ip4.as_u32 = ip4_0->dst_address.as_u32;
526 tr->length = ip6_0->payload_length;
527 tr->src.ip6.as_u64[0] = ip6_0->src_address.as_u64[0];
528 tr->src.ip6.as_u64[1] = ip6_0->src_address.as_u64[1];
529 tr->dst.ip6.as_u64[0] = ip6_0->dst_address.as_u64[0];
530 tr->dst.ip6.as_u64[1] = ip6_0->dst_address.as_u64[1];
534 vlib_buffer_advance (b0, sizeof (*h0));
536 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
537 to_next, n_left_to_next,
541 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
543 vlib_node_increment_counter (vm, !is_ipv6 ? gre4_input_node.index : gre6_input_node.index,
544 GRE_ERROR_PKTS_DECAP, from_frame->n_vectors);
545 return from_frame->n_vectors;
549 gre4_input (vlib_main_t * vm,
550 vlib_node_runtime_t * node,
551 vlib_frame_t * from_frame)
553 return gre_input(vm, node, from_frame, /* is_ip6 */ 0);
557 gre6_input (vlib_main_t * vm,
558 vlib_node_runtime_t * node,
559 vlib_frame_t * from_frame)
561 return gre_input(vm, node, from_frame, /* is_ip6 */ 1);
564 static char * gre_error_strings[] = {
565 #define gre_error(n,s) s,
570 VLIB_REGISTER_NODE (gre4_input_node) = {
571 .function = gre4_input,
572 .name = "gre4-input",
573 /* Takes a vector of packets. */
574 .vector_size = sizeof (u32),
576 .n_errors = GRE_N_ERROR,
577 .error_strings = gre_error_strings,
579 .n_next_nodes = GRE_INPUT_N_NEXT,
581 #define _(s,n) [GRE_INPUT_NEXT_##s] = n,
582 foreach_gre_input_next
586 .format_buffer = format_gre_header_with_length,
587 .format_trace = format_gre_rx_trace,
588 .unformat_buffer = unformat_gre_header,
591 VLIB_REGISTER_NODE (gre6_input_node) = {
592 .function = gre6_input,
593 .name = "gre6-input",
594 /* Takes a vector of packets. */
595 .vector_size = sizeof (u32),
597 .runtime_data_bytes = sizeof (gre_input_runtime_t),
599 .n_errors = GRE_N_ERROR,
600 .error_strings = gre_error_strings,
602 .n_next_nodes = GRE_INPUT_N_NEXT,
604 #define _(s,n) [GRE_INPUT_NEXT_##s] = n,
605 foreach_gre_input_next
609 .format_buffer = format_gre_header_with_length,
610 .format_trace = format_gre_rx_trace,
611 .unformat_buffer = unformat_gre_header,
614 VLIB_NODE_FUNCTION_MULTIARCH (gre4_input_node, gre4_input)
615 VLIB_NODE_FUNCTION_MULTIARCH (gre6_input_node, gre6_input)
618 gre_register_input_protocol (vlib_main_t * vm,
619 gre_protocol_t protocol,
622 gre_main_t * em = &gre_main;
623 gre_protocol_info_t * pi;
628 clib_error_t * error = vlib_call_init_function (vm, gre_input_init);
630 clib_error_report (error);
633 pi = gre_get_protocol_info (em, protocol);
634 pi->node_index = node_index;
635 pi->next_index = vlib_node_add_next (vm, gre4_input_node.index, node_index);
636 i = vlib_node_add_next (vm, gre6_input_node.index, node_index);
637 ASSERT(i == pi->next_index);
639 /* Setup gre protocol -> next index sparse vector mapping. */
640 n = sparse_vec_validate (em->next_by_protocol,
641 clib_host_to_net_u16 (protocol));
642 n[0] = pi->next_index;
646 gre_setup_node (vlib_main_t * vm, u32 node_index)
648 vlib_node_t * n = vlib_get_node (vm, node_index);
649 pg_node_t * pn = pg_get_node (node_index);
651 n->format_buffer = format_gre_header_with_length;
652 n->unformat_buffer = unformat_gre_header;
653 pn->unformat_edit = unformat_pg_gre_header;
656 static clib_error_t * gre_input_init (vlib_main_t * vm)
658 gre_main_t * gm = &gre_main;
659 vlib_node_t *ethernet_input, *ip4_input, *ip6_input, *mpls_unicast_input;
662 clib_error_t * error;
663 error = vlib_call_init_function (vm, gre_init);
665 clib_error_report (error);
668 gre_setup_node (vm, gre4_input_node.index);
669 gre_setup_node (vm, gre6_input_node.index);
671 gm->next_by_protocol = sparse_vec_new
672 (/* elt bytes */ sizeof (gm->next_by_protocol[0]),
673 /* bits in index */ BITS (((gre_header_t *) 0)->protocol));
675 /* These could be moved to the supported protocol input node defn's */
676 ethernet_input = vlib_get_node_by_name (vm, (u8 *)"ethernet-input");
677 ASSERT(ethernet_input);
678 ip4_input = vlib_get_node_by_name (vm, (u8 *)"ip4-input");
680 ip6_input = vlib_get_node_by_name (vm, (u8 *)"ip6-input");
682 mpls_unicast_input = vlib_get_node_by_name (vm, (u8 *)"mpls-input");
683 ASSERT(mpls_unicast_input);
685 gre_register_input_protocol (vm, GRE_PROTOCOL_teb,
686 ethernet_input->index);
688 gre_register_input_protocol (vm, GRE_PROTOCOL_ip4,
691 gre_register_input_protocol (vm, GRE_PROTOCOL_ip6,
694 gre_register_input_protocol (vm, GRE_PROTOCOL_mpls_unicast,
695 mpls_unicast_input->index);
697 ip4_register_protocol (IP_PROTOCOL_GRE, gre4_input_node.index);
698 ip6_register_protocol (IP_PROTOCOL_GRE, gre6_input_node.index);
703 VLIB_INIT_FUNCTION (gre_input_init);