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 extern gre_main_t gre_main;
24 #ifndef CLIB_MARCH_VARIANT
31 ip4_and_gre_header_t ip4_and_gre;
34 } ip4_and_gre_union_t;
40 ip6_and_gre_header_t ip6_and_gre;
43 } ip6_and_gre_union_t;
44 #endif /* CLIB_MARCH_VARIANT */
47 /* Packet trace structure */
50 /* Tunnel-id / index in tunnel vector */
56 /* tunnel ip addresses */
61 extern u8 *format_gre_tx_trace (u8 * s, va_list * args);
63 #ifndef CLIB_MARCH_VARIANT
65 format_gre_tx_trace (u8 * s, va_list * args)
67 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
68 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
69 gre_tx_trace_t *t = va_arg (*args, gre_tx_trace_t *);
71 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
72 t->tunnel_id, t->length,
73 format_ip46_address, &t->src, IP46_TYPE_ANY,
74 format_ip46_address, &t->dst, IP46_TYPE_ANY);
79 format_gre_protocol (u8 * s, va_list * args)
81 gre_protocol_t p = va_arg (*args, u32);
82 gre_main_t *gm = &gre_main;
83 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
86 s = format (s, "%s", pi->name);
88 s = format (s, "0x%04x", p);
94 format_gre_header_with_length (u8 * s, va_list * args)
96 gre_main_t *gm = &gre_main;
97 gre_header_t *h = va_arg (*args, gre_header_t *);
98 u32 max_header_bytes = va_arg (*args, u32);
99 gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
100 u32 indent, header_bytes;
102 header_bytes = sizeof (h[0]);
103 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
104 return format (s, "gre header truncated");
106 indent = format_get_indent (s);
108 s = format (s, "GRE %U", format_gre_protocol, p);
110 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
112 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
113 vlib_node_t *node = vlib_get_node (gm->vlib_main, pi->node_index);
114 if (node->format_buffer)
115 s = format (s, "\n%U%U",
116 format_white_space, indent,
117 node->format_buffer, (void *) (h + 1),
118 max_header_bytes - header_bytes);
125 format_gre_header (u8 * s, va_list * args)
127 gre_header_t *h = va_arg (*args, gre_header_t *);
128 return format (s, "%U", format_gre_header_with_length, h, 0);
131 /* Returns gre protocol as an int in host byte order. */
133 unformat_gre_protocol_host_byte_order (unformat_input_t * input,
136 u16 *result = va_arg (*args, u16 *);
137 gre_main_t *gm = &gre_main;
141 if (unformat_user (input, unformat_vlib_number_by_name,
142 gm->protocol_info_by_name, &i))
144 gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i);
145 *result = pi->protocol;
153 unformat_gre_protocol_net_byte_order (unformat_input_t * input,
156 u16 *result = va_arg (*args, u16 *);
157 if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result))
159 *result = clib_host_to_net_u16 ((u16) * result);
164 unformat_gre_header (unformat_input_t * input, va_list * args)
166 u8 **result = va_arg (*args, u8 **);
167 gre_header_t _h, *h = &_h;
170 if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p))
173 h->protocol = clib_host_to_net_u16 (p);
175 /* Add header to result. */
178 u32 n_bytes = sizeof (h[0]);
180 vec_add2 (*result, p, n_bytes);
181 clib_memcpy (p, h, n_bytes);
188 gre_proto_from_vnet_link (vnet_link_t link)
193 return (GRE_PROTOCOL_ip4);
195 return (GRE_PROTOCOL_ip6);
197 return (GRE_PROTOCOL_mpls_unicast);
198 case VNET_LINK_ETHERNET:
199 return (GRE_PROTOCOL_teb);
201 return (GRE_PROTOCOL_arp);
207 return (GRE_PROTOCOL_ip4);
211 gre_build_rewrite (vnet_main_t * vnm,
213 vnet_link_t link_type, const void *dst_address)
215 gre_main_t *gm = &gre_main;
216 ip4_and_gre_header_t *h4;
217 ip6_and_gre_header_t *h6;
224 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
227 /* not one of ours */
230 t = pool_elt_at_index (gm->tunnels, ti);
232 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
236 vec_validate (rewrite, sizeof (*h4) - 1);
237 h4 = (ip4_and_gre_header_t *) rewrite;
239 h4->ip4.ip_version_and_header_length = 0x45;
241 h4->ip4.protocol = IP_PROTOCOL_GRE;
242 /* fixup ip4 header length and checksum after-the-fact */
243 h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32;
244 h4->ip4.dst_address.as_u32 = t->tunnel_dst.fp_addr.ip4.as_u32;
245 h4->ip4.checksum = ip4_header_checksum (&h4->ip4);
249 vec_validate (rewrite, sizeof (*h6) - 1);
250 h6 = (ip6_and_gre_header_t *) rewrite;
252 h6->ip6.ip_version_traffic_class_and_flow_label =
253 clib_host_to_net_u32 (6 << 28);
254 h6->ip6.hop_limit = 255;
255 h6->ip6.protocol = IP_PROTOCOL_GRE;
256 /* fixup ip6 header length and checksum after-the-fact */
257 h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
258 h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
259 h6->ip6.dst_address.as_u64[0] = t->tunnel_dst.fp_addr.ip6.as_u64[0];
260 h6->ip6.dst_address.as_u64[1] = t->tunnel_dst.fp_addr.ip6.as_u64[1];
263 if (PREDICT_FALSE (t->type == GRE_TUNNEL_TYPE_ERSPAN))
265 gre->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_erspan);
266 gre->flags_and_version = clib_host_to_net_u16 (GRE_FLAGS_SEQUENCE);
270 clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
275 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
278 gre4_fixup (vlib_main_t * vm,
279 ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
283 ip0 = vlib_buffer_get_current (b0);
285 /* Fixup the checksum and len fields in the GRE tunnel encap
286 * that was applied at the midchain node */
287 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
288 ip0->checksum = ip4_header_checksum (ip0);
292 gre6_fixup (vlib_main_t * vm,
293 ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
297 ip0 = vlib_buffer_get_current (b0);
299 /* Fixup the payload length field in the GRE tunnel encap that was applied
300 * at the midchain node */
301 ip0->payload_length =
302 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
307 gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
309 gre_main_t *gm = &gre_main;
315 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
316 t = pool_elt_at_index (gm->tunnels, ti);
317 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
318 af = ADJ_FLAG_MIDCHAIN_IP_STACK;
320 if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
321 af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
323 adj_nbr_midchain_update_rewrite
324 (ai, !is_ipv6 ? gre4_fixup : gre6_fixup, NULL, af,
325 gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), NULL));
327 gre_tunnel_stack (ai);
329 #endif /* CLIB_MARCH_VARIANT */
333 GRE_ENCAP_NEXT_L2_MIDCHAIN,
338 * @brief TX function. Only called for L2 payload including TEB or ERSPAN.
339 * L3 traffic uses the adj-midchains.
341 VLIB_NODE_FN (gre_encap_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
342 vlib_frame_t * frame)
344 gre_main_t *gm = &gre_main;
345 u32 *from, n_left_from;
346 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
347 u32 sw_if_index[2] = { ~0, ~0 };
348 const gre_tunnel_t *gt[2] = { 0 };
349 adj_index_t adj_index[2] = { ADJ_INDEX_INVALID, ADJ_INDEX_INVALID };
351 from = vlib_frame_vector_args (frame);
352 n_left_from = frame->n_vectors;
353 vlib_get_buffers (vm, from, bufs, n_left_from);
355 while (n_left_from >= 2)
359 (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
361 const vnet_hw_interface_t *hi;
362 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
363 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
364 gt[0] = &gm->tunnels[hi->dev_instance];
365 adj_index[0] = gt[0]->l2_adj_index;
368 (sw_if_index[1] != vnet_buffer (b[1])->sw_if_index[VLIB_TX]))
370 const vnet_hw_interface_t *hi;
371 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
372 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[1]);
373 gt[1] = &gm->tunnels[hi->dev_instance];
374 adj_index[1] = gt[1]->l2_adj_index;
377 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
378 vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = adj_index[1];
380 if (PREDICT_FALSE (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN))
382 /* Encap GRE seq# and ERSPAN type II header */
386 vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
387 h0 = vlib_buffer_get_current (b[0]);
388 seq_num = clib_atomic_fetch_add (>[0]->gre_sn->seq_num, 1);
389 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
390 h0->seq_num = clib_host_to_net_u32 (seq_num);
392 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
394 if (PREDICT_FALSE (gt[1]->type == GRE_TUNNEL_TYPE_ERSPAN))
396 /* Encap GRE seq# and ERSPAN type II header */
400 vlib_buffer_advance (b[1], -sizeof (erspan_t2_t));
401 h0 = vlib_buffer_get_current (b[1]);
402 seq_num = clib_atomic_fetch_add (>[1]->gre_sn->seq_num, 1);
403 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
404 h0->seq_num = clib_host_to_net_u32 (seq_num);
406 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[1]->session_id);
409 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
411 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
413 tr->tunnel_id = gt[0] - gm->tunnels;
414 tr->src = gt[0]->tunnel_src;
415 tr->dst = gt[0]->tunnel_dst.fp_addr;
416 tr->length = vlib_buffer_length_in_chain (vm, b[0]);
418 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
420 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
422 tr->tunnel_id = gt[1] - gm->tunnels;
423 tr->src = gt[1]->tunnel_src;
424 tr->dst = gt[1]->tunnel_dst.fp_addr;
425 tr->length = vlib_buffer_length_in_chain (vm, b[1]);
432 while (n_left_from >= 1)
436 (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
438 const vnet_hw_interface_t *hi;
439 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
440 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
441 gt[0] = &gm->tunnels[hi->dev_instance];
442 adj_index[0] = gt[0]->l2_adj_index;
445 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
447 if (PREDICT_FALSE (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN))
449 /* Encap GRE seq# and ERSPAN type II header */
453 vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
454 h0 = vlib_buffer_get_current (b[0]);
455 seq_num = clib_atomic_fetch_add (>[0]->gre_sn->seq_num, 1);
456 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
457 h0->seq_num = clib_host_to_net_u32 (seq_num);
459 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
462 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
464 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
466 tr->tunnel_id = gt[0] - gm->tunnels;
467 tr->src = gt[0]->tunnel_src;
468 tr->dst = gt[0]->tunnel_dst.fp_addr;
469 tr->length = vlib_buffer_length_in_chain (vm, b[0]);
476 vlib_buffer_enqueue_to_single_next (vm, node, from,
477 GRE_ENCAP_NEXT_L2_MIDCHAIN,
480 vlib_node_increment_counter (vm, node->node_index,
481 GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
483 return frame->n_vectors;
486 static char *gre_error_strings[] = {
487 #define gre_error(n,s) s,
493 VLIB_REGISTER_NODE (gre_encap_node) =
496 .vector_size = sizeof (u32),
497 .format_trace = format_gre_tx_trace,
498 .type = VLIB_NODE_TYPE_INTERNAL,
499 .n_errors = GRE_N_ERROR,
500 .error_strings = gre_error_strings,
501 .n_next_nodes = GRE_ENCAP_N_NEXT,
503 [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
508 #ifndef CLIB_MARCH_VARIANT
510 format_gre_tunnel_name (u8 * s, va_list * args)
512 u32 dev_instance = va_arg (*args, u32);
513 gre_main_t *gm = &gre_main;
516 if (dev_instance >= vec_len (gm->tunnels))
517 return format (s, "<improperly-referenced>");
519 t = pool_elt_at_index (gm->tunnels, dev_instance);
520 return format (s, "gre%d", t->user_instance);
524 format_gre_device (u8 * s, va_list * args)
526 u32 dev_instance = va_arg (*args, u32);
527 CLIB_UNUSED (int verbose) = va_arg (*args, int);
529 s = format (s, "GRE tunnel: id %d\n", dev_instance);
534 VNET_DEVICE_CLASS (gre_device_class) = {
535 .name = "GRE tunnel device",
536 .format_device_name = format_gre_tunnel_name,
537 .format_device = format_gre_device,
538 .format_tx_trace = format_gre_tx_trace,
539 .admin_up_down_function = gre_interface_admin_up_down,
545 VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
547 .format_header = format_gre_header_with_length,
548 .unformat_header = unformat_gre_header,
549 .build_rewrite = gre_build_rewrite,
550 .update_adjacency = gre_update_adj,
551 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
554 #endif /* CLIB_MARCH_VARIANT */
557 add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name)
559 gre_protocol_info_t *pi;
562 vec_add2 (gm->protocol_infos, pi, 1);
563 i = pi - gm->protocol_infos;
565 pi->name = protocol_name;
566 pi->protocol = protocol;
567 pi->next_index = pi->node_index = ~0;
569 hash_set (gm->protocol_info_by_protocol, protocol, i);
570 hash_set_mem (gm->protocol_info_by_name, pi->name, i);
573 static clib_error_t *
574 gre_init (vlib_main_t * vm)
576 gre_main_t *gm = &gre_main;
578 ip_main_t *im = &ip_main;
579 ip_protocol_info_t *pi;
581 clib_memset (gm, 0, sizeof (gm[0]));
583 gm->vnet_main = vnet_get_main ();
585 if ((error = vlib_call_init_function (vm, ip_main_init)))
588 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
591 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
594 /* Set up the ip packet generator */
595 pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
596 pi->format_header = format_gre_header;
597 pi->unformat_pg_edit = unformat_pg_gre_header;
599 gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
600 gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
602 hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword));
604 hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword));
606 hash_create_mem (0, sizeof (gre_sn_key_t), sizeof (uword));
608 #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
611 return vlib_call_init_function (vm, gre_input_init);
614 VLIB_INIT_FUNCTION (gre_init);
617 * fd.io coding-style-patch-verification: ON
620 * eval: (c-set-style "gnu")