gre: Multi-point interfaces
[vpp.git] / src / vnet / gre / gre.c
1 /*
2  * gre.c: gre
3  *
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:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/gre/gre.h>
20 #include <vnet/adj/adj_midchain.h>
21 #include <vnet/nhrp/nhrp.h>
22
23 extern gre_main_t gre_main;
24
25 #ifndef CLIB_MARCH_VARIANT
26 gre_main_t gre_main;
27
28 typedef struct
29 {
30   union
31   {
32     ip4_and_gre_header_t ip4_and_gre;
33     u64 as_u64[3];
34   };
35 } ip4_and_gre_union_t;
36
37 typedef struct
38 {
39   union
40   {
41     ip6_and_gre_header_t ip6_and_gre;
42     u64 as_u64[3];
43   };
44 } ip6_and_gre_union_t;
45 #endif /* CLIB_MARCH_VARIANT */
46
47
48 /* Packet trace structure */
49 typedef struct
50 {
51   /* Tunnel-id / index in tunnel vector */
52   u32 tunnel_id;
53
54   /* pkt length */
55   u32 length;
56
57   /* tunnel ip addresses */
58   ip46_address_t src;
59   ip46_address_t dst;
60 } gre_tx_trace_t;
61
62 extern u8 *format_gre_tx_trace (u8 * s, va_list * args);
63
64 #ifndef CLIB_MARCH_VARIANT
65 u8 *
66 format_gre_tx_trace (u8 * s, va_list * args)
67 {
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 *);
71
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);
76   return s;
77 }
78
79 u8 *
80 format_gre_protocol (u8 * s, va_list * args)
81 {
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);
85
86   if (pi)
87     s = format (s, "%s", pi->name);
88   else
89     s = format (s, "0x%04x", p);
90
91   return s;
92 }
93
94 u8 *
95 format_gre_header_with_length (u8 * s, va_list * args)
96 {
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;
102
103   header_bytes = sizeof (h[0]);
104   if (max_header_bytes != 0 && header_bytes > max_header_bytes)
105     return format (s, "gre header truncated");
106
107   indent = format_get_indent (s);
108
109   s = format (s, "GRE %U", format_gre_protocol, p);
110
111   if (max_header_bytes != 0 && header_bytes < max_header_bytes)
112     {
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);
120     }
121
122   return s;
123 }
124
125 u8 *
126 format_gre_header (u8 * s, va_list * args)
127 {
128   gre_header_t *h = va_arg (*args, gre_header_t *);
129   return format (s, "%U", format_gre_header_with_length, h, 0);
130 }
131
132 /* Returns gre protocol as an int in host byte order. */
133 uword
134 unformat_gre_protocol_host_byte_order (unformat_input_t * input,
135                                        va_list * args)
136 {
137   u16 *result = va_arg (*args, u16 *);
138   gre_main_t *gm = &gre_main;
139   int i;
140
141   /* Named type. */
142   if (unformat_user (input, unformat_vlib_number_by_name,
143                      gm->protocol_info_by_name, &i))
144     {
145       gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i);
146       *result = pi->protocol;
147       return 1;
148     }
149
150   return 0;
151 }
152
153 uword
154 unformat_gre_protocol_net_byte_order (unformat_input_t * input,
155                                       va_list * args)
156 {
157   u16 *result = va_arg (*args, u16 *);
158   if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result))
159     return 0;
160   *result = clib_host_to_net_u16 ((u16) * result);
161   return 1;
162 }
163
164 uword
165 unformat_gre_header (unformat_input_t * input, va_list * args)
166 {
167   u8 **result = va_arg (*args, u8 **);
168   gre_header_t _h, *h = &_h;
169   u16 p;
170
171   if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p))
172     return 0;
173
174   h->protocol = clib_host_to_net_u16 (p);
175
176   /* Add header to result. */
177   {
178     void *p;
179     u32 n_bytes = sizeof (h[0]);
180
181     vec_add2 (*result, p, n_bytes);
182     clib_memcpy (p, h, n_bytes);
183   }
184
185   return 1;
186 }
187
188 static int
189 gre_proto_from_vnet_link (vnet_link_t link)
190 {
191   switch (link)
192     {
193     case VNET_LINK_IP4:
194       return (GRE_PROTOCOL_ip4);
195     case VNET_LINK_IP6:
196       return (GRE_PROTOCOL_ip6);
197     case VNET_LINK_MPLS:
198       return (GRE_PROTOCOL_mpls_unicast);
199     case VNET_LINK_ETHERNET:
200       return (GRE_PROTOCOL_teb);
201     case VNET_LINK_ARP:
202       return (GRE_PROTOCOL_arp);
203     case VNET_LINK_NSH:
204       ASSERT (0);
205       break;
206     }
207   ASSERT (0);
208   return (GRE_PROTOCOL_ip4);
209 }
210
211 static u8 *
212 gre_build_rewrite (vnet_main_t * vnm,
213                    u32 sw_if_index,
214                    vnet_link_t link_type, const void *dst_address)
215 {
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;
220   gre_header_t *gre;
221   u8 *rewrite = NULL;
222   gre_tunnel_t *t;
223   u32 ti;
224   u8 is_ipv6;
225
226   dst = dst_address;
227   ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
228
229   if (~0 == ti)
230     /* not one of ours */
231     return (0);
232
233   t = pool_elt_at_index (gm->tunnels, ti);
234
235   is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
236
237   if (!is_ipv6)
238     {
239       vec_validate (rewrite, sizeof (*h4) - 1);
240       h4 = (ip4_and_gre_header_t *) rewrite;
241       gre = &h4->gre;
242       h4->ip4.ip_version_and_header_length = 0x45;
243       h4->ip4.ttl = 254;
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);
249     }
250   else
251     {
252       vec_validate (rewrite, sizeof (*h6) - 1);
253       h6 = (ip6_and_gre_header_t *) rewrite;
254       gre = &h6->gre;
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];
264     }
265
266   if (PREDICT_FALSE (t->type == GRE_TUNNEL_TYPE_ERSPAN))
267     {
268       gre->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_erspan);
269       gre->flags_and_version = clib_host_to_net_u16 (GRE_FLAGS_SEQUENCE);
270     }
271   else
272     gre->protocol =
273       clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
274
275   return (rewrite);
276 }
277
278 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
279
280 static void
281 gre4_fixup (vlib_main_t * vm,
282             const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
283 {
284   ip4_header_t *ip0;
285
286   ip0 = vlib_buffer_get_current (b0);
287
288   /* Fixup the checksum and len fields in the GRE tunnel encap
289    * that was applied at the midchain node */
290   ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
291   ip0->checksum = ip4_header_checksum (ip0);
292 }
293
294 static void
295 gre6_fixup (vlib_main_t * vm,
296             const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
297 {
298   ip6_header_t *ip0;
299
300   ip0 = vlib_buffer_get_current (b0);
301
302   /* Fixup the payload length field in the GRE tunnel encap that was applied
303    * at the midchain node */
304   ip0->payload_length =
305     clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
306                           sizeof (*ip0));
307 }
308
309 void
310 gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
311 {
312   gre_main_t *gm = &gre_main;
313   gre_tunnel_t *t;
314   adj_flags_t af;
315   u8 is_ipv6;
316   u32 ti;
317
318   ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
319   t = pool_elt_at_index (gm->tunnels, ti);
320   is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
321   af = ADJ_FLAG_MIDCHAIN_IP_STACK;
322
323   if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
324     af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
325
326   adj_nbr_midchain_update_rewrite
327     (ai, !is_ipv6 ? gre4_fixup : gre6_fixup, NULL, af,
328      gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai),
329                         &t->tunnel_dst.fp_addr));
330
331   gre_tunnel_stack (ai);
332 }
333
334 static adj_walk_rc_t
335 mgre_mk_complete_walk (adj_index_t ai, void *ctx)
336 {
337   nhrp_entry_adj_stack (ctx, ai);
338
339   return (ADJ_WALK_RC_CONTINUE);
340 }
341
342 void
343 mgre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
344 {
345   gre_main_t *gm = &gre_main;
346   ip_adjacency_t *adj;
347   nhrp_entry_t *ne;
348   gre_tunnel_t *t;
349   adj_flags_t af;
350   u8 is_ipv6;
351   u32 ti;
352
353   adj = adj_get (ai);
354   ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
355   t = pool_elt_at_index (gm->tunnels, ti);
356   is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
357   af = ADJ_FLAG_MIDCHAIN_IP_STACK;
358
359   adj_nbr_midchain_update_rewrite
360     (ai, !is_ipv6 ? gre4_fixup : gre6_fixup, NULL, af,
361      gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai),
362                         &adj->sub_type.nbr.next_hop));
363
364   ne = nhrp_entry_find (sw_if_index, &adj->sub_type.nbr.next_hop);
365
366   if (NULL == ne)
367     // no NHRP entry to provide the next-hop
368     return;
369
370   adj_nbr_walk_nh (sw_if_index, t->tunnel_dst.fp_proto,
371                    &adj->sub_type.nbr.next_hop, mgre_mk_complete_walk, ne);
372 }
373 #endif /* CLIB_MARCH_VARIANT */
374
375 typedef enum
376 {
377   GRE_ENCAP_NEXT_L2_MIDCHAIN,
378   GRE_ENCAP_N_NEXT,
379 } gre_encap_next_t;
380
381 /**
382  * @brief TX function. Only called for L2 payload including TEB or ERSPAN.
383  *        L3 traffic uses the adj-midchains.
384  */
385 VLIB_NODE_FN (gre_encap_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
386                                vlib_frame_t * frame)
387 {
388   gre_main_t *gm = &gre_main;
389   u32 *from, n_left_from;
390   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
391   u32 sw_if_index[2] = { ~0, ~0 };
392   const gre_tunnel_t *gt[2] = { 0 };
393   adj_index_t adj_index[2] = { ADJ_INDEX_INVALID, ADJ_INDEX_INVALID };
394
395   from = vlib_frame_vector_args (frame);
396   n_left_from = frame->n_vectors;
397   vlib_get_buffers (vm, from, bufs, n_left_from);
398
399   while (n_left_from >= 2)
400     {
401
402       if (PREDICT_FALSE
403           (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
404         {
405           const vnet_hw_interface_t *hi;
406           sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
407           hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
408           gt[0] = &gm->tunnels[hi->dev_instance];
409           adj_index[0] = gt[0]->l2_adj_index;
410         }
411       if (PREDICT_FALSE
412           (sw_if_index[1] != vnet_buffer (b[1])->sw_if_index[VLIB_TX]))
413         {
414           const vnet_hw_interface_t *hi;
415           sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
416           hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[1]);
417           gt[1] = &gm->tunnels[hi->dev_instance];
418           adj_index[1] = gt[1]->l2_adj_index;
419         }
420
421       vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
422       vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = adj_index[1];
423
424       if (PREDICT_FALSE (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN))
425         {
426           /* Encap GRE seq# and ERSPAN type II header */
427           erspan_t2_t *h0;
428           u32 seq_num;
429           u64 hdr;
430           vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
431           h0 = vlib_buffer_get_current (b[0]);
432           seq_num = clib_atomic_fetch_add (&gt[0]->gre_sn->seq_num, 1);
433           hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
434           h0->seq_num = clib_host_to_net_u32 (seq_num);
435           h0->t2_u64 = hdr;
436           h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
437         }
438       if (PREDICT_FALSE (gt[1]->type == GRE_TUNNEL_TYPE_ERSPAN))
439         {
440           /* Encap GRE seq# and ERSPAN type II header */
441           erspan_t2_t *h0;
442           u32 seq_num;
443           u64 hdr;
444           vlib_buffer_advance (b[1], -sizeof (erspan_t2_t));
445           h0 = vlib_buffer_get_current (b[1]);
446           seq_num = clib_atomic_fetch_add (&gt[1]->gre_sn->seq_num, 1);
447           hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
448           h0->seq_num = clib_host_to_net_u32 (seq_num);
449           h0->t2_u64 = hdr;
450           h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[1]->session_id);
451         }
452
453       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
454         {
455           gre_tx_trace_t *tr = vlib_add_trace (vm, node,
456                                                b[0], sizeof (*tr));
457           tr->tunnel_id = gt[0] - gm->tunnels;
458           tr->src = gt[0]->tunnel_src;
459           tr->dst = gt[0]->tunnel_dst.fp_addr;
460           tr->length = vlib_buffer_length_in_chain (vm, b[0]);
461         }
462       if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
463         {
464           gre_tx_trace_t *tr = vlib_add_trace (vm, node,
465                                                b[1], sizeof (*tr));
466           tr->tunnel_id = gt[1] - gm->tunnels;
467           tr->src = gt[1]->tunnel_src;
468           tr->dst = gt[1]->tunnel_dst.fp_addr;
469           tr->length = vlib_buffer_length_in_chain (vm, b[1]);
470         }
471
472       b += 2;
473       n_left_from -= 2;
474     }
475
476   while (n_left_from >= 1)
477     {
478
479       if (PREDICT_FALSE
480           (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
481         {
482           const vnet_hw_interface_t *hi;
483           sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
484           hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
485           gt[0] = &gm->tunnels[hi->dev_instance];
486           adj_index[0] = gt[0]->l2_adj_index;
487         }
488
489       vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
490
491       if (PREDICT_FALSE (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN))
492         {
493           /* Encap GRE seq# and ERSPAN type II header */
494           erspan_t2_t *h0;
495           u32 seq_num;
496           u64 hdr;
497           vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
498           h0 = vlib_buffer_get_current (b[0]);
499           seq_num = clib_atomic_fetch_add (&gt[0]->gre_sn->seq_num, 1);
500           hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
501           h0->seq_num = clib_host_to_net_u32 (seq_num);
502           h0->t2_u64 = hdr;
503           h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
504         }
505
506       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
507         {
508           gre_tx_trace_t *tr = vlib_add_trace (vm, node,
509                                                b[0], sizeof (*tr));
510           tr->tunnel_id = gt[0] - gm->tunnels;
511           tr->src = gt[0]->tunnel_src;
512           tr->dst = gt[0]->tunnel_dst.fp_addr;
513           tr->length = vlib_buffer_length_in_chain (vm, b[0]);
514         }
515
516       b += 1;
517       n_left_from -= 1;
518     }
519
520   vlib_buffer_enqueue_to_single_next (vm, node, from,
521                                       GRE_ENCAP_NEXT_L2_MIDCHAIN,
522                                       frame->n_vectors);
523
524   vlib_node_increment_counter (vm, node->node_index,
525                                GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
526
527   return frame->n_vectors;
528 }
529
530 static char *gre_error_strings[] = {
531 #define gre_error(n,s) s,
532 #include "error.def"
533 #undef gre_error
534 };
535
536 /* *INDENT-OFF* */
537 VLIB_REGISTER_NODE (gre_encap_node) =
538 {
539   .name = "gre-encap",
540   .vector_size = sizeof (u32),
541   .format_trace = format_gre_tx_trace,
542   .type = VLIB_NODE_TYPE_INTERNAL,
543   .n_errors = GRE_N_ERROR,
544   .error_strings = gre_error_strings,
545   .n_next_nodes = GRE_ENCAP_N_NEXT,
546   .next_nodes = {
547     [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
548   },
549 };
550 /* *INDENT-ON* */
551
552 #ifndef CLIB_MARCH_VARIANT
553 static u8 *
554 format_gre_tunnel_name (u8 * s, va_list * args)
555 {
556   u32 dev_instance = va_arg (*args, u32);
557   gre_main_t *gm = &gre_main;
558   gre_tunnel_t *t;
559
560   if (dev_instance >= vec_len (gm->tunnels))
561     return format (s, "<improperly-referenced>");
562
563   t = pool_elt_at_index (gm->tunnels, dev_instance);
564   return format (s, "gre%d", t->user_instance);
565 }
566
567 static u8 *
568 format_gre_device (u8 * s, va_list * args)
569 {
570   u32 dev_instance = va_arg (*args, u32);
571   CLIB_UNUSED (int verbose) = va_arg (*args, int);
572
573   s = format (s, "GRE tunnel: id %d\n", dev_instance);
574   return s;
575 }
576
577 static int
578 gre_tunnel_desc (u32 sw_if_index,
579                  ip46_address_t * src, ip46_address_t * dst, u8 * is_l2)
580 {
581   gre_main_t *gm = &gre_main;
582   gre_tunnel_t *t;
583   u32 ti;
584
585   ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
586
587   if (~0 == ti)
588     /* not one of ours */
589     return -1;
590
591   t = pool_elt_at_index (gm->tunnels, ti);
592
593   *src = t->tunnel_src;
594   *dst = t->tunnel_dst.fp_addr;
595   *is_l2 = t->type == GRE_TUNNEL_TYPE_TEB;
596
597   return (0);
598 }
599
600 /* *INDENT-OFF* */
601 VNET_DEVICE_CLASS (gre_device_class) = {
602   .name = "GRE tunnel device",
603   .format_device_name = format_gre_tunnel_name,
604   .format_device = format_gre_device,
605   .format_tx_trace = format_gre_tx_trace,
606   .admin_up_down_function = gre_interface_admin_up_down,
607   .ip_tun_desc = gre_tunnel_desc,
608 #ifdef SOON
609   .clear counter = 0;
610 #endif
611 };
612
613 VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
614   .name = "GRE",
615   .format_header = format_gre_header_with_length,
616   .unformat_header = unformat_gre_header,
617   .build_rewrite = gre_build_rewrite,
618   .update_adjacency = gre_update_adj,
619   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
620 };
621
622 VNET_HW_INTERFACE_CLASS (mgre_hw_interface_class) = {
623   .name = "mGRE",
624   .format_header = format_gre_header_with_length,
625   .unformat_header = unformat_gre_header,
626   .build_rewrite = gre_build_rewrite,
627   .update_adjacency = mgre_update_adj,
628   .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
629 };
630 /* *INDENT-ON* */
631 #endif /* CLIB_MARCH_VARIANT */
632
633 static void
634 add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name)
635 {
636   gre_protocol_info_t *pi;
637   u32 i;
638
639   vec_add2 (gm->protocol_infos, pi, 1);
640   i = pi - gm->protocol_infos;
641
642   pi->name = protocol_name;
643   pi->protocol = protocol;
644   pi->next_index = pi->node_index = ~0;
645
646   hash_set (gm->protocol_info_by_protocol, protocol, i);
647   hash_set_mem (gm->protocol_info_by_name, pi->name, i);
648 }
649
650 static clib_error_t *
651 gre_init (vlib_main_t * vm)
652 {
653   gre_main_t *gm = &gre_main;
654   clib_error_t *error;
655   ip_main_t *im = &ip_main;
656   ip_protocol_info_t *pi;
657
658   clib_memset (gm, 0, sizeof (gm[0]));
659   gm->vlib_main = vm;
660   gm->vnet_main = vnet_get_main ();
661
662   if ((error = vlib_call_init_function (vm, ip_main_init)))
663     return error;
664
665   if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
666     return error;
667
668   if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
669     return error;
670
671   /* Set up the ip packet generator */
672   pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
673   pi->format_header = format_gre_header;
674   pi->unformat_pg_edit = unformat_pg_gre_header;
675
676   gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
677   gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
678   gm->tunnel_by_key4 =
679     hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword));
680   gm->tunnel_by_key6 =
681     hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword));
682   gm->seq_num_by_key =
683     hash_create_mem (0, sizeof (gre_sn_key_t), sizeof (uword));
684
685 #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
686   foreach_gre_protocol
687 #undef _
688     return vlib_call_init_function (vm, gre_input_init);
689 }
690
691 VLIB_INIT_FUNCTION (gre_init);
692
693 /*
694  * fd.io coding-style-patch-verification: ON
695  *
696  * Local Variables:
697  * eval: (c-set-style "gnu")
698  * End:
699  */