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>
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")
34 #define _(s, n) GRE_INPUT_NEXT_##s,
35 foreach_gre_input_next
48 extern u8 *format_gre_rx_trace (u8 *s, va_list *args);
50 #ifndef CLIB_MARCH_VARIANT
52 format_gre_rx_trace (u8 *s, va_list *args)
54 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
55 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
56 gre_rx_trace_t *t = va_arg (*args, gre_rx_trace_t *);
58 s = format (s, "GRE: tunnel %d len %d src %U dst %U", t->tunnel_id,
59 clib_net_to_host_u16 (t->length), format_ip46_address, &t->src,
60 IP46_TYPE_ANY, format_ip46_address, &t->dst, IP46_TYPE_ANY);
63 #endif /* CLIB_MARCH_VARIANT */
67 /* Sparse vector mapping gre protocol in network byte order
69 u16 *next_by_protocol;
70 } gre_input_runtime_t;
73 gre_trace (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b,
74 u32 tun_sw_if_index, const ip6_header_t *ip6,
75 const ip4_header_t *ip4, int is_ipv6)
77 gre_rx_trace_t *tr = vlib_add_trace (vm, node, b, sizeof (*tr));
78 tr->tunnel_id = tun_sw_if_index;
81 tr->length = ip6->payload_length;
82 tr->src.ip6.as_u64[0] = ip6->src_address.as_u64[0];
83 tr->src.ip6.as_u64[1] = ip6->src_address.as_u64[1];
84 tr->dst.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
85 tr->dst.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
89 tr->length = ip4->length;
90 tr->src.as_u64[0] = tr->src.as_u64[1] = 0;
91 tr->dst.as_u64[0] = tr->dst.as_u64[1] = 0;
92 tr->src.ip4.as_u32 = ip4->src_address.as_u32;
93 tr->dst.ip4.as_u32 = ip4->dst_address.as_u32;
98 gre_tunnel_get (const gre_main_t *gm, vlib_node_runtime_t *node,
99 vlib_buffer_t *b, u16 *next, const gre_tunnel_key_t *key,
100 gre_tunnel_key_t *cached_key, u32 *tun_sw_if_index,
101 u32 *cached_tun_sw_if_index, int is_ipv6)
104 p = is_ipv6 ? hash_get_mem (gm->tunnel_by_key6, &key->gtk_v6) :
105 hash_get_mem (gm->tunnel_by_key4, &key->gtk_v4);
106 if (PREDICT_FALSE (!p))
108 *next = GRE_INPUT_NEXT_DROP;
109 b->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL];
110 *tun_sw_if_index = ~0;
114 const gre_tunnel_t *tun;
115 tun = pool_elt_at_index (gm->tunnels, *p);
116 *cached_tun_sw_if_index = *tun_sw_if_index = tun->sw_if_index;
118 cached_key->gtk_v6 = key->gtk_v6;
120 cached_key->gtk_v4 = key->gtk_v4;
125 gre_input (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame,
128 gre_main_t *gm = &gre_main;
129 u32 *from, n_left_from;
130 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
131 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
132 u16 cached_protocol = ~0;
133 u32 cached_next_index = SPARSE_VEC_INVALID_INDEX;
134 u32 cached_tun_sw_if_index = ~0;
135 gre_tunnel_key_t cached_key;
137 from = vlib_frame_vector_args (frame);
138 n_left_from = frame->n_vectors;
139 vlib_get_buffers (vm, from, bufs, n_left_from);
142 clib_memset (&cached_key.gtk_v6, 0xff, sizeof (cached_key.gtk_v6));
144 clib_memset (&cached_key.gtk_v4, 0xff, sizeof (cached_key.gtk_v4));
146 while (n_left_from >= 2)
148 const ip6_header_t *ip6[2];
149 const ip4_header_t *ip4[2];
150 const gre_header_t *gre[2];
156 gre_tunnel_key_t key[2];
158 u32 tun_sw_if_index[2];
160 if (PREDICT_TRUE (n_left_from >= 6))
162 vlib_prefetch_buffer_data (b[2], LOAD);
163 vlib_prefetch_buffer_data (b[3], LOAD);
164 vlib_prefetch_buffer_header (b[4], STORE);
165 vlib_prefetch_buffer_header (b[5], STORE);
170 /* ip6_local hands us the ip header, not the gre header */
171 ip6[0] = vlib_buffer_get_current (b[0]);
172 ip6[1] = vlib_buffer_get_current (b[1]);
173 gre[0] = (void *) (ip6[0] + 1);
174 gre[1] = (void *) (ip6[1] + 1);
175 vlib_buffer_advance (b[0], sizeof (*ip6[0]) + sizeof (*gre[0]));
176 vlib_buffer_advance (b[1], sizeof (*ip6[0]) + sizeof (*gre[0]));
180 /* ip4_local hands us the ip header, not the gre header */
181 ip4[0] = vlib_buffer_get_current (b[0]);
182 ip4[1] = vlib_buffer_get_current (b[1]);
183 gre[0] = (void *) (ip4[0] + 1);
184 gre[1] = (void *) (ip4[1] + 1);
185 vlib_buffer_advance (b[0], sizeof (*ip4[0]) + sizeof (*gre[0]));
186 vlib_buffer_advance (b[1], sizeof (*ip4[0]) + sizeof (*gre[0]));
189 if (PREDICT_TRUE (cached_protocol == gre[0]->protocol))
191 nidx[0] = cached_next_index;
195 cached_next_index = nidx[0] =
196 sparse_vec_index (gm->next_by_protocol, gre[0]->protocol);
197 cached_protocol = gre[0]->protocol;
199 if (PREDICT_TRUE (cached_protocol == gre[1]->protocol))
201 nidx[1] = cached_next_index;
205 cached_next_index = nidx[1] =
206 sparse_vec_index (gm->next_by_protocol, gre[1]->protocol);
207 cached_protocol = gre[1]->protocol;
210 ni[0] = vec_elt (gm->next_by_protocol, nidx[0]);
211 ni[1] = vec_elt (gm->next_by_protocol, nidx[1]);
212 next[0] = ni[0].next_index;
213 next[1] = ni[1].next_index;
214 type[0] = ni[0].tunnel_type;
215 type[1] = ni[1].tunnel_type;
217 b[0]->error = nidx[0] == SPARSE_VEC_INVALID_INDEX ?
218 node->errors[GRE_ERROR_UNKNOWN_PROTOCOL] :
219 node->errors[GRE_ERROR_NONE];
220 b[1]->error = nidx[1] == SPARSE_VEC_INVALID_INDEX ?
221 node->errors[GRE_ERROR_UNKNOWN_PROTOCOL] :
222 node->errors[GRE_ERROR_NONE];
224 version[0] = clib_net_to_host_u16 (gre[0]->flags_and_version);
225 version[1] = clib_net_to_host_u16 (gre[1]->flags_and_version);
226 version[0] &= GRE_VERSION_MASK;
227 version[1] &= GRE_VERSION_MASK;
230 version[0] ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] : b[0]->error;
231 next[0] = version[0] ? GRE_INPUT_NEXT_DROP : next[0];
233 version[1] ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] : b[1]->error;
234 next[1] = version[1] ? GRE_INPUT_NEXT_DROP : next[1];
236 len[0] = vlib_buffer_length_in_chain (vm, b[0]);
237 len[1] = vlib_buffer_length_in_chain (vm, b[1]);
239 /* always search for P2P types in the DP */
242 gre_mk_key6 (&ip6[0]->dst_address, &ip6[0]->src_address,
243 vnet_buffer (b[0])->ip.fib_index, type[0],
244 TUNNEL_MODE_P2P, 0, &key[0].gtk_v6);
245 gre_mk_key6 (&ip6[1]->dst_address, &ip6[1]->src_address,
246 vnet_buffer (b[1])->ip.fib_index, type[1],
247 TUNNEL_MODE_P2P, 0, &key[1].gtk_v6);
248 matched[0] = gre_match_key6 (&cached_key.gtk_v6, &key[0].gtk_v6);
249 matched[1] = gre_match_key6 (&cached_key.gtk_v6, &key[1].gtk_v6);
253 gre_mk_key4 (ip4[0]->dst_address, ip4[0]->src_address,
254 vnet_buffer (b[0])->ip.fib_index, type[0],
255 TUNNEL_MODE_P2P, 0, &key[0].gtk_v4);
256 gre_mk_key4 (ip4[1]->dst_address, ip4[1]->src_address,
257 vnet_buffer (b[1])->ip.fib_index, type[1],
258 TUNNEL_MODE_P2P, 0, &key[1].gtk_v4);
259 matched[0] = gre_match_key4 (&cached_key.gtk_v4, &key[0].gtk_v4);
260 matched[1] = gre_match_key4 (&cached_key.gtk_v4, &key[1].gtk_v4);
263 tun_sw_if_index[0] = cached_tun_sw_if_index;
264 tun_sw_if_index[1] = cached_tun_sw_if_index;
265 if (PREDICT_FALSE (!matched[0]))
266 gre_tunnel_get (gm, node, b[0], &next[0], &key[0], &cached_key,
267 &tun_sw_if_index[0], &cached_tun_sw_if_index, is_ipv6);
268 if (PREDICT_FALSE (!matched[1]))
269 gre_tunnel_get (gm, node, b[1], &next[1], &key[1], &cached_key,
270 &tun_sw_if_index[1], &cached_tun_sw_if_index, is_ipv6);
272 if (PREDICT_TRUE (next[0] > GRE_INPUT_NEXT_DROP))
274 vlib_increment_combined_counter (
275 &gm->vnet_main->interface_main
276 .combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX],
277 vm->thread_index, tun_sw_if_index[0], 1 /* packets */,
279 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = tun_sw_if_index[0];
281 if (PREDICT_TRUE (next[1] > GRE_INPUT_NEXT_DROP))
283 vlib_increment_combined_counter (
284 &gm->vnet_main->interface_main
285 .combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX],
286 vm->thread_index, tun_sw_if_index[1], 1 /* packets */,
288 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = tun_sw_if_index[1];
291 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = (u32) ~0;
292 vnet_buffer (b[1])->sw_if_index[VLIB_TX] = (u32) ~0;
294 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
295 gre_trace (vm, node, b[0], tun_sw_if_index[0], ip6[0], ip4[0],
297 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
298 gre_trace (vm, node, b[1], tun_sw_if_index[1], ip6[1], ip4[1],
306 while (n_left_from >= 1)
308 const ip6_header_t *ip6[1];
309 const ip4_header_t *ip4[1];
310 const gre_header_t *gre[1];
316 gre_tunnel_key_t key[1];
318 u32 tun_sw_if_index[1];
320 if (PREDICT_TRUE (n_left_from >= 3))
322 vlib_prefetch_buffer_data (b[1], LOAD);
323 vlib_prefetch_buffer_header (b[2], STORE);
328 /* ip6_local hands us the ip header, not the gre header */
329 ip6[0] = vlib_buffer_get_current (b[0]);
330 gre[0] = (void *) (ip6[0] + 1);
331 vlib_buffer_advance (b[0], sizeof (*ip6[0]) + sizeof (*gre[0]));
335 /* ip4_local hands us the ip header, not the gre header */
336 ip4[0] = vlib_buffer_get_current (b[0]);
337 gre[0] = (void *) (ip4[0] + 1);
338 vlib_buffer_advance (b[0], sizeof (*ip4[0]) + sizeof (*gre[0]));
341 if (PREDICT_TRUE (cached_protocol == gre[0]->protocol))
343 nidx[0] = cached_next_index;
347 cached_next_index = nidx[0] =
348 sparse_vec_index (gm->next_by_protocol, gre[0]->protocol);
349 cached_protocol = gre[0]->protocol;
352 ni[0] = vec_elt (gm->next_by_protocol, nidx[0]);
353 next[0] = ni[0].next_index;
354 type[0] = ni[0].tunnel_type;
356 b[0]->error = nidx[0] == SPARSE_VEC_INVALID_INDEX ?
357 node->errors[GRE_ERROR_UNKNOWN_PROTOCOL] :
358 node->errors[GRE_ERROR_NONE];
360 version[0] = clib_net_to_host_u16 (gre[0]->flags_and_version);
361 version[0] &= GRE_VERSION_MASK;
364 version[0] ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] : b[0]->error;
365 next[0] = version[0] ? GRE_INPUT_NEXT_DROP : next[0];
367 len[0] = vlib_buffer_length_in_chain (vm, b[0]);
371 gre_mk_key6 (&ip6[0]->dst_address, &ip6[0]->src_address,
372 vnet_buffer (b[0])->ip.fib_index, type[0],
373 TUNNEL_MODE_P2P, 0, &key[0].gtk_v6);
374 matched[0] = gre_match_key6 (&cached_key.gtk_v6, &key[0].gtk_v6);
378 gre_mk_key4 (ip4[0]->dst_address, ip4[0]->src_address,
379 vnet_buffer (b[0])->ip.fib_index, type[0],
380 TUNNEL_MODE_P2P, 0, &key[0].gtk_v4);
381 matched[0] = gre_match_key4 (&cached_key.gtk_v4, &key[0].gtk_v4);
384 tun_sw_if_index[0] = cached_tun_sw_if_index;
385 if (PREDICT_FALSE (!matched[0]))
386 gre_tunnel_get (gm, node, b[0], &next[0], &key[0], &cached_key,
387 &tun_sw_if_index[0], &cached_tun_sw_if_index, is_ipv6);
389 if (PREDICT_TRUE (next[0] > GRE_INPUT_NEXT_DROP))
391 vlib_increment_combined_counter (
392 &gm->vnet_main->interface_main
393 .combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX],
394 vm->thread_index, tun_sw_if_index[0], 1 /* packets */,
396 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = tun_sw_if_index[0];
399 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = (u32) ~0;
401 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
402 gre_trace (vm, node, b[0], tun_sw_if_index[0], ip6[0], ip4[0],
410 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
412 vlib_node_increment_counter (
413 vm, is_ipv6 ? gre6_input_node.index : gre4_input_node.index,
414 GRE_ERROR_PKTS_DECAP, n_left_from);
416 return frame->n_vectors;
419 VLIB_NODE_FN (gre4_input_node)
420 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
422 return gre_input (vm, node, from_frame, /* is_ip6 */ 0);
425 VLIB_NODE_FN (gre6_input_node)
426 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
428 return gre_input (vm, node, from_frame, /* is_ip6 */ 1);
431 static char *gre_error_strings[] = {
432 #define gre_error(n, s) s,
438 VLIB_REGISTER_NODE (gre4_input_node) = {
439 .name = "gre4-input",
440 /* Takes a vector of packets. */
441 .vector_size = sizeof (u32),
443 .n_errors = GRE_N_ERROR,
444 .error_strings = gre_error_strings,
446 .n_next_nodes = GRE_INPUT_N_NEXT,
448 #define _(s, n) [GRE_INPUT_NEXT_##s] = n,
449 foreach_gre_input_next
453 .format_buffer = format_gre_header_with_length,
454 .format_trace = format_gre_rx_trace,
455 .unformat_buffer = unformat_gre_header,
458 VLIB_REGISTER_NODE (gre6_input_node) = {
459 .name = "gre6-input",
460 /* Takes a vector of packets. */
461 .vector_size = sizeof (u32),
463 .runtime_data_bytes = sizeof (gre_input_runtime_t),
465 .n_errors = GRE_N_ERROR,
466 .error_strings = gre_error_strings,
468 .n_next_nodes = GRE_INPUT_N_NEXT,
470 #define _(s, n) [GRE_INPUT_NEXT_##s] = n,
471 foreach_gre_input_next
475 .format_buffer = format_gre_header_with_length,
476 .format_trace = format_gre_rx_trace,
477 .unformat_buffer = unformat_gre_header,
481 #ifndef CLIB_MARCH_VARIANT
483 gre_register_input_protocol (vlib_main_t *vm, gre_protocol_t protocol,
484 u32 node_index, gre_tunnel_type_t tunnel_type)
486 gre_main_t *em = &gre_main;
487 gre_protocol_info_t *pi;
492 clib_error_t *error = vlib_call_init_function (vm, gre_input_init);
494 clib_error_report (error);
497 pi = gre_get_protocol_info (em, protocol);
498 pi->node_index = node_index;
499 pi->tunnel_type = tunnel_type;
500 pi->next_index = vlib_node_add_next (vm, gre4_input_node.index, node_index);
501 i = vlib_node_add_next (vm, gre6_input_node.index, node_index);
502 ASSERT (i == pi->next_index);
504 /* Setup gre protocol -> next index sparse vector mapping. */
505 n = sparse_vec_validate (em->next_by_protocol,
506 clib_host_to_net_u16 (protocol));
507 n->next_index = pi->next_index;
508 n->tunnel_type = tunnel_type;
512 gre_setup_node (vlib_main_t *vm, u32 node_index)
514 vlib_node_t *n = vlib_get_node (vm, node_index);
515 pg_node_t *pn = pg_get_node (node_index);
517 n->format_buffer = format_gre_header_with_length;
518 n->unformat_buffer = unformat_gre_header;
519 pn->unformat_edit = unformat_pg_gre_header;
522 static clib_error_t *
523 gre_input_init (vlib_main_t *vm)
525 gre_main_t *gm = &gre_main;
526 vlib_node_t *ethernet_input, *ip4_input, *ip6_input, *mpls_unicast_input;
530 error = vlib_call_init_function (vm, gre_init);
532 clib_error_report (error);
535 gre_setup_node (vm, gre4_input_node.index);
536 gre_setup_node (vm, gre6_input_node.index);
538 gm->next_by_protocol =
539 sparse_vec_new (/* elt bytes */ sizeof (gm->next_by_protocol[0]),
540 /* bits in index */ BITS (((gre_header_t *) 0)->protocol));
542 /* These could be moved to the supported protocol input node defn's */
543 ethernet_input = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
544 ASSERT (ethernet_input);
545 ip4_input = vlib_get_node_by_name (vm, (u8 *) "ip4-input");
547 ip6_input = vlib_get_node_by_name (vm, (u8 *) "ip6-input");
549 mpls_unicast_input = vlib_get_node_by_name (vm, (u8 *) "mpls-input");
550 ASSERT (mpls_unicast_input);
552 gre_register_input_protocol (vm, GRE_PROTOCOL_teb, ethernet_input->index,
553 GRE_TUNNEL_TYPE_TEB);
555 gre_register_input_protocol (vm, GRE_PROTOCOL_ip4, ip4_input->index,
558 gre_register_input_protocol (vm, GRE_PROTOCOL_ip6, ip6_input->index,
561 gre_register_input_protocol (vm, GRE_PROTOCOL_mpls_unicast,
562 mpls_unicast_input->index, GRE_TUNNEL_TYPE_L3);
567 VLIB_INIT_FUNCTION (gre_input_init);
569 #endif /* CLIB_MARCH_VARIANT */
571 * fd.io coding-style-patch-verification: ON
574 * eval: (c-set-style "gnu")