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>
28 ip4_and_gre_header_t ip4_and_gre;
31 } ip4_and_gre_union_t;
37 ip6_and_gre_header_t ip6_and_gre;
40 } ip6_and_gre_union_t;
43 /* Packet trace structure */
46 /* Tunnel-id / index in tunnel vector */
52 /* tunnel ip addresses */
58 format_gre_tx_trace (u8 * s, va_list * args)
60 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
61 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
62 gre_tx_trace_t *t = va_arg (*args, gre_tx_trace_t *);
64 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
65 t->tunnel_id, t->length,
66 format_ip46_address, &t->src, IP46_TYPE_ANY,
67 format_ip46_address, &t->dst, IP46_TYPE_ANY);
72 format_gre_protocol (u8 * s, va_list * args)
74 gre_protocol_t p = va_arg (*args, u32);
75 gre_main_t *gm = &gre_main;
76 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
79 s = format (s, "%s", pi->name);
81 s = format (s, "0x%04x", p);
87 format_gre_header_with_length (u8 * s, va_list * args)
89 gre_main_t *gm = &gre_main;
90 gre_header_t *h = va_arg (*args, gre_header_t *);
91 u32 max_header_bytes = va_arg (*args, u32);
92 gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
93 u32 indent, header_bytes;
95 header_bytes = sizeof (h[0]);
96 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
97 return format (s, "gre header truncated");
99 indent = format_get_indent (s);
101 s = format (s, "GRE %U", format_gre_protocol, p);
103 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
105 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
106 vlib_node_t *node = vlib_get_node (gm->vlib_main, pi->node_index);
107 if (node->format_buffer)
108 s = format (s, "\n%U%U",
109 format_white_space, indent,
110 node->format_buffer, (void *) (h + 1),
111 max_header_bytes - header_bytes);
118 format_gre_header (u8 * s, va_list * args)
120 gre_header_t *h = va_arg (*args, gre_header_t *);
121 return format (s, "%U", format_gre_header_with_length, h, 0);
124 /* Returns gre protocol as an int in host byte order. */
126 unformat_gre_protocol_host_byte_order (unformat_input_t * input,
129 u16 *result = va_arg (*args, u16 *);
130 gre_main_t *gm = &gre_main;
134 if (unformat_user (input, unformat_vlib_number_by_name,
135 gm->protocol_info_by_name, &i))
137 gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i);
138 *result = pi->protocol;
146 unformat_gre_protocol_net_byte_order (unformat_input_t * input,
149 u16 *result = va_arg (*args, u16 *);
150 if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result))
152 *result = clib_host_to_net_u16 ((u16) * result);
157 unformat_gre_header (unformat_input_t * input, va_list * args)
159 u8 **result = va_arg (*args, u8 **);
160 gre_header_t _h, *h = &_h;
163 if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p))
166 h->protocol = clib_host_to_net_u16 (p);
168 /* Add header to result. */
171 u32 n_bytes = sizeof (h[0]);
173 vec_add2 (*result, p, n_bytes);
174 clib_memcpy (p, h, n_bytes);
181 gre_proto_from_vnet_link (vnet_link_t link)
186 return (GRE_PROTOCOL_ip4);
188 return (GRE_PROTOCOL_ip6);
190 return (GRE_PROTOCOL_mpls_unicast);
191 case VNET_LINK_ETHERNET:
192 return (GRE_PROTOCOL_teb);
194 return (GRE_PROTOCOL_arp);
200 return (GRE_PROTOCOL_ip4);
204 gre_build_rewrite (vnet_main_t * vnm,
206 vnet_link_t link_type, const void *dst_address)
208 gre_main_t *gm = &gre_main;
209 ip4_and_gre_header_t *h4;
210 ip6_and_gre_header_t *h6;
217 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
220 /* not one of ours */
223 t = pool_elt_at_index (gm->tunnels, ti);
225 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
229 vec_validate (rewrite, sizeof (*h4) - 1);
230 h4 = (ip4_and_gre_header_t *) rewrite;
232 h4->ip4.ip_version_and_header_length = 0x45;
234 h4->ip4.protocol = IP_PROTOCOL_GRE;
235 /* fixup ip4 header length and checksum after-the-fact */
236 h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32;
237 h4->ip4.dst_address.as_u32 = t->tunnel_dst.fp_addr.ip4.as_u32;
238 h4->ip4.checksum = ip4_header_checksum (&h4->ip4);
242 vec_validate (rewrite, sizeof (*h6) - 1);
243 h6 = (ip6_and_gre_header_t *) rewrite;
245 h6->ip6.ip_version_traffic_class_and_flow_label =
246 clib_host_to_net_u32 (6 << 28);
247 h6->ip6.hop_limit = 255;
248 h6->ip6.protocol = IP_PROTOCOL_GRE;
249 /* fixup ip6 header length and checksum after-the-fact */
250 h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
251 h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
252 h6->ip6.dst_address.as_u64[0] = t->tunnel_dst.fp_addr.ip6.as_u64[0];
253 h6->ip6.dst_address.as_u64[1] = t->tunnel_dst.fp_addr.ip6.as_u64[1];
256 if (PREDICT_FALSE (t->type == GRE_TUNNEL_TYPE_ERSPAN))
258 gre->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_erspan);
259 gre->flags_and_version = clib_host_to_net_u16 (GRE_FLAGS_SEQUENCE);
263 clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
268 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
271 gre4_fixup (vlib_main_t * vm,
272 ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
276 ip0 = vlib_buffer_get_current (b0);
278 /* Fixup the checksum and len fields in the GRE tunnel encap
279 * that was applied at the midchain node */
280 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
281 ip0->checksum = ip4_header_checksum (ip0);
285 gre6_fixup (vlib_main_t * vm,
286 ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
290 ip0 = vlib_buffer_get_current (b0);
292 /* Fixup the payload length field in the GRE tunnel encap that was applied
293 * at the midchain node */
294 ip0->payload_length =
295 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
300 gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
302 gre_main_t *gm = &gre_main;
308 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
309 t = pool_elt_at_index (gm->tunnels, ti);
310 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
311 af = ADJ_FLAG_MIDCHAIN_IP_STACK;
313 if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
314 af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
316 adj_nbr_midchain_update_rewrite
317 (ai, !is_ipv6 ? gre4_fixup : gre6_fixup, NULL, af,
318 gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), NULL));
320 gre_tunnel_stack (ai);
326 GRE_ENCAP_NEXT_L2_MIDCHAIN,
331 * @brief TX function. Only called for L2 payload including TEB or ERSPAN.
332 * L3 traffic uses the adj-midchains.
335 gre_interface_tx (vlib_main_t * vm,
336 vlib_node_runtime_t * node, vlib_frame_t * frame)
338 gre_main_t *gm = &gre_main;
339 vnet_main_t *vnm = gm->vnet_main;
341 u32 *from, *to_next, n_left_from, n_left_to_next;
342 u32 sw_if_index0 = 0;
343 u32 sw_if_index1 = 0;
344 adj_index_t adj_index0 = ADJ_INDEX_INVALID;
345 adj_index_t adj_index1 = ADJ_INDEX_INVALID;
346 gre_tunnel_t *gt0 = NULL;
347 gre_tunnel_t *gt1 = NULL;
349 /* Vector of buffer / pkt indices we're supposed to process */
350 from = vlib_frame_vector_args (frame);
352 /* Number of buffers / pkts */
353 n_left_from = frame->n_vectors;
355 /* Speculatively send the first buffer to the last disposition we used */
356 next_index = GRE_ENCAP_NEXT_L2_MIDCHAIN;
358 while (n_left_from > 0)
360 /* set up to enqueue to our disposition with index = next_index */
361 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
363 while (n_left_from >= 4 && n_left_to_next >= 2)
367 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
368 vlib_buffer_t *b1 = vlib_get_buffer (vm, bi1);
377 if (sw_if_index0 != vnet_buffer (b0)->sw_if_index[VLIB_TX])
379 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
380 vnet_hw_interface_t *hi0 =
381 vnet_get_sup_hw_interface (vnm, sw_if_index0);
382 gt0 = &gm->tunnels[hi0->dev_instance];
383 adj_index0 = gt0->l2_adj_index;
386 if (sw_if_index1 != vnet_buffer (b1)->sw_if_index[VLIB_TX])
388 if (sw_if_index0 == vnet_buffer (b1)->sw_if_index[VLIB_TX])
390 sw_if_index1 = sw_if_index0;
392 adj_index1 = adj_index0;
396 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
397 vnet_hw_interface_t *hi1 =
398 vnet_get_sup_hw_interface (vnm, sw_if_index1);
399 gt1 = &gm->tunnels[hi1->dev_instance];
400 adj_index1 = gt1->l2_adj_index;
404 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = adj_index0;
405 vnet_buffer (b1)->ip.adj_index[VLIB_TX] = adj_index1;
407 if (PREDICT_FALSE (gt0->type == GRE_TUNNEL_TYPE_ERSPAN))
409 /* Encap GRE seq# and ERSPAN type II header */
410 vlib_buffer_advance (b0, -sizeof (erspan_t2_t));
411 erspan_t2_t *h0 = vlib_buffer_get_current (b0);
412 u32 seq_num = clib_atomic_fetch_add (>0->gre_sn->seq_num, 1);
413 u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
414 h0->seq_num = clib_host_to_net_u32 (seq_num);
416 h0->t2.cos_en_t_session |=
417 clib_host_to_net_u16 (gt0->session_id);
419 if (PREDICT_FALSE (gt1->type == GRE_TUNNEL_TYPE_ERSPAN))
421 /* Encap GRE seq# and ERSPAN type II header */
422 vlib_buffer_advance (b1, -sizeof (erspan_t2_t));
423 erspan_t2_t *h1 = vlib_buffer_get_current (b1);
424 u32 seq_num = clib_atomic_fetch_add (>1->gre_sn->seq_num, 1);
425 u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
426 h1->seq_num = clib_host_to_net_u32 (seq_num);
428 h1->t2.cos_en_t_session |=
429 clib_host_to_net_u16 (gt1->session_id);
432 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
434 gre_tx_trace_t *tr0 = vlib_add_trace (vm, node,
436 tr0->tunnel_id = gt0 - gm->tunnels;
437 tr0->src = gt0->tunnel_src;
438 tr0->dst = gt0->tunnel_dst.fp_addr;
439 tr0->length = vlib_buffer_length_in_chain (vm, b0);
441 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
443 gre_tx_trace_t *tr1 = vlib_add_trace (vm, node,
445 tr1->tunnel_id = gt1 - gm->tunnels;
446 tr1->src = gt1->tunnel_src;
447 tr1->dst = gt1->tunnel_dst.fp_addr;
448 tr1->length = vlib_buffer_length_in_chain (vm, b1);
452 while (n_left_from > 0 && n_left_to_next > 0)
455 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
463 if (sw_if_index0 != vnet_buffer (b0)->sw_if_index[VLIB_TX])
465 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
466 vnet_hw_interface_t *hi0 =
467 vnet_get_sup_hw_interface (vnm, sw_if_index0);
468 gt0 = &gm->tunnels[hi0->dev_instance];
469 adj_index0 = gt0->l2_adj_index;
472 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = adj_index0;
474 if (PREDICT_FALSE (gt0->type == GRE_TUNNEL_TYPE_ERSPAN))
476 /* Encap GRE seq# and ERSPAN type II header */
477 vlib_buffer_advance (b0, -sizeof (erspan_t2_t));
478 erspan_t2_t *h0 = vlib_buffer_get_current (b0);
479 u32 seq_num = clib_atomic_fetch_add (>0->gre_sn->seq_num, 1);
480 u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
481 h0->seq_num = clib_host_to_net_u32 (seq_num);
483 h0->t2.cos_en_t_session |=
484 clib_host_to_net_u16 (gt0->session_id);
487 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
489 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
491 tr->tunnel_id = gt0 - gm->tunnels;
492 tr->src = gt0->tunnel_src;
493 tr->dst = gt0->tunnel_dst.fp_addr;
494 tr->length = vlib_buffer_length_in_chain (vm, b0);
498 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
501 vlib_node_increment_counter (vm, node->node_index,
502 GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
504 return frame->n_vectors;
507 static char *gre_error_strings[] = {
508 #define gre_error(n,s) s,
514 VLIB_REGISTER_NODE (gre_encap_node) =
516 .function = gre_interface_tx,
518 .vector_size = sizeof (u32),
519 .format_trace = format_gre_tx_trace,
520 .type = VLIB_NODE_TYPE_INTERNAL,
521 .n_errors = GRE_N_ERROR,
522 .error_strings = gre_error_strings,
523 .n_next_nodes = GRE_ENCAP_N_NEXT,
525 [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
529 VLIB_NODE_FUNCTION_MULTIARCH (gre_encap_node, gre_interface_tx)
533 format_gre_tunnel_name (u8 * s, va_list * args)
535 u32 dev_instance = va_arg (*args, u32);
536 gre_main_t *gm = &gre_main;
539 if (dev_instance >= vec_len (gm->tunnels))
540 return format (s, "<improperly-referenced>");
542 t = pool_elt_at_index (gm->tunnels, dev_instance);
543 return format (s, "gre%d", t->user_instance);
547 format_gre_device (u8 * s, va_list * args)
549 u32 dev_instance = va_arg (*args, u32);
550 CLIB_UNUSED (int verbose) = va_arg (*args, int);
552 s = format (s, "GRE tunnel: id %d\n", dev_instance);
557 VNET_DEVICE_CLASS (gre_device_class) = {
558 .name = "GRE tunnel device",
559 .format_device_name = format_gre_tunnel_name,
560 .format_device = format_gre_device,
561 .format_tx_trace = format_gre_tx_trace,
562 .admin_up_down_function = gre_interface_admin_up_down,
568 VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
570 .format_header = format_gre_header_with_length,
571 .unformat_header = unformat_gre_header,
572 .build_rewrite = gre_build_rewrite,
573 .update_adjacency = gre_update_adj,
574 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
579 add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name)
581 gre_protocol_info_t *pi;
584 vec_add2 (gm->protocol_infos, pi, 1);
585 i = pi - gm->protocol_infos;
587 pi->name = protocol_name;
588 pi->protocol = protocol;
589 pi->next_index = pi->node_index = ~0;
591 hash_set (gm->protocol_info_by_protocol, protocol, i);
592 hash_set_mem (gm->protocol_info_by_name, pi->name, i);
595 static clib_error_t *
596 gre_init (vlib_main_t * vm)
598 gre_main_t *gm = &gre_main;
600 ip_main_t *im = &ip_main;
601 ip_protocol_info_t *pi;
603 clib_memset (gm, 0, sizeof (gm[0]));
605 gm->vnet_main = vnet_get_main ();
607 if ((error = vlib_call_init_function (vm, ip_main_init)))
610 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
613 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
616 /* Set up the ip packet generator */
617 pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
618 pi->format_header = format_gre_header;
619 pi->unformat_pg_edit = unformat_pg_gre_header;
621 gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
622 gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
624 hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword));
626 hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword));
628 hash_create_mem (0, sizeof (gre_sn_key_t), sizeof (uword));
630 #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
633 return vlib_call_init_function (vm, gre_input_init);
636 VLIB_INIT_FUNCTION (gre_init);
639 * fd.io coding-style-patch-verification: ON
642 * eval: (c-set-style "gnu")