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 <vppinfra/sparse_vec.h>
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")
31 #define _(s,n) GRE_INPUT_NEXT_##s,
32 foreach_gre_input_next
44 u8 * format_gre_rx_trace (u8 * s, va_list * args)
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 *);
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);
58 /* Sparse vector mapping gre protocol in network byte order
60 u16 * next_by_protocol;
61 } gre_input_runtime_t;
64 gre_input (vlib_main_t * vm,
65 vlib_node_runtime_t * node,
66 vlib_frame_t * from_frame)
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;
75 u32 cpu_index = os_get_cpu_number();
77 from = vlib_frame_vector_args (from_frame);
78 n_left_from = from_frame->n_vectors;
80 next_index = node->cached_next_index;
82 while (n_left_from > 0)
86 vlib_get_next_frame (vm, node, next_index,
87 to_next, n_left_to_next);
89 while (n_left_from >= 4 && n_left_to_next >= 2)
92 vlib_buffer_t * b0, * b1;
93 gre_header_t * h0, * h1;
94 u16 version0, version1;
96 u32 i0, i1, next0, next1, protocol0, protocol1;
97 ip4_header_t *ip0, *ip1;
99 /* Prefetch next iteration. */
101 vlib_buffer_t * p2, * p3;
103 p2 = vlib_get_buffer (vm, from[2]);
104 p3 = vlib_get_buffer (vm, from[3]);
106 vlib_prefetch_buffer_header (p2, LOAD);
107 vlib_prefetch_buffer_header (p3, LOAD);
109 CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
110 CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
122 b0 = vlib_get_buffer (vm, bi0);
123 b1 = vlib_get_buffer (vm, bi1);
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);
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;
135 vlib_buffer_advance (b0, sizeof (*ip0));
136 vlib_buffer_advance (b1, sizeof (*ip1));
138 h0 = vlib_buffer_get_current (b0);
139 h1 = vlib_buffer_get_current (b1);
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,
146 next0 = vec_elt(rt->next_by_protocol, i0);
147 next1 = vec_elt(rt->next_by_protocol, i1);
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];
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;
157 b0->error = verr0 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION]
159 next0 = verr0 ? GRE_INPUT_NEXT_DROP : next0;
160 b1->error = verr1 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION]
162 next1 = verr1 ? GRE_INPUT_NEXT_DROP : next1;
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))
170 u64 key = ((u64)(vnet_buffer(b0)->gre.dst) << 32) |
171 (u64)(vnet_buffer(b0)->gre.src);
173 if (cached_tunnel_key != key)
175 vnet_hw_interface_t * hi;
179 ip4_main_t * ip4m = &ip4_main;
180 p = hash_get (gm->tunnel_by_key, key);
183 next0 = GRE_INPUT_NEXT_DROP;
184 b0->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
187 t = pool_elt_at_index (gm->tunnels, p[0]);
188 hi = vnet_get_hw_interface (gm->vnet_main,
190 tunnel_sw_if_index = hi->sw_if_index;
191 tunnel_fib_index = vec_elt (ip4m->fib_index_by_sw_if_index,
194 cached_tunnel_sw_if_index = tunnel_sw_if_index;
195 cached_tunnel_fib_index = tunnel_fib_index;
199 tunnel_sw_if_index = cached_tunnel_sw_if_index;
200 tunnel_fib_index = cached_tunnel_fib_index;
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,
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;
217 if (PREDICT_FALSE(next1 == GRE_INPUT_NEXT_IP4_INPUT
218 || next1 == GRE_INPUT_NEXT_IP6_INPUT
219 || next1 == GRE_INPUT_NEXT_ETHERNET_INPUT))
221 u64 key = ((u64)(vnet_buffer(b1)->gre.dst) << 32) |
222 (u64)(vnet_buffer(b1)->gre.src);
224 if (cached_tunnel_key != key)
226 vnet_hw_interface_t * hi;
230 ip4_main_t * ip4m = &ip4_main;
231 p = hash_get (gm->tunnel_by_key, key);
234 next1 = GRE_INPUT_NEXT_DROP;
235 b1->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
238 t = pool_elt_at_index (gm->tunnels, p[0]);
239 hi = vnet_get_hw_interface (gm->vnet_main,
241 tunnel_sw_if_index = hi->sw_if_index;
242 tunnel_fib_index = vec_elt (ip4m->fib_index_by_sw_if_index,
245 cached_tunnel_sw_if_index = tunnel_sw_if_index;
246 cached_tunnel_fib_index = tunnel_fib_index;
250 tunnel_sw_if_index = cached_tunnel_sw_if_index;
251 tunnel_fib_index = cached_tunnel_fib_index;
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,
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;
267 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
269 gre_rx_trace_t *tr = vlib_add_trace (vm, node,
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;
277 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
279 gre_rx_trace_t *tr = vlib_add_trace (vm, node,
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;
287 vlib_buffer_advance (b0, sizeof (*h0));
288 vlib_buffer_advance (b1, sizeof (*h1));
290 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
291 to_next, n_left_to_next,
292 bi0, bi1, next0, next1);
295 while (n_left_from > 0 && n_left_to_next > 0)
312 b0 = vlib_get_buffer (vm, bi0);
313 ip0 = vlib_buffer_get_current (b0);
315 vnet_buffer(b0)->gre.src = ip0->src_address.as_u32;
316 vnet_buffer(b0)->gre.dst = ip0->dst_address.as_u32;
318 vlib_buffer_advance (b0, sizeof (*ip0));
320 h0 = vlib_buffer_get_current (b0);
322 i0 = sparse_vec_index (rt->next_by_protocol, h0->protocol);
323 next0 = vec_elt(rt->next_by_protocol, i0);
326 node->errors[i0 == SPARSE_VEC_INVALID_INDEX
327 ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
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]
333 next0 = verr0 ? GRE_INPUT_NEXT_DROP : next0;
336 /* For IP payload we need to find source interface
337 so we can increase counters and help forward node to
339 if (PREDICT_FALSE(next0 == GRE_INPUT_NEXT_IP4_INPUT
340 || next0 == GRE_INPUT_NEXT_IP6_INPUT
341 || next0 == GRE_INPUT_NEXT_ETHERNET_INPUT))
343 u64 key = ((u64)(vnet_buffer(b0)->gre.dst) << 32) |
344 (u64)(vnet_buffer(b0)->gre.src);
346 if (cached_tunnel_key != key)
348 vnet_hw_interface_t * hi;
352 ip4_main_t * ip4m = &ip4_main;
353 p = hash_get (gm->tunnel_by_key, key);
356 next0 = GRE_INPUT_NEXT_DROP;
357 b0->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
360 t = pool_elt_at_index (gm->tunnels, p[0]);
361 hi = vnet_get_hw_interface (gm->vnet_main,
363 tunnel_sw_if_index = hi->sw_if_index;
364 tunnel_fib_index = vec_elt (ip4m->fib_index_by_sw_if_index,
367 cached_tunnel_sw_if_index = tunnel_sw_if_index;
368 cached_tunnel_fib_index = tunnel_fib_index;
372 tunnel_sw_if_index = cached_tunnel_sw_if_index;
373 tunnel_fib_index = cached_tunnel_fib_index;
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,
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;
390 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
392 gre_rx_trace_t *tr = vlib_add_trace (vm, node,
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;
400 vlib_buffer_advance (b0, sizeof (*h0));
402 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
403 to_next, n_left_to_next,
407 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
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;
414 static char * gre_error_strings[] = {
415 #define gre_error(n,s) s,
420 VLIB_REGISTER_NODE (gre_input_node) = {
421 .function = gre_input,
423 /* Takes a vector of packets. */
424 .vector_size = sizeof (u32),
426 .runtime_data_bytes = sizeof (gre_input_runtime_t),
428 .n_errors = GRE_N_ERROR,
429 .error_strings = gre_error_strings,
431 .n_next_nodes = GRE_INPUT_N_NEXT,
433 #define _(s,n) [GRE_INPUT_NEXT_##s] = n,
434 foreach_gre_input_next
438 .format_buffer = format_gre_header_with_length,
439 .format_trace = format_gre_rx_trace,
440 .unformat_buffer = unformat_gre_header,
443 VLIB_NODE_FUNCTION_MULTIARCH (gre_input_node, gre_input)
446 gre_register_input_protocol (vlib_main_t * vm,
447 gre_protocol_t protocol,
450 gre_main_t * em = &gre_main;
451 gre_protocol_info_t * pi;
452 gre_input_runtime_t * rt;
456 clib_error_t * error = vlib_call_init_function (vm, gre_input_init);
458 clib_error_report (error);
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,
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;
475 gre_setup_node (vlib_main_t * vm, u32 node_index)
477 vlib_node_t * n = vlib_get_node (vm, node_index);
478 pg_node_t * pn = pg_get_node (node_index);
480 n->format_buffer = format_gre_header_with_length;
481 n->unformat_buffer = unformat_gre_header;
482 pn->unformat_edit = unformat_pg_gre_header;
485 static clib_error_t * gre_input_init (vlib_main_t * vm)
487 gre_input_runtime_t * rt;
488 vlib_node_t *ethernet_input, *ip4_input, *ip6_input, *mpls_unicast_input;
491 clib_error_t * error;
492 error = vlib_call_init_function (vm, gre_init);
494 clib_error_report (error);
497 gre_setup_node (vm, gre_input_node.index);
499 rt = vlib_node_get_runtime_data (vm, gre_input_node.index);
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));
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");
510 ip6_input = vlib_get_node_by_name (vm, (u8 *)"ip6-input");
512 mpls_unicast_input = vlib_get_node_by_name (vm, (u8 *)"mpls-gre-input");
513 ASSERT(mpls_unicast_input);
515 gre_register_input_protocol (vm, GRE_PROTOCOL_teb,
516 ethernet_input->index);
518 gre_register_input_protocol (vm, GRE_PROTOCOL_ip4,
521 gre_register_input_protocol (vm, GRE_PROTOCOL_ip6,
524 gre_register_input_protocol (vm, GRE_PROTOCOL_mpls_unicast,
525 mpls_unicast_input->index);
527 ip4_register_protocol (IP_PROTOCOL_GRE, gre_input_node.index);
532 VLIB_INIT_FUNCTION (gre_input_init);