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>
20 #include <vnet/adj/adj_midchain.h>
21 #include <vnet/tunnel/tunnel_dp.h>
22 #include <vpp/app/version.h>
23 #include <vnet/plugin/plugin.h>
25 extern gre_main_t gre_main;
27 #ifndef CLIB_MARCH_VARIANT
34 ip4_and_gre_header_t ip4_and_gre;
37 } ip4_and_gre_union_t;
43 ip6_and_gre_header_t ip6_and_gre;
46 } ip6_and_gre_union_t;
47 #endif /* CLIB_MARCH_VARIANT */
49 /* Packet trace structure */
52 /* Tunnel-id / index in tunnel vector */
58 /* tunnel ip addresses */
63 extern u8 *format_gre_tx_trace (u8 *s, va_list *args);
65 #ifndef CLIB_MARCH_VARIANT
67 format_gre_tx_trace (u8 *s, va_list *args)
69 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
70 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
71 gre_tx_trace_t *t = va_arg (*args, gre_tx_trace_t *);
73 s = format (s, "GRE: tunnel %d len %d src %U dst %U", t->tunnel_id,
74 t->length, format_ip46_address, &t->src, IP46_TYPE_ANY,
75 format_ip46_address, &t->dst, IP46_TYPE_ANY);
80 format_gre_protocol (u8 *s, va_list *args)
82 gre_protocol_t p = va_arg (*args, u32);
83 gre_main_t *gm = &gre_main;
84 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
87 s = format (s, "%s", pi->name);
89 s = format (s, "0x%04x", p);
95 format_gre_header_with_length (u8 *s, va_list *args)
97 gre_main_t *gm = &gre_main;
98 gre_header_t *h = va_arg (*args, gre_header_t *);
99 u32 max_header_bytes = va_arg (*args, u32);
100 gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
101 u32 indent, header_bytes;
103 header_bytes = sizeof (h[0]);
104 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
105 return format (s, "gre header truncated");
107 indent = format_get_indent (s);
109 s = format (s, "GRE %U", format_gre_protocol, p);
111 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
113 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
114 vlib_node_t *node = vlib_get_node (gm->vlib_main, pi->node_index);
115 if (node->format_buffer)
117 format (s, "\n%U%U", format_white_space, indent, node->format_buffer,
118 (void *) (h + 1), 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, va_list *args)
135 u16 *result = va_arg (*args, u16 *);
136 gre_main_t *gm = &gre_main;
140 if (unformat_user (input, unformat_vlib_number_by_name,
141 gm->protocol_info_by_name, &i))
143 gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i);
144 *result = pi->protocol;
152 unformat_gre_protocol_net_byte_order (unformat_input_t *input, va_list *args)
154 u16 *result = va_arg (*args, u16 *);
155 if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result))
157 *result = clib_host_to_net_u16 ((u16) *result);
162 unformat_gre_header (unformat_input_t *input, va_list *args)
164 u8 **result = va_arg (*args, u8 **);
165 gre_header_t _h, *h = &_h;
168 if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p))
171 h->protocol = clib_host_to_net_u16 (p);
173 /* Add header to result. */
176 u32 n_bytes = sizeof (h[0]);
178 vec_add2 (*result, p, n_bytes);
179 clib_memcpy (p, h, n_bytes);
186 gre_proto_from_vnet_link (vnet_link_t link)
191 return (GRE_PROTOCOL_ip4);
193 return (GRE_PROTOCOL_ip6);
195 return (GRE_PROTOCOL_mpls_unicast);
196 case VNET_LINK_ETHERNET:
197 return (GRE_PROTOCOL_teb);
199 return (GRE_PROTOCOL_arp);
205 return (GRE_PROTOCOL_ip4);
209 gre_build_rewrite (vnet_main_t *vnm, u32 sw_if_index, vnet_link_t link_type,
210 const void *dst_address)
212 gre_main_t *gm = &gre_main;
213 const ip46_address_t *dst;
214 ip4_and_gre_header_t *h4;
215 ip6_and_gre_header_t *h6;
223 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
226 /* not one of ours */
229 t = pool_elt_at_index (gm->tunnels, ti);
231 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
235 vec_validate (rewrite, sizeof (*h4) - 1);
236 h4 = (ip4_and_gre_header_t *) rewrite;
238 h4->ip4.ip_version_and_header_length = 0x45;
240 h4->ip4.protocol = IP_PROTOCOL_GRE;
241 /* fixup ip4 header length and checksum after-the-fact */
242 h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32;
243 h4->ip4.dst_address.as_u32 = dst->ip4.as_u32;
244 h4->ip4.checksum = ip4_header_checksum (&h4->ip4);
248 vec_validate (rewrite, sizeof (*h6) - 1);
249 h6 = (ip6_and_gre_header_t *) rewrite;
251 h6->ip6.ip_version_traffic_class_and_flow_label =
252 clib_host_to_net_u32 (6 << 28);
253 h6->ip6.hop_limit = 255;
254 h6->ip6.protocol = IP_PROTOCOL_GRE;
255 /* fixup ip6 header length and checksum after-the-fact */
256 h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
257 h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
258 h6->ip6.dst_address.as_u64[0] = dst->ip6.as_u64[0];
259 h6->ip6.dst_address.as_u64[1] = dst->ip6.as_u64[1];
262 if (PREDICT_FALSE (t->type == GRE_TUNNEL_TYPE_ERSPAN))
264 gre->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_erspan);
265 gre->flags_and_version = clib_host_to_net_u16 (GRE_FLAGS_SEQUENCE);
269 clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
275 gre44_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b0,
278 tunnel_encap_decap_flags_t flags;
279 ip4_and_gre_header_t *ip0;
281 ip0 = vlib_buffer_get_current (b0);
282 flags = pointer_to_uword (data);
284 /* Fixup the checksum and len fields in the GRE tunnel encap
285 * that was applied at the midchain node */
287 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
288 tunnel_encap_fixup_4o4 (flags, (ip4_header_t *) (ip0 + 1), &ip0->ip4);
289 ip0->ip4.checksum = ip4_header_checksum (&ip0->ip4);
293 gre64_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b0,
296 tunnel_encap_decap_flags_t flags;
297 ip4_and_gre_header_t *ip0;
299 ip0 = vlib_buffer_get_current (b0);
300 flags = pointer_to_uword (data);
302 /* Fixup the checksum and len fields in the GRE tunnel encap
303 * that was applied at the midchain node */
305 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
306 tunnel_encap_fixup_6o4 (flags, (ip6_header_t *) (ip0 + 1), &ip0->ip4);
307 ip0->ip4.checksum = ip4_header_checksum (&ip0->ip4);
311 grex4_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b0,
316 ip0 = vlib_buffer_get_current (b0);
318 /* Fixup the checksum and len fields in the GRE tunnel encap
319 * that was applied at the midchain node */
320 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
321 ip0->checksum = ip4_header_checksum (ip0);
325 gre46_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b0,
328 tunnel_encap_decap_flags_t flags;
329 ip6_and_gre_header_t *ip0;
331 ip0 = vlib_buffer_get_current (b0);
332 flags = pointer_to_uword (data);
334 /* Fixup the payload length field in the GRE tunnel encap that was applied
335 * at the midchain node */
336 ip0->ip6.payload_length = clib_host_to_net_u16 (
337 vlib_buffer_length_in_chain (vm, b0) - sizeof (ip0->ip6));
338 tunnel_encap_fixup_4o6 (flags, b0, (ip4_header_t *) (ip0 + 1), &ip0->ip6);
342 gre66_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b0,
345 tunnel_encap_decap_flags_t flags;
346 ip6_and_gre_header_t *ip0;
348 ip0 = vlib_buffer_get_current (b0);
349 flags = pointer_to_uword (data);
351 /* Fixup the payload length field in the GRE tunnel encap that was applied
352 * at the midchain node */
353 ip0->ip6.payload_length = clib_host_to_net_u16 (
354 vlib_buffer_length_in_chain (vm, b0) - sizeof (ip0->ip6));
355 tunnel_encap_fixup_6o6 (flags, (ip6_header_t *) (ip0 + 1), &ip0->ip6);
359 grex6_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b0,
362 ip6_and_gre_header_t *ip0;
364 ip0 = vlib_buffer_get_current (b0);
366 /* Fixup the payload length field in the GRE tunnel encap that was applied
367 * at the midchain node */
368 ip0->ip6.payload_length = clib_host_to_net_u16 (
369 vlib_buffer_length_in_chain (vm, b0) - sizeof (ip0->ip6));
373 * return the appropriate fixup function given the overlay (link-type) and
374 * underlay (fproto) combination
376 static adj_midchain_fixup_t
377 gre_get_fixup (fib_protocol_t fproto, vnet_link_t lt)
379 if (fproto == FIB_PROTOCOL_IP6 && lt == VNET_LINK_IP6)
380 return (gre66_fixup);
381 if (fproto == FIB_PROTOCOL_IP6 && lt == VNET_LINK_IP4)
382 return (gre46_fixup);
383 if (fproto == FIB_PROTOCOL_IP4 && lt == VNET_LINK_IP6)
384 return (gre64_fixup);
385 if (fproto == FIB_PROTOCOL_IP4 && lt == VNET_LINK_IP4)
386 return (gre44_fixup);
387 if (fproto == FIB_PROTOCOL_IP6)
388 return (grex6_fixup);
389 if (fproto == FIB_PROTOCOL_IP4)
390 return (grex4_fixup);
393 return (gre44_fixup);
397 gre_update_adj (vnet_main_t *vnm, u32 sw_if_index, adj_index_t ai)
399 gre_main_t *gm = &gre_main;
404 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
405 t = pool_elt_at_index (gm->tunnels, ti);
409 * the user has not requested that the load-balancing be based on
410 * a flow hash of the inner packet. so use the stacking to choose
413 if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH))
414 af |= ADJ_FLAG_MIDCHAIN_IP_STACK;
416 adj_nbr_midchain_update_rewrite (
417 ai, gre_get_fixup (t->tunnel_dst.fp_proto, adj_get_link_type (ai)),
418 uword_to_pointer (t->flags, void *), af,
419 gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai),
420 &t->tunnel_dst.fp_addr));
422 gre_tunnel_stack (ai);
426 mgre_mk_complete_walk (adj_index_t ai, void *data)
428 mgre_walk_ctx_t *ctx = data;
434 * the user has not requested that the load-balancing be based on
435 * a flow hash of the inner packet. so use the stacking to choose
438 if (!(ctx->t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH))
439 af |= ADJ_FLAG_MIDCHAIN_IP_STACK;
441 adj_nbr_midchain_update_rewrite (
442 ai, gre_get_fixup (ctx->t->tunnel_dst.fp_proto, adj_get_link_type (ai)),
443 uword_to_pointer (ctx->t->flags, void *), af,
444 gre_build_rewrite (vnet_get_main (), ctx->t->sw_if_index,
445 adj_get_link_type (ai),
446 &teib_entry_get_nh (ctx->ne)->fp_addr));
448 teib_entry_adj_stack (ctx->ne, ai);
450 return (ADJ_WALK_RC_CONTINUE);
454 mgre_mk_incomplete_walk (adj_index_t ai, void *data)
456 gre_tunnel_t *t = data;
458 adj_nbr_midchain_update_rewrite (
459 ai, gre_get_fixup (t->tunnel_dst.fp_proto, adj_get_link_type (ai)), NULL,
460 ADJ_FLAG_NONE, NULL);
462 adj_midchain_delegate_unstack (ai);
464 return (ADJ_WALK_RC_CONTINUE);
468 mgre_update_adj (vnet_main_t *vnm, u32 sw_if_index, adj_index_t ai)
470 gre_main_t *gm = &gre_main;
477 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
478 t = pool_elt_at_index (gm->tunnels, ti);
480 ne = teib_entry_find_46 (sw_if_index, adj->ia_nh_proto,
481 &adj->sub_type.nbr.next_hop);
485 // no TEIB entry to provide the next-hop
486 adj_nbr_midchain_update_rewrite (
487 ai, gre_get_fixup (t->tunnel_dst.fp_proto, adj_get_link_type (ai)),
488 uword_to_pointer (t->flags, void *), ADJ_FLAG_NONE, NULL);
492 mgre_walk_ctx_t ctx = { .t = t, .ne = ne };
493 adj_nbr_walk_nh (sw_if_index, adj->ia_nh_proto, &adj->sub_type.nbr.next_hop,
494 mgre_mk_complete_walk, &ctx);
496 #endif /* CLIB_MARCH_VARIANT */
500 GRE_ENCAP_NEXT_L2_MIDCHAIN,
505 * @brief TX function. Only called for L2 payload including TEB or ERSPAN.
506 * L3 traffic uses the adj-midchains.
508 static_always_inline u32
509 gre_encap_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
510 vlib_frame_t *frame, gre_tunnel_type_t type)
512 gre_main_t *gm = &gre_main;
513 u32 *from, n_left_from;
514 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
515 u32 sw_if_index[2] = { ~0, ~0 };
516 const gre_tunnel_t *gt[2] = { 0 };
517 adj_index_t adj_index[2] = { ADJ_INDEX_INVALID, ADJ_INDEX_INVALID };
519 from = vlib_frame_vector_args (frame);
520 n_left_from = frame->n_vectors;
521 vlib_get_buffers (vm, from, bufs, n_left_from);
523 while (n_left_from >= 2)
526 if (PREDICT_FALSE (sw_if_index[0] !=
527 vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
529 const vnet_hw_interface_t *hi;
530 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
531 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
532 gt[0] = &gm->tunnels[hi->dev_instance];
533 adj_index[0] = gt[0]->l2_adj_index;
535 if (PREDICT_FALSE (sw_if_index[1] !=
536 vnet_buffer (b[1])->sw_if_index[VLIB_TX]))
538 const vnet_hw_interface_t *hi;
539 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
540 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[1]);
541 gt[1] = &gm->tunnels[hi->dev_instance];
542 adj_index[1] = gt[1]->l2_adj_index;
545 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
546 vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = adj_index[1];
548 if (type == GRE_TUNNEL_TYPE_ERSPAN)
550 /* Encap GRE seq# and ERSPAN type II header */
554 vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
555 h0 = vlib_buffer_get_current (b[0]);
556 seq_num = clib_atomic_fetch_add (>[0]->gre_sn->seq_num, 1);
557 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
558 h0->seq_num = clib_host_to_net_u32 (seq_num);
560 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
562 if (type == GRE_TUNNEL_TYPE_ERSPAN)
564 /* Encap GRE seq# and ERSPAN type II header */
568 vlib_buffer_advance (b[1], -sizeof (erspan_t2_t));
569 h0 = vlib_buffer_get_current (b[1]);
570 seq_num = clib_atomic_fetch_add (>[1]->gre_sn->seq_num, 1);
571 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
572 h0->seq_num = clib_host_to_net_u32 (seq_num);
574 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[1]->session_id);
577 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
579 gre_tx_trace_t *tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
580 tr->tunnel_id = gt[0] - gm->tunnels;
581 tr->src = gt[0]->tunnel_src;
582 tr->dst = gt[0]->tunnel_dst.fp_addr;
583 tr->length = vlib_buffer_length_in_chain (vm, b[0]);
585 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
587 gre_tx_trace_t *tr = vlib_add_trace (vm, node, b[1], sizeof (*tr));
588 tr->tunnel_id = gt[1] - gm->tunnels;
589 tr->src = gt[1]->tunnel_src;
590 tr->dst = gt[1]->tunnel_dst.fp_addr;
591 tr->length = vlib_buffer_length_in_chain (vm, b[1]);
598 while (n_left_from >= 1)
601 if (PREDICT_FALSE (sw_if_index[0] !=
602 vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
604 const vnet_hw_interface_t *hi;
605 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
606 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
607 gt[0] = &gm->tunnels[hi->dev_instance];
608 adj_index[0] = gt[0]->l2_adj_index;
611 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
613 if (type == GRE_TUNNEL_TYPE_ERSPAN)
615 /* Encap GRE seq# and ERSPAN type II header */
619 ASSERT (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN);
620 vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
621 h0 = vlib_buffer_get_current (b[0]);
622 seq_num = clib_atomic_fetch_add (>[0]->gre_sn->seq_num, 1);
623 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
624 h0->seq_num = clib_host_to_net_u32 (seq_num);
626 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
629 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
631 gre_tx_trace_t *tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
632 tr->tunnel_id = gt[0] - gm->tunnels;
633 tr->src = gt[0]->tunnel_src;
634 tr->dst = gt[0]->tunnel_dst.fp_addr;
635 tr->length = vlib_buffer_length_in_chain (vm, b[0]);
642 vlib_buffer_enqueue_to_single_next (
643 vm, node, from, GRE_ENCAP_NEXT_L2_MIDCHAIN, frame->n_vectors);
645 vlib_node_increment_counter (vm, node->node_index, GRE_ERROR_PKTS_ENCAP,
648 return frame->n_vectors;
651 static char *gre_error_strings[] = {
652 #define gre_error(n, s) s,
657 VLIB_NODE_FN (gre_teb_encap_node)
658 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
660 return (gre_encap_inline (vm, node, frame, GRE_TUNNEL_TYPE_TEB));
663 VLIB_NODE_FN (gre_erspan_encap_node)
664 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
666 return (gre_encap_inline (vm, node, frame, GRE_TUNNEL_TYPE_ERSPAN));
670 VLIB_REGISTER_NODE (gre_teb_encap_node) =
672 .name = "gre-teb-encap",
673 .vector_size = sizeof (u32),
674 .format_trace = format_gre_tx_trace,
675 .type = VLIB_NODE_TYPE_INTERNAL,
676 .n_errors = GRE_N_ERROR,
677 .error_strings = gre_error_strings,
678 .n_next_nodes = GRE_ENCAP_N_NEXT,
680 [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
683 VLIB_REGISTER_NODE (gre_erspan_encap_node) =
685 .name = "gre-erspan-encap",
686 .vector_size = sizeof (u32),
687 .format_trace = format_gre_tx_trace,
688 .type = VLIB_NODE_TYPE_INTERNAL,
689 .n_errors = GRE_N_ERROR,
690 .error_strings = gre_error_strings,
691 .n_next_nodes = GRE_ENCAP_N_NEXT,
693 [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
698 #ifndef CLIB_MARCH_VARIANT
700 format_gre_tunnel_name (u8 *s, va_list *args)
702 u32 dev_instance = va_arg (*args, u32);
703 gre_main_t *gm = &gre_main;
706 if (dev_instance >= vec_len (gm->tunnels))
707 return format (s, "<improperly-referenced>");
709 t = pool_elt_at_index (gm->tunnels, dev_instance);
710 return format (s, "gre%d", t->user_instance);
714 format_gre_device (u8 *s, va_list *args)
716 u32 dev_instance = va_arg (*args, u32);
717 CLIB_UNUSED (int verbose) = va_arg (*args, int);
719 s = format (s, "GRE tunnel: id %d\n", dev_instance);
724 gre_tunnel_desc (u32 sw_if_index, ip46_address_t *src, ip46_address_t *dst,
727 gre_main_t *gm = &gre_main;
731 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
734 /* not one of ours */
737 t = pool_elt_at_index (gm->tunnels, ti);
739 *src = t->tunnel_src;
740 *dst = t->tunnel_dst.fp_addr;
741 *is_l2 = t->type == GRE_TUNNEL_TYPE_TEB;
747 VNET_DEVICE_CLASS (gre_device_class) = {
748 .name = "GRE tunnel device",
749 .format_device_name = format_gre_tunnel_name,
750 .format_device = format_gre_device,
751 .format_tx_trace = format_gre_tx_trace,
752 .admin_up_down_function = gre_interface_admin_up_down,
753 .ip_tun_desc = gre_tunnel_desc,
760 VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
762 .format_header = format_gre_header_with_length,
763 .unformat_header = unformat_gre_header,
764 .build_rewrite = gre_build_rewrite,
765 .update_adjacency = gre_update_adj,
766 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
769 VNET_HW_INTERFACE_CLASS (mgre_hw_interface_class) = {
771 .format_header = format_gre_header_with_length,
772 .unformat_header = unformat_gre_header,
773 .build_rewrite = gre_build_rewrite,
774 .update_adjacency = mgre_update_adj,
775 .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
778 #endif /* CLIB_MARCH_VARIANT */
781 add_protocol (gre_main_t *gm, gre_protocol_t protocol, char *protocol_name)
783 gre_protocol_info_t *pi;
786 vec_add2 (gm->protocol_infos, pi, 1);
787 i = pi - gm->protocol_infos;
789 pi->name = protocol_name;
790 pi->protocol = protocol;
791 pi->next_index = pi->node_index = ~0;
793 hash_set (gm->protocol_info_by_protocol, protocol, i);
794 hash_set_mem (gm->protocol_info_by_name, pi->name, i);
797 static clib_error_t *
798 gre_init (vlib_main_t *vm)
800 gre_main_t *gm = &gre_main;
802 ip_main_t *im = &ip_main;
803 ip_protocol_info_t *pi;
805 clib_memset (gm, 0, sizeof (gm[0]));
807 gm->vnet_main = vnet_get_main ();
809 if ((error = vlib_call_init_function (vm, ip_main_init)))
812 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
815 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
818 /* Set up the ip packet generator */
819 pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
820 pi->format_header = format_gre_header;
821 pi->unformat_pg_edit = unformat_pg_gre_header;
823 gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
824 gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
826 hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword));
828 hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword));
830 hash_create_mem (0, sizeof (gre_sn_key_t), sizeof (uword));
832 #define _(n, s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
835 return vlib_call_init_function (vm, gre_input_init);
838 VLIB_INIT_FUNCTION (gre_init);
841 * fd.io coding-style-patch-verification: ON
844 * eval: (c-set-style "gnu")