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 _(IP4_INPUT, "ip4-input") \
27 _(IP6_INPUT, "ip6-input")
30 #define _(s,n) GRE_INPUT_NEXT_##s,
31 foreach_gre_input_next
43 u8 * format_gre_rx_trace (u8 * s, va_list * args)
45 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47 gre_rx_trace_t * t = va_arg (*args, gre_rx_trace_t *);
49 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
50 t->tunnel_id, clib_net_to_host_u16(t->length),
51 format_ip4_address, &t->src.as_u8,
52 format_ip4_address, &t->dst.as_u8);
57 /* Sparse vector mapping gre protocol in network byte order
59 u16 * next_by_protocol;
61 u32 * sparse_index_by_next_index;
62 } gre_input_runtime_t;
65 gre_input (vlib_main_t * vm,
66 vlib_node_runtime_t * node,
67 vlib_frame_t * from_frame)
69 gre_main_t * gm = &gre_main;
70 gre_input_runtime_t * rt = (void *) node->runtime_data;
71 __attribute__((unused)) u32 n_left_from, next_index, i_next, * from, * to_next;
72 u64 cached_tunnel_key = (u64) ~0;
73 u32 cached_tunnel_sw_if_index = 0, tunnel_sw_if_index;
74 u32 cached_tunnel_fib_index = 0, tunnel_fib_index;
76 u32 cpu_index = os_get_cpu_number();
78 from = vlib_frame_vector_args (from_frame);
79 n_left_from = from_frame->n_vectors;
81 next_index = node->cached_next_index;
82 i_next = vec_elt (rt->sparse_index_by_next_index, next_index);
84 while (n_left_from > 0)
88 vlib_get_next_frame (vm, node, next_index,
89 to_next, n_left_to_next);
91 while (n_left_from >= 4 && n_left_to_next >= 2)
94 vlib_buffer_t * b0, * b1;
95 gre_header_t * h0, * h1;
96 u16 version0, version1;
98 u32 i0, i1, next0, next1, protocol0, protocol1;
99 ip4_header_t *ip0, *ip1;
101 /* Prefetch next iteration. */
103 vlib_buffer_t * p2, * p3;
105 p2 = vlib_get_buffer (vm, from[2]);
106 p3 = vlib_get_buffer (vm, from[3]);
108 vlib_prefetch_buffer_header (p2, LOAD);
109 vlib_prefetch_buffer_header (p3, LOAD);
111 CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
112 CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
124 b0 = vlib_get_buffer (vm, bi0);
125 b1 = vlib_get_buffer (vm, bi1);
127 /* ip4_local hands us the ip header, not the gre header */
128 ip0 = vlib_buffer_get_current (b0);
129 ip1 = vlib_buffer_get_current (b1);
131 /* Save src + dst ip4 address, e.g. for mpls-o-gre */
132 vnet_buffer(b0)->gre.src = ip0->src_address.as_u32;
133 vnet_buffer(b0)->gre.dst = ip0->dst_address.as_u32;
134 vnet_buffer(b1)->gre.src = ip1->src_address.as_u32;
135 vnet_buffer(b1)->gre.dst = ip1->dst_address.as_u32;
137 vlib_buffer_advance (b0, sizeof (*ip0));
138 vlib_buffer_advance (b1, sizeof (*ip1));
140 h0 = vlib_buffer_get_current (b0);
141 h1 = vlib_buffer_get_current (b1);
143 /* Index sparse array with network byte order. */
144 protocol0 = h0->protocol;
145 protocol1 = h1->protocol;
146 sparse_vec_index2 (rt->next_by_protocol, protocol0, protocol1,
148 next0 = vec_elt(rt->next_by_protocol, i0);
149 next1 = vec_elt(rt->next_by_protocol, i1);
151 b0->error = node->errors[next0 == SPARSE_VEC_INVALID_INDEX ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
152 b1->error = node->errors[next1 == SPARSE_VEC_INVALID_INDEX ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
154 version0 = clib_net_to_host_u16 (h0->flags_and_version);
155 verr0 = version0 & GRE_VERSION_MASK;
156 version1 = clib_net_to_host_u16 (h1->flags_and_version);
157 verr1 = version1 & GRE_VERSION_MASK;
159 b0->error = verr0 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION]
161 next0 = verr0 ? GRE_INPUT_NEXT_DROP : next0;
162 b1->error = verr1 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION]
164 next1 = verr1 ? GRE_INPUT_NEXT_DROP : next1;
166 /* RPF check for ip4/ip6 input */
167 if (PREDICT_FALSE(next0 == GRE_INPUT_NEXT_IP4_INPUT
168 || next0 == GRE_INPUT_NEXT_IP6_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;
216 if (PREDICT_FALSE(next1 == GRE_INPUT_NEXT_IP4_INPUT
217 || next1 == GRE_INPUT_NEXT_IP6_INPUT))
219 u64 key = ((u64)(vnet_buffer(b1)->gre.dst) << 32) |
220 (u64)(vnet_buffer(b1)->gre.src);
222 if (cached_tunnel_key != key)
224 vnet_hw_interface_t * hi;
228 ip4_main_t * ip4m = &ip4_main;
229 p = hash_get (gm->tunnel_by_key, key);
232 next1 = GRE_INPUT_NEXT_DROP;
233 b1->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
236 t = pool_elt_at_index (gm->tunnels, p[0]);
237 hi = vnet_get_hw_interface (gm->vnet_main,
239 tunnel_sw_if_index = hi->sw_if_index;
240 tunnel_fib_index = vec_elt (ip4m->fib_index_by_sw_if_index,
243 cached_tunnel_sw_if_index = tunnel_sw_if_index;
244 cached_tunnel_fib_index = tunnel_fib_index;
248 tunnel_sw_if_index = cached_tunnel_sw_if_index;
249 tunnel_fib_index = cached_tunnel_fib_index;
252 u32 len = vlib_buffer_length_in_chain (vm, b1);
253 vnet_interface_main_t *im = &gm->vnet_main->interface_main;
254 vlib_increment_combined_counter (im->combined_sw_if_counters
255 + VNET_INTERFACE_COUNTER_RX,
261 vnet_buffer(b1)->sw_if_index[VLIB_TX] = tunnel_fib_index;
264 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
266 gre_rx_trace_t *tr = vlib_add_trace (vm, node,
269 tr->length = ip0->length;
270 tr->src.as_u32 = ip0->src_address.as_u32;
271 tr->dst.as_u32 = ip0->dst_address.as_u32;
274 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
276 gre_rx_trace_t *tr = vlib_add_trace (vm, node,
279 tr->length = ip1->length;
280 tr->src.as_u32 = ip1->src_address.as_u32;
281 tr->dst.as_u32 = ip1->dst_address.as_u32;
284 vlib_buffer_advance (b0, sizeof (*h0));
285 vlib_buffer_advance (b1, sizeof (*h1));
287 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
288 to_next, n_left_to_next,
289 bi0, bi1, next0, next1);
292 while (n_left_from > 0 && n_left_to_next > 0)
309 b0 = vlib_get_buffer (vm, bi0);
310 ip0 = vlib_buffer_get_current (b0);
312 vnet_buffer(b0)->gre.src = ip0->src_address.as_u32;
313 vnet_buffer(b0)->gre.dst = ip0->dst_address.as_u32;
315 vlib_buffer_advance (b0, sizeof (*ip0));
317 h0 = vlib_buffer_get_current (b0);
319 i0 = sparse_vec_index (rt->next_by_protocol, h0->protocol);
320 next0 = vec_elt(rt->next_by_protocol, i0);
323 node->errors[next0 == SPARSE_VEC_INVALID_INDEX
324 ? GRE_ERROR_UNKNOWN_PROTOCOL : GRE_ERROR_NONE];
326 version0 = clib_net_to_host_u16 (h0->flags_and_version);
327 verr0 = version0 & GRE_VERSION_MASK;
328 b0->error = verr0 ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION]
330 next0 = verr0 ? GRE_INPUT_NEXT_DROP : next0;
332 /* For IP payload we need to find source interface
333 so we can increase counters and help forward node to
335 if (PREDICT_FALSE(next0 == GRE_INPUT_NEXT_IP4_INPUT
336 || next0 == GRE_INPUT_NEXT_IP6_INPUT))
338 u64 key = ((u64)(vnet_buffer(b0)->gre.dst) << 32) |
339 (u64)(vnet_buffer(b0)->gre.src);
341 if (cached_tunnel_key != key)
343 vnet_hw_interface_t * hi;
347 ip4_main_t * ip4m = &ip4_main;
348 p = hash_get (gm->tunnel_by_key, key);
351 next0 = GRE_INPUT_NEXT_DROP;
352 b0->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
355 t = pool_elt_at_index (gm->tunnels, p[0]);
356 hi = vnet_get_hw_interface (gm->vnet_main,
358 tunnel_sw_if_index = hi->sw_if_index;
359 tunnel_fib_index = vec_elt (ip4m->fib_index_by_sw_if_index,
362 cached_tunnel_sw_if_index = tunnel_sw_if_index;
363 cached_tunnel_fib_index = tunnel_fib_index;
367 tunnel_sw_if_index = cached_tunnel_sw_if_index;
368 tunnel_fib_index = cached_tunnel_fib_index;
371 u32 len = vlib_buffer_length_in_chain (vm, b0);
372 vnet_interface_main_t *im = &gm->vnet_main->interface_main;
373 vlib_increment_combined_counter (im->combined_sw_if_counters
374 + VNET_INTERFACE_COUNTER_RX,
380 vnet_buffer(b0)->sw_if_index[VLIB_TX] = tunnel_fib_index;
384 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
386 gre_rx_trace_t *tr = vlib_add_trace (vm, node,
389 tr->length = ip0->length;
390 tr->src.as_u32 = ip0->src_address.as_u32;
391 tr->dst.as_u32 = ip0->dst_address.as_u32;
394 vlib_buffer_advance (b0, sizeof (*h0));
396 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
397 to_next, n_left_to_next,
401 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
403 vlib_node_increment_counter (vm, gre_input_node.index,
404 GRE_ERROR_PKTS_DECAP, from_frame->n_vectors);
405 return from_frame->n_vectors;
408 static char * gre_error_strings[] = {
409 #define gre_error(n,s) s,
414 VLIB_REGISTER_NODE (gre_input_node) = {
415 .function = gre_input,
417 /* Takes a vector of packets. */
418 .vector_size = sizeof (u32),
420 .runtime_data_bytes = sizeof (gre_input_runtime_t),
422 .n_errors = GRE_N_ERROR,
423 .error_strings = gre_error_strings,
425 .n_next_nodes = GRE_INPUT_N_NEXT,
427 #define _(s,n) [GRE_INPUT_NEXT_##s] = n,
428 foreach_gre_input_next
432 .format_buffer = format_gre_header_with_length,
433 .format_trace = format_gre_rx_trace,
434 .unformat_buffer = unformat_gre_header,
438 gre_register_input_protocol (vlib_main_t * vm,
439 gre_protocol_t protocol,
442 gre_main_t * em = &gre_main;
443 gre_protocol_info_t * pi;
444 gre_input_runtime_t * rt;
449 clib_error_t * error = vlib_call_init_function (vm, gre_input_init);
451 clib_error_report (error);
454 pi = gre_get_protocol_info (em, protocol);
455 pi->node_index = node_index;
456 pi->next_index = vlib_node_add_next (vm,
457 gre_input_node.index,
460 /* Setup gre protocol -> next index sparse vector mapping. */
461 rt = vlib_node_get_runtime_data (vm, gre_input_node.index);
462 n = sparse_vec_validate (rt->next_by_protocol,
463 clib_host_to_net_u16 (protocol));
464 n[0] = pi->next_index;
466 /* Rebuild next index -> sparse index inverse mapping when sparse vector
468 vec_validate (rt->sparse_index_by_next_index, pi->next_index);
469 for (i = 1; i < vec_len (rt->next_by_protocol); i++)
470 rt->sparse_index_by_next_index[rt->next_by_protocol[i]] = i;
474 gre_setup_node (vlib_main_t * vm, u32 node_index)
476 vlib_node_t * n = vlib_get_node (vm, node_index);
477 pg_node_t * pn = pg_get_node (node_index);
479 n->format_buffer = format_gre_header_with_length;
480 n->unformat_buffer = unformat_gre_header;
481 pn->unformat_edit = unformat_pg_gre_header;
484 static clib_error_t * gre_input_init (vlib_main_t * vm)
486 gre_input_runtime_t * rt;
487 vlib_node_t *ip4_input, *ip6_input, *mpls_unicast_input;
490 clib_error_t * error;
491 error = vlib_call_init_function (vm, gre_init);
493 clib_error_report (error);
496 gre_setup_node (vm, gre_input_node.index);
498 rt = vlib_node_get_runtime_data (vm, gre_input_node.index);
500 rt->next_by_protocol = sparse_vec_new
501 (/* elt bytes */ sizeof (rt->next_by_protocol[0]),
502 /* bits in index */ BITS (((gre_header_t *) 0)->protocol));
504 vec_validate (rt->sparse_index_by_next_index, GRE_INPUT_NEXT_DROP);
505 vec_validate (rt->sparse_index_by_next_index, GRE_INPUT_NEXT_PUNT);
506 rt->sparse_index_by_next_index[GRE_INPUT_NEXT_DROP]
507 = SPARSE_VEC_INVALID_INDEX;
508 rt->sparse_index_by_next_index[GRE_INPUT_NEXT_PUNT]
509 = SPARSE_VEC_INVALID_INDEX;
511 /* These could be moved to the supported protocol input node defn's */
512 ip4_input = vlib_get_node_by_name (vm, (u8 *)"ip4-input");
514 ip6_input = vlib_get_node_by_name (vm, (u8 *)"ip6-input");
516 mpls_unicast_input = vlib_get_node_by_name (vm, (u8 *)"mpls-gre-input");
517 ASSERT(mpls_unicast_input);
519 gre_register_input_protocol (vm, GRE_PROTOCOL_ip4,
522 gre_register_input_protocol (vm, GRE_PROTOCOL_ip6,
525 gre_register_input_protocol (vm, GRE_PROTOCOL_mpls_unicast,
526 mpls_unicast_input->index);
528 ip4_register_protocol (IP_PROTOCOL_GRE, gre_input_node.index);
533 VLIB_INIT_FUNCTION (gre_input_init);