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>
21 #include <vnet/tunnel/tunnel_dp.h>
23 extern gre_main_t gre_main;
25 #ifndef CLIB_MARCH_VARIANT
32 ip4_and_gre_header_t ip4_and_gre;
35 } ip4_and_gre_union_t;
41 ip6_and_gre_header_t ip6_and_gre;
44 } ip6_and_gre_union_t;
45 #endif /* CLIB_MARCH_VARIANT */
48 /* Packet trace structure */
51 /* Tunnel-id / index in tunnel vector */
57 /* tunnel ip addresses */
62 extern u8 *format_gre_tx_trace (u8 * s, va_list * args);
64 #ifndef CLIB_MARCH_VARIANT
66 format_gre_tx_trace (u8 * s, va_list * args)
68 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
69 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
70 gre_tx_trace_t *t = va_arg (*args, gre_tx_trace_t *);
72 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
73 t->tunnel_id, t->length,
74 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)
116 s = format (s, "\n%U%U",
117 format_white_space, indent,
118 node->format_buffer, (void *) (h + 1),
119 max_header_bytes - header_bytes);
126 format_gre_header (u8 * s, va_list * args)
128 gre_header_t *h = va_arg (*args, gre_header_t *);
129 return format (s, "%U", format_gre_header_with_length, h, 0);
132 /* Returns gre protocol as an int in host byte order. */
134 unformat_gre_protocol_host_byte_order (unformat_input_t * input,
137 u16 *result = va_arg (*args, u16 *);
138 gre_main_t *gm = &gre_main;
142 if (unformat_user (input, unformat_vlib_number_by_name,
143 gm->protocol_info_by_name, &i))
145 gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i);
146 *result = pi->protocol;
154 unformat_gre_protocol_net_byte_order (unformat_input_t * input,
157 u16 *result = va_arg (*args, u16 *);
158 if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result))
160 *result = clib_host_to_net_u16 ((u16) * result);
165 unformat_gre_header (unformat_input_t * input, va_list * args)
167 u8 **result = va_arg (*args, u8 **);
168 gre_header_t _h, *h = &_h;
171 if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p))
174 h->protocol = clib_host_to_net_u16 (p);
176 /* Add header to result. */
179 u32 n_bytes = sizeof (h[0]);
181 vec_add2 (*result, p, n_bytes);
182 clib_memcpy (p, h, n_bytes);
189 gre_proto_from_vnet_link (vnet_link_t link)
194 return (GRE_PROTOCOL_ip4);
196 return (GRE_PROTOCOL_ip6);
198 return (GRE_PROTOCOL_mpls_unicast);
199 case VNET_LINK_ETHERNET:
200 return (GRE_PROTOCOL_teb);
202 return (GRE_PROTOCOL_arp);
208 return (GRE_PROTOCOL_ip4);
212 gre_build_rewrite (vnet_main_t * vnm,
214 vnet_link_t link_type, const void *dst_address)
216 gre_main_t *gm = &gre_main;
217 const ip46_address_t *dst;
218 ip4_and_gre_header_t *h4;
219 ip6_and_gre_header_t *h6;
227 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
230 /* not one of ours */
233 t = pool_elt_at_index (gm->tunnels, ti);
235 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
239 vec_validate (rewrite, sizeof (*h4) - 1);
240 h4 = (ip4_and_gre_header_t *) rewrite;
242 h4->ip4.ip_version_and_header_length = 0x45;
244 h4->ip4.protocol = IP_PROTOCOL_GRE;
245 /* fixup ip4 header length and checksum after-the-fact */
246 h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32;
247 h4->ip4.dst_address.as_u32 = dst->ip4.as_u32;
248 h4->ip4.checksum = ip4_header_checksum (&h4->ip4);
252 vec_validate (rewrite, sizeof (*h6) - 1);
253 h6 = (ip6_and_gre_header_t *) rewrite;
255 h6->ip6.ip_version_traffic_class_and_flow_label =
256 clib_host_to_net_u32 (6 << 28);
257 h6->ip6.hop_limit = 255;
258 h6->ip6.protocol = IP_PROTOCOL_GRE;
259 /* fixup ip6 header length and checksum after-the-fact */
260 h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
261 h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
262 h6->ip6.dst_address.as_u64[0] = dst->ip6.as_u64[0];
263 h6->ip6.dst_address.as_u64[1] = dst->ip6.as_u64[1];
266 if (PREDICT_FALSE (t->type == GRE_TUNNEL_TYPE_ERSPAN))
268 gre->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_erspan);
269 gre->flags_and_version = clib_host_to_net_u16 (GRE_FLAGS_SEQUENCE);
273 clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
279 gre44_fixup (vlib_main_t * vm,
280 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
282 tunnel_encap_decap_flags_t flags;
283 ip4_and_gre_header_t *ip0;
285 ip0 = vlib_buffer_get_current (b0);
286 flags = pointer_to_uword (data);
288 /* Fixup the checksum and len fields in the GRE tunnel encap
289 * that was applied at the midchain node */
291 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
292 tunnel_encap_fixup_4o4 (flags, (ip4_header_t *) (ip0 + 1), &ip0->ip4);
293 ip0->ip4.checksum = ip4_header_checksum (&ip0->ip4);
297 gre64_fixup (vlib_main_t * vm,
298 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
300 tunnel_encap_decap_flags_t flags;
301 ip4_and_gre_header_t *ip0;
303 ip0 = vlib_buffer_get_current (b0);
304 flags = pointer_to_uword (data);
306 /* Fixup the checksum and len fields in the GRE tunnel encap
307 * that was applied at the midchain node */
309 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
310 tunnel_encap_fixup_6o4 (flags, (ip6_header_t *) (ip0 + 1), &ip0->ip4);
311 ip0->ip4.checksum = ip4_header_checksum (&ip0->ip4);
315 grex4_fixup (vlib_main_t * vm,
316 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
320 ip0 = vlib_buffer_get_current (b0);
322 /* Fixup the checksum and len fields in the GRE tunnel encap
323 * that was applied at the midchain node */
324 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
325 ip0->checksum = ip4_header_checksum (ip0);
329 gre46_fixup (vlib_main_t * vm,
330 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
332 tunnel_encap_decap_flags_t flags;
333 ip6_and_gre_header_t *ip0;
335 ip0 = vlib_buffer_get_current (b0);
336 flags = pointer_to_uword (data);
338 /* Fixup the payload length field in the GRE tunnel encap that was applied
339 * at the midchain node */
340 ip0->ip6.payload_length =
341 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
343 tunnel_encap_fixup_4o6 (flags, (ip4_header_t *) (ip0 + 1), &ip0->ip6);
347 gre66_fixup (vlib_main_t * vm,
348 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
350 tunnel_encap_decap_flags_t flags;
351 ip6_and_gre_header_t *ip0;
353 ip0 = vlib_buffer_get_current (b0);
354 flags = pointer_to_uword (data);
356 /* Fixup the payload length field in the GRE tunnel encap that was applied
357 * at the midchain node */
358 ip0->ip6.payload_length =
359 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
361 tunnel_encap_fixup_6o6 (flags, (ip6_header_t *) (ip0 + 1), &ip0->ip6);
365 grex6_fixup (vlib_main_t * vm,
366 const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
368 ip6_and_gre_header_t *ip0;
370 ip0 = vlib_buffer_get_current (b0);
372 /* Fixup the payload length field in the GRE tunnel encap that was applied
373 * at the midchain node */
374 ip0->ip6.payload_length =
375 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
380 * return the appropriate fixup function given the overlay (link-type) and
381 * underlay (fproto) combination
383 static adj_midchain_fixup_t
384 gre_get_fixup (fib_protocol_t fproto, vnet_link_t lt)
386 if (fproto == FIB_PROTOCOL_IP6 && lt == VNET_LINK_IP6)
387 return (gre66_fixup);
388 if (fproto == FIB_PROTOCOL_IP6 && lt == VNET_LINK_IP4)
389 return (gre46_fixup);
390 if (fproto == FIB_PROTOCOL_IP4 && lt == VNET_LINK_IP6)
391 return (gre64_fixup);
392 if (fproto == FIB_PROTOCOL_IP4 && lt == VNET_LINK_IP4)
393 return (gre44_fixup);
394 if (fproto == FIB_PROTOCOL_IP6 && lt == VNET_LINK_ETHERNET)
395 return (grex6_fixup);
396 if (fproto == FIB_PROTOCOL_IP4 && lt == VNET_LINK_ETHERNET)
397 return (grex4_fixup);
400 return (gre44_fixup);
404 gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
406 gre_main_t *gm = &gre_main;
411 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
412 t = pool_elt_at_index (gm->tunnels, ti);
413 af = ADJ_FLAG_MIDCHAIN_IP_STACK;
415 if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
416 af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
418 adj_nbr_midchain_update_rewrite
419 (ai, gre_get_fixup (t->tunnel_dst.fp_proto,
420 adj_get_link_type (ai)),
421 uword_to_pointer (t->flags, void *), af,
422 gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai),
423 &t->tunnel_dst.fp_addr));
425 gre_tunnel_stack (ai);
429 mgre_mk_complete_walk (adj_index_t ai, void *data)
431 mgre_walk_ctx_t *ctx = data;
433 adj_nbr_midchain_update_rewrite
434 (ai, gre_get_fixup (ctx->t->tunnel_dst.fp_proto,
435 adj_get_link_type (ai)),
436 uword_to_pointer (ctx->t->flags, void *),
437 ADJ_FLAG_MIDCHAIN_IP_STACK,
438 gre_build_rewrite (vnet_get_main (),
440 adj_get_link_type (ai),
441 &teib_entry_get_nh (ctx->ne)->fp_addr));
443 teib_entry_adj_stack (ctx->ne, ai);
445 return (ADJ_WALK_RC_CONTINUE);
449 mgre_mk_incomplete_walk (adj_index_t ai, void *data)
451 gre_tunnel_t *t = data;
453 adj_nbr_midchain_update_rewrite (ai, gre_get_fixup (t->tunnel_dst.fp_proto,
454 adj_get_link_type (ai)),
455 NULL, ADJ_FLAG_NONE, NULL);
457 adj_midchain_delegate_unstack (ai);
459 return (ADJ_WALK_RC_CONTINUE);
463 mgre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
465 gre_main_t *gm = &gre_main;
472 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
473 t = pool_elt_at_index (gm->tunnels, ti);
475 ne = teib_entry_find (sw_if_index, &adj->sub_type.nbr.next_hop);
478 // no NHRP entry to provide the next-hop
481 mgre_walk_ctx_t ctx = {
485 adj_nbr_walk_nh (sw_if_index,
487 &adj->sub_type.nbr.next_hop, mgre_mk_complete_walk, &ctx);
489 #endif /* CLIB_MARCH_VARIANT */
493 GRE_ENCAP_NEXT_L2_MIDCHAIN,
498 * @brief TX function. Only called for L2 payload including TEB or ERSPAN.
499 * L3 traffic uses the adj-midchains.
501 static_always_inline u32
502 gre_encap_inline (vlib_main_t * vm,
503 vlib_node_runtime_t * node,
504 vlib_frame_t * frame, gre_tunnel_type_t type)
506 gre_main_t *gm = &gre_main;
507 u32 *from, n_left_from;
508 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
509 u32 sw_if_index[2] = { ~0, ~0 };
510 const gre_tunnel_t *gt[2] = { 0 };
511 adj_index_t adj_index[2] = { ADJ_INDEX_INVALID, ADJ_INDEX_INVALID };
513 from = vlib_frame_vector_args (frame);
514 n_left_from = frame->n_vectors;
515 vlib_get_buffers (vm, from, bufs, n_left_from);
517 while (n_left_from >= 2)
521 (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
523 const vnet_hw_interface_t *hi;
524 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
525 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
526 gt[0] = &gm->tunnels[hi->dev_instance];
527 adj_index[0] = gt[0]->l2_adj_index;
530 (sw_if_index[1] != vnet_buffer (b[1])->sw_if_index[VLIB_TX]))
532 const vnet_hw_interface_t *hi;
533 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
534 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[1]);
535 gt[1] = &gm->tunnels[hi->dev_instance];
536 adj_index[1] = gt[1]->l2_adj_index;
539 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
540 vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = adj_index[1];
542 if (type == GRE_TUNNEL_TYPE_ERSPAN)
544 /* Encap GRE seq# and ERSPAN type II header */
548 vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
549 h0 = vlib_buffer_get_current (b[0]);
550 seq_num = clib_atomic_fetch_add (>[0]->gre_sn->seq_num, 1);
551 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
552 h0->seq_num = clib_host_to_net_u32 (seq_num);
554 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
556 if (type == GRE_TUNNEL_TYPE_ERSPAN)
558 /* Encap GRE seq# and ERSPAN type II header */
562 vlib_buffer_advance (b[1], -sizeof (erspan_t2_t));
563 h0 = vlib_buffer_get_current (b[1]);
564 seq_num = clib_atomic_fetch_add (>[1]->gre_sn->seq_num, 1);
565 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
566 h0->seq_num = clib_host_to_net_u32 (seq_num);
568 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[1]->session_id);
571 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
573 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
575 tr->tunnel_id = gt[0] - gm->tunnels;
576 tr->src = gt[0]->tunnel_src;
577 tr->dst = gt[0]->tunnel_dst.fp_addr;
578 tr->length = vlib_buffer_length_in_chain (vm, b[0]);
580 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
582 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
584 tr->tunnel_id = gt[1] - gm->tunnels;
585 tr->src = gt[1]->tunnel_src;
586 tr->dst = gt[1]->tunnel_dst.fp_addr;
587 tr->length = vlib_buffer_length_in_chain (vm, b[1]);
594 while (n_left_from >= 1)
598 (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
600 const vnet_hw_interface_t *hi;
601 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
602 hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
603 gt[0] = &gm->tunnels[hi->dev_instance];
604 adj_index[0] = gt[0]->l2_adj_index;
607 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
609 if (type == GRE_TUNNEL_TYPE_ERSPAN)
611 /* Encap GRE seq# and ERSPAN type II header */
615 ASSERT (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN);
616 vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
617 h0 = vlib_buffer_get_current (b[0]);
618 seq_num = clib_atomic_fetch_add (>[0]->gre_sn->seq_num, 1);
619 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
620 h0->seq_num = clib_host_to_net_u32 (seq_num);
622 h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
625 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
627 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
629 tr->tunnel_id = gt[0] - gm->tunnels;
630 tr->src = gt[0]->tunnel_src;
631 tr->dst = gt[0]->tunnel_dst.fp_addr;
632 tr->length = vlib_buffer_length_in_chain (vm, b[0]);
639 vlib_buffer_enqueue_to_single_next (vm, node, from,
640 GRE_ENCAP_NEXT_L2_MIDCHAIN,
643 vlib_node_increment_counter (vm, node->node_index,
644 GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
646 return frame->n_vectors;
649 static char *gre_error_strings[] = {
650 #define gre_error(n,s) s,
655 VLIB_NODE_FN (gre_teb_encap_node) (vlib_main_t * vm,
656 vlib_node_runtime_t * node,
657 vlib_frame_t * frame)
659 return (gre_encap_inline (vm, node, frame, GRE_TUNNEL_TYPE_TEB));
662 VLIB_NODE_FN (gre_erspan_encap_node) (vlib_main_t * vm,
663 vlib_node_runtime_t * node,
664 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,
725 ip46_address_t * src, ip46_address_t * dst, u8 * is_l2)
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,
759 VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
761 .format_header = format_gre_header_with_length,
762 .unformat_header = unformat_gre_header,
763 .build_rewrite = gre_build_rewrite,
764 .update_adjacency = gre_update_adj,
765 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
768 VNET_HW_INTERFACE_CLASS (mgre_hw_interface_class) = {
770 .format_header = format_gre_header_with_length,
771 .unformat_header = unformat_gre_header,
772 .build_rewrite = gre_build_rewrite,
773 .update_adjacency = mgre_update_adj,
774 .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
777 #endif /* CLIB_MARCH_VARIANT */
780 add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name)
782 gre_protocol_info_t *pi;
785 vec_add2 (gm->protocol_infos, pi, 1);
786 i = pi - gm->protocol_infos;
788 pi->name = protocol_name;
789 pi->protocol = protocol;
790 pi->next_index = pi->node_index = ~0;
792 hash_set (gm->protocol_info_by_protocol, protocol, i);
793 hash_set_mem (gm->protocol_info_by_name, pi->name, i);
796 static clib_error_t *
797 gre_init (vlib_main_t * vm)
799 gre_main_t *gm = &gre_main;
801 ip_main_t *im = &ip_main;
802 ip_protocol_info_t *pi;
804 clib_memset (gm, 0, sizeof (gm[0]));
806 gm->vnet_main = vnet_get_main ();
808 if ((error = vlib_call_init_function (vm, ip_main_init)))
811 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
814 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
817 /* Set up the ip packet generator */
818 pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
819 pi->format_header = format_gre_header;
820 pi->unformat_pg_edit = unformat_pg_gre_header;
822 gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
823 gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
825 hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword));
827 hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword));
829 hash_create_mem (0, sizeof (gre_sn_key_t), sizeof (uword));
831 #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
834 return vlib_call_init_function (vm, gre_input_init);
837 VLIB_INIT_FUNCTION (gre_init);
840 * fd.io coding-style-patch-verification: ON
843 * eval: (c-set-style "gnu")