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 <vnet/vnet.h>
19 #include <vnet/gre/gre.h>
20 #include <vnet/adj/adj_midchain.h>
22 #ifndef CLIB_MARCH_VARIANT
29 ip4_and_gre_header_t ip4_and_gre;
32 } ip4_and_gre_union_t;
38 ip6_and_gre_header_t ip6_and_gre;
41 } ip6_and_gre_union_t;
42 #endif /* CLIB_MARCH_VARIANT */
45 /* Packet trace structure */
48 /* Tunnel-id / index in tunnel vector */
54 /* tunnel ip addresses */
60 format_gre_tx_trace (u8 * s, va_list * args)
62 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
63 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
64 gre_tx_trace_t *t = va_arg (*args, gre_tx_trace_t *);
66 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
67 t->tunnel_id, t->length,
68 format_ip46_address, &t->src, IP46_TYPE_ANY,
69 format_ip46_address, &t->dst, IP46_TYPE_ANY);
73 #ifndef CLIB_MARCH_VARIANT
75 format_gre_protocol (u8 * s, va_list * args)
77 gre_protocol_t p = va_arg (*args, u32);
78 gre_main_t *gm = &gre_main;
79 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
82 s = format (s, "%s", pi->name);
84 s = format (s, "0x%04x", p);
90 format_gre_header_with_length (u8 * s, va_list * args)
92 gre_main_t *gm = &gre_main;
93 gre_header_t *h = va_arg (*args, gre_header_t *);
94 u32 max_header_bytes = va_arg (*args, u32);
95 gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
96 u32 indent, header_bytes;
98 header_bytes = sizeof (h[0]);
99 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
100 return format (s, "gre header truncated");
102 indent = format_get_indent (s);
104 s = format (s, "GRE %U", format_gre_protocol, p);
106 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
108 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
109 vlib_node_t *node = vlib_get_node (gm->vlib_main, pi->node_index);
110 if (node->format_buffer)
111 s = format (s, "\n%U%U",
112 format_white_space, indent,
113 node->format_buffer, (void *) (h + 1),
114 max_header_bytes - header_bytes);
121 format_gre_header (u8 * s, va_list * args)
123 gre_header_t *h = va_arg (*args, gre_header_t *);
124 return format (s, "%U", format_gre_header_with_length, h, 0);
127 /* Returns gre protocol as an int in host byte order. */
129 unformat_gre_protocol_host_byte_order (unformat_input_t * input,
132 u16 *result = va_arg (*args, u16 *);
133 gre_main_t *gm = &gre_main;
137 if (unformat_user (input, unformat_vlib_number_by_name,
138 gm->protocol_info_by_name, &i))
140 gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i);
141 *result = pi->protocol;
149 unformat_gre_protocol_net_byte_order (unformat_input_t * input,
152 u16 *result = va_arg (*args, u16 *);
153 if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result))
155 *result = clib_host_to_net_u16 ((u16) * result);
160 unformat_gre_header (unformat_input_t * input, va_list * args)
162 u8 **result = va_arg (*args, u8 **);
163 gre_header_t _h, *h = &_h;
166 if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p))
169 h->protocol = clib_host_to_net_u16 (p);
171 /* Add header to result. */
174 u32 n_bytes = sizeof (h[0]);
176 vec_add2 (*result, p, n_bytes);
177 clib_memcpy (p, h, n_bytes);
184 gre_proto_from_vnet_link (vnet_link_t link)
189 return (GRE_PROTOCOL_ip4);
191 return (GRE_PROTOCOL_ip6);
193 return (GRE_PROTOCOL_mpls_unicast);
194 case VNET_LINK_ETHERNET:
195 return (GRE_PROTOCOL_teb);
197 return (GRE_PROTOCOL_arp);
203 return (GRE_PROTOCOL_ip4);
207 gre_build_rewrite (vnet_main_t * vnm,
209 vnet_link_t link_type, const void *dst_address)
211 gre_main_t *gm = &gre_main;
212 ip4_and_gre_header_t *h4;
213 ip6_and_gre_header_t *h6;
220 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
223 /* not one of ours */
226 t = pool_elt_at_index (gm->tunnels, ti);
228 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
232 vec_validate (rewrite, sizeof (*h4) - 1);
233 h4 = (ip4_and_gre_header_t *) rewrite;
235 h4->ip4.ip_version_and_header_length = 0x45;
237 h4->ip4.protocol = IP_PROTOCOL_GRE;
238 /* fixup ip4 header length and checksum after-the-fact */
239 h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32;
240 h4->ip4.dst_address.as_u32 = t->tunnel_dst.fp_addr.ip4.as_u32;
241 h4->ip4.checksum = ip4_header_checksum (&h4->ip4);
245 vec_validate (rewrite, sizeof (*h6) - 1);
246 h6 = (ip6_and_gre_header_t *) rewrite;
248 h6->ip6.ip_version_traffic_class_and_flow_label =
249 clib_host_to_net_u32 (6 << 28);
250 h6->ip6.hop_limit = 255;
251 h6->ip6.protocol = IP_PROTOCOL_GRE;
252 /* fixup ip6 header length and checksum after-the-fact */
253 h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
254 h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
255 h6->ip6.dst_address.as_u64[0] = t->tunnel_dst.fp_addr.ip6.as_u64[0];
256 h6->ip6.dst_address.as_u64[1] = t->tunnel_dst.fp_addr.ip6.as_u64[1];
259 if (PREDICT_FALSE (t->type == GRE_TUNNEL_TYPE_ERSPAN))
261 gre->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_erspan);
262 gre->flags_and_version = clib_host_to_net_u16 (GRE_FLAGS_SEQUENCE);
266 clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
271 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
274 gre4_fixup (vlib_main_t * vm,
275 ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
279 ip0 = vlib_buffer_get_current (b0);
281 /* Fixup the checksum and len fields in the GRE tunnel encap
282 * that was applied at the midchain node */
283 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
284 ip0->checksum = ip4_header_checksum (ip0);
288 gre6_fixup (vlib_main_t * vm,
289 ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
293 ip0 = vlib_buffer_get_current (b0);
295 /* Fixup the payload length field in the GRE tunnel encap that was applied
296 * at the midchain node */
297 ip0->payload_length =
298 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
303 gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
305 gre_main_t *gm = &gre_main;
311 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
312 t = pool_elt_at_index (gm->tunnels, ti);
313 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
314 af = ADJ_FLAG_MIDCHAIN_IP_STACK;
316 if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
317 af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
319 adj_nbr_midchain_update_rewrite
320 (ai, !is_ipv6 ? gre4_fixup : gre6_fixup, NULL, af,
321 gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), NULL));
323 gre_tunnel_stack (ai);
325 #endif /* CLIB_MARCH_VARIANT */
330 GRE_ENCAP_NEXT_L2_MIDCHAIN,
335 * @brief TX function. Only called for L2 payload including TEB or ERSPAN.
336 * L3 traffic uses the adj-midchains.
338 VLIB_NODE_FN (gre_encap_node) (vlib_main_t * vm,
339 vlib_node_runtime_t * node,
340 vlib_frame_t * frame)
342 gre_main_t *gm = &gre_main;
343 vnet_main_t *vnm = gm->vnet_main;
345 u32 *from, *to_next, n_left_from, n_left_to_next;
346 u32 sw_if_index0 = ~0;
347 u32 sw_if_index1 = ~0;
348 adj_index_t adj_index0 = ADJ_INDEX_INVALID;
349 adj_index_t adj_index1 = ADJ_INDEX_INVALID;
350 gre_tunnel_t *gt0 = NULL;
351 gre_tunnel_t *gt1 = NULL;
353 /* Vector of buffer / pkt indices we're supposed to process */
354 from = vlib_frame_vector_args (frame);
356 /* Number of buffers / pkts */
357 n_left_from = frame->n_vectors;
359 /* Speculatively send the first buffer to the last disposition we used */
360 next_index = GRE_ENCAP_NEXT_L2_MIDCHAIN;
362 while (n_left_from > 0)
364 /* set up to enqueue to our disposition with index = next_index */
365 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
367 while (n_left_from >= 4 && n_left_to_next >= 2)
371 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
372 vlib_buffer_t *b1 = vlib_get_buffer (vm, bi1);
381 if (sw_if_index0 != vnet_buffer (b0)->sw_if_index[VLIB_TX])
383 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
384 vnet_hw_interface_t *hi0 =
385 vnet_get_sup_hw_interface (vnm, sw_if_index0);
386 gt0 = &gm->tunnels[hi0->dev_instance];
387 adj_index0 = gt0->l2_adj_index;
390 if (sw_if_index1 != vnet_buffer (b1)->sw_if_index[VLIB_TX])
392 if (sw_if_index0 == vnet_buffer (b1)->sw_if_index[VLIB_TX])
394 sw_if_index1 = sw_if_index0;
396 adj_index1 = adj_index0;
400 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
401 vnet_hw_interface_t *hi1 =
402 vnet_get_sup_hw_interface (vnm, sw_if_index1);
403 gt1 = &gm->tunnels[hi1->dev_instance];
404 adj_index1 = gt1->l2_adj_index;
408 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = adj_index0;
409 vnet_buffer (b1)->ip.adj_index[VLIB_TX] = adj_index1;
411 if (PREDICT_FALSE (gt0->type == GRE_TUNNEL_TYPE_ERSPAN))
413 /* Encap GRE seq# and ERSPAN type II header */
414 vlib_buffer_advance (b0, -sizeof (erspan_t2_t));
415 erspan_t2_t *h0 = vlib_buffer_get_current (b0);
416 u32 seq_num = clib_atomic_fetch_add (>0->gre_sn->seq_num, 1);
417 u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
418 h0->seq_num = clib_host_to_net_u32 (seq_num);
420 h0->t2.cos_en_t_session |=
421 clib_host_to_net_u16 (gt0->session_id);
423 if (PREDICT_FALSE (gt1->type == GRE_TUNNEL_TYPE_ERSPAN))
425 /* Encap GRE seq# and ERSPAN type II header */
426 vlib_buffer_advance (b1, -sizeof (erspan_t2_t));
427 erspan_t2_t *h1 = vlib_buffer_get_current (b1);
428 u32 seq_num = clib_atomic_fetch_add (>1->gre_sn->seq_num, 1);
429 u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
430 h1->seq_num = clib_host_to_net_u32 (seq_num);
432 h1->t2.cos_en_t_session |=
433 clib_host_to_net_u16 (gt1->session_id);
436 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
438 gre_tx_trace_t *tr0 = vlib_add_trace (vm, node,
440 tr0->tunnel_id = gt0 - gm->tunnels;
441 tr0->src = gt0->tunnel_src;
442 tr0->dst = gt0->tunnel_dst.fp_addr;
443 tr0->length = vlib_buffer_length_in_chain (vm, b0);
445 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
447 gre_tx_trace_t *tr1 = vlib_add_trace (vm, node,
449 tr1->tunnel_id = gt1 - gm->tunnels;
450 tr1->src = gt1->tunnel_src;
451 tr1->dst = gt1->tunnel_dst.fp_addr;
452 tr1->length = vlib_buffer_length_in_chain (vm, b1);
456 while (n_left_from > 0 && n_left_to_next > 0)
459 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
467 if (sw_if_index0 != vnet_buffer (b0)->sw_if_index[VLIB_TX])
469 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
470 vnet_hw_interface_t *hi0 =
471 vnet_get_sup_hw_interface (vnm, sw_if_index0);
472 gt0 = &gm->tunnels[hi0->dev_instance];
473 adj_index0 = gt0->l2_adj_index;
476 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = adj_index0;
478 if (PREDICT_FALSE (gt0->type == GRE_TUNNEL_TYPE_ERSPAN))
480 /* Encap GRE seq# and ERSPAN type II header */
481 vlib_buffer_advance (b0, -sizeof (erspan_t2_t));
482 erspan_t2_t *h0 = vlib_buffer_get_current (b0);
483 u32 seq_num = clib_atomic_fetch_add (>0->gre_sn->seq_num, 1);
484 u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
485 h0->seq_num = clib_host_to_net_u32 (seq_num);
487 h0->t2.cos_en_t_session |=
488 clib_host_to_net_u16 (gt0->session_id);
491 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
493 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
495 tr->tunnel_id = gt0 - gm->tunnels;
496 tr->src = gt0->tunnel_src;
497 tr->dst = gt0->tunnel_dst.fp_addr;
498 tr->length = vlib_buffer_length_in_chain (vm, b0);
502 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
505 vlib_node_increment_counter (vm, node->node_index,
506 GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
508 return frame->n_vectors;
511 static char *gre_error_strings[] = {
512 #define gre_error(n,s) s,
518 VLIB_REGISTER_NODE (gre_encap_node) =
521 .vector_size = sizeof (u32),
522 .format_trace = format_gre_tx_trace,
523 .type = VLIB_NODE_TYPE_INTERNAL,
524 .n_errors = GRE_N_ERROR,
525 .error_strings = gre_error_strings,
526 .n_next_nodes = GRE_ENCAP_N_NEXT,
528 [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
534 format_gre_tunnel_name (u8 * s, va_list * args)
536 u32 dev_instance = va_arg (*args, u32);
537 gre_main_t *gm = &gre_main;
540 if (dev_instance >= vec_len (gm->tunnels))
541 return format (s, "<improperly-referenced>");
543 t = pool_elt_at_index (gm->tunnels, dev_instance);
544 return format (s, "gre%d", t->user_instance);
548 format_gre_device (u8 * s, va_list * args)
550 u32 dev_instance = va_arg (*args, u32);
551 CLIB_UNUSED (int verbose) = va_arg (*args, int);
553 s = format (s, "GRE tunnel: id %d\n", dev_instance);
558 VNET_DEVICE_CLASS (gre_device_class) = {
559 .name = "GRE tunnel device",
560 .format_device_name = format_gre_tunnel_name,
561 .format_device = format_gre_device,
562 .format_tx_trace = format_gre_tx_trace,
563 .admin_up_down_function = gre_interface_admin_up_down,
569 #ifndef CLIB_MARCH_VARIANT
570 VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
572 .format_header = format_gre_header_with_length,
573 .unformat_header = unformat_gre_header,
574 .build_rewrite = gre_build_rewrite,
575 .update_adjacency = gre_update_adj,
576 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
581 add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name)
583 gre_protocol_info_t *pi;
586 vec_add2 (gm->protocol_infos, pi, 1);
587 i = pi - gm->protocol_infos;
589 pi->name = protocol_name;
590 pi->protocol = protocol;
591 pi->next_index = pi->node_index = ~0;
593 hash_set (gm->protocol_info_by_protocol, protocol, i);
594 hash_set_mem (gm->protocol_info_by_name, pi->name, i);
597 static clib_error_t *
598 gre_init (vlib_main_t * vm)
600 gre_main_t *gm = &gre_main;
602 ip_main_t *im = &ip_main;
603 ip_protocol_info_t *pi;
605 clib_memset (gm, 0, sizeof (gm[0]));
607 gm->vnet_main = vnet_get_main ();
609 if ((error = vlib_call_init_function (vm, ip_main_init)))
612 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
615 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
618 /* Set up the ip packet generator */
619 pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
620 pi->format_header = format_gre_header;
621 pi->unformat_pg_edit = unformat_pg_gre_header;
623 gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
624 gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
626 hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword));
628 hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword));
630 hash_create_mem (0, sizeof (gre_sn_key_t), sizeof (uword));
632 #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
635 return vlib_call_init_function (vm, gre_input_init);
638 VLIB_INIT_FUNCTION (gre_init);
640 #endif /* CLIB_MARCH_VARIANT */
643 * fd.io coding-style-patch-verification: ON
646 * eval: (c-set-style "gnu")