dc735e6a77b9f83ae147c5eb107b80cc4bfbcfa6
[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/tunnel/tunnel_dp.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 static void
279 gre44_fixup (vlib_main_t * vm,
280              const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
281 {
282   tunnel_encap_decap_flags_t flags;
283   ip4_and_gre_header_t *ip0;
284
285   ip0 = vlib_buffer_get_current (b0);
286   flags = pointer_to_uword (data);
287
288   /* Fixup the checksum and len fields in the GRE tunnel encap
289    * that was applied at the midchain node */
290   ip0->ip4.length =
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);
294 }
295
296 static void
297 gre64_fixup (vlib_main_t * vm,
298              const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
299 {
300   tunnel_encap_decap_flags_t flags;
301   ip4_and_gre_header_t *ip0;
302
303   ip0 = vlib_buffer_get_current (b0);
304   flags = pointer_to_uword (data);
305
306   /* Fixup the checksum and len fields in the GRE tunnel encap
307    * that was applied at the midchain node */
308   ip0->ip4.length =
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);
312 }
313
314 static void
315 grex4_fixup (vlib_main_t * vm,
316              const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
317 {
318   ip4_header_t *ip0;
319
320   ip0 = vlib_buffer_get_current (b0);
321
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);
326 }
327
328 static void
329 gre46_fixup (vlib_main_t * vm,
330              const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
331 {
332   tunnel_encap_decap_flags_t flags;
333   ip6_and_gre_header_t *ip0;
334
335   ip0 = vlib_buffer_get_current (b0);
336   flags = pointer_to_uword (data);
337
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) -
342                           sizeof (ip0->ip6));
343   tunnel_encap_fixup_4o6 (flags, b0, (ip4_header_t *) (ip0 + 1), &ip0->ip6);
344 }
345
346 static void
347 gre66_fixup (vlib_main_t * vm,
348              const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
349 {
350   tunnel_encap_decap_flags_t flags;
351   ip6_and_gre_header_t *ip0;
352
353   ip0 = vlib_buffer_get_current (b0);
354   flags = pointer_to_uword (data);
355
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) -
360                           sizeof (ip0->ip6));
361   tunnel_encap_fixup_6o6 (flags, (ip6_header_t *) (ip0 + 1), &ip0->ip6);
362 }
363
364 static void
365 grex6_fixup (vlib_main_t * vm,
366              const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
367 {
368   ip6_and_gre_header_t *ip0;
369
370   ip0 = vlib_buffer_get_current (b0);
371
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) -
376                           sizeof (ip0->ip6));
377 }
378
379 /**
380  * return the appropriate fixup function given the overlay (link-type) and
381  * underlay (fproto) combination
382  */
383 static adj_midchain_fixup_t
384 gre_get_fixup (fib_protocol_t fproto, vnet_link_t lt)
385 {
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)
395     return (grex6_fixup);
396   if (fproto == FIB_PROTOCOL_IP4)
397     return (grex4_fixup);
398
399   ASSERT (0);
400   return (gre44_fixup);
401 }
402
403 void
404 gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
405 {
406   gre_main_t *gm = &gre_main;
407   gre_tunnel_t *t;
408   adj_flags_t af;
409   u32 ti;
410
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_NONE;
414
415   /*
416    * the user has not requested that the load-balancing be based on
417    * a flow hash of the inner packet. so use the stacking to choose
418    * a path.
419    */
420   if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH))
421     af |= ADJ_FLAG_MIDCHAIN_IP_STACK;
422
423   adj_nbr_midchain_update_rewrite
424     (ai, gre_get_fixup (t->tunnel_dst.fp_proto,
425                         adj_get_link_type (ai)),
426      uword_to_pointer (t->flags, void *), af,
427      gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai),
428                         &t->tunnel_dst.fp_addr));
429
430   gre_tunnel_stack (ai);
431 }
432
433 adj_walk_rc_t
434 mgre_mk_complete_walk (adj_index_t ai, void *data)
435 {
436   mgre_walk_ctx_t *ctx = data;
437   adj_flags_t af;
438
439   af = ADJ_FLAG_NONE;
440
441   /*
442    * the user has not requested that the load-balancing be based on
443    * a flow hash of the inner packet. so use the stacking to choose
444    * a path.
445    */
446   if (!(ctx->t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH))
447     af |= ADJ_FLAG_MIDCHAIN_IP_STACK;
448
449   adj_nbr_midchain_update_rewrite
450     (ai, gre_get_fixup (ctx->t->tunnel_dst.fp_proto,
451                         adj_get_link_type (ai)),
452      uword_to_pointer (ctx->t->flags, void *),
453      af,
454      gre_build_rewrite (vnet_get_main (),
455                         ctx->t->sw_if_index,
456                         adj_get_link_type (ai),
457                         &teib_entry_get_nh (ctx->ne)->fp_addr));
458
459   teib_entry_adj_stack (ctx->ne, ai);
460
461   return (ADJ_WALK_RC_CONTINUE);
462 }
463
464 adj_walk_rc_t
465 mgre_mk_incomplete_walk (adj_index_t ai, void *data)
466 {
467   gre_tunnel_t *t = data;
468
469   adj_nbr_midchain_update_rewrite (ai, gre_get_fixup (t->tunnel_dst.fp_proto,
470                                                       adj_get_link_type (ai)),
471                                    NULL, ADJ_FLAG_NONE, NULL);
472
473   adj_midchain_delegate_unstack (ai);
474
475   return (ADJ_WALK_RC_CONTINUE);
476 }
477
478 void
479 mgre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
480 {
481   gre_main_t *gm = &gre_main;
482   ip_adjacency_t *adj;
483   teib_entry_t *ne;
484   gre_tunnel_t *t;
485   u32 ti;
486
487   adj = adj_get (ai);
488   ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
489   t = pool_elt_at_index (gm->tunnels, ti);
490
491   ne = teib_entry_find_46 (sw_if_index,
492                            adj->ia_nh_proto, &adj->sub_type.nbr.next_hop);
493
494   if (NULL == ne)
495     {
496       // no TEIB entry to provide the next-hop
497       adj_nbr_midchain_update_rewrite (
498         ai, gre_get_fixup (t->tunnel_dst.fp_proto, adj_get_link_type (ai)),
499         uword_to_pointer (t->flags, void *), ADJ_FLAG_NONE, NULL);
500       return;
501     }
502
503   mgre_walk_ctx_t ctx = {
504     .t = t,
505     .ne = ne
506   };
507   adj_nbr_walk_nh (sw_if_index,
508                    adj->ia_nh_proto,
509                    &adj->sub_type.nbr.next_hop, mgre_mk_complete_walk, &ctx);
510 }
511 #endif /* CLIB_MARCH_VARIANT */
512
513 typedef enum
514 {
515   GRE_ENCAP_NEXT_L2_MIDCHAIN,
516   GRE_ENCAP_N_NEXT,
517 } gre_encap_next_t;
518
519 /**
520  * @brief TX function. Only called for L2 payload including TEB or ERSPAN.
521  *        L3 traffic uses the adj-midchains.
522  */
523 static_always_inline u32
524 gre_encap_inline (vlib_main_t * vm,
525                   vlib_node_runtime_t * node,
526                   vlib_frame_t * frame, gre_tunnel_type_t type)
527 {
528   gre_main_t *gm = &gre_main;
529   u32 *from, n_left_from;
530   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
531   u32 sw_if_index[2] = { ~0, ~0 };
532   const gre_tunnel_t *gt[2] = { 0 };
533   adj_index_t adj_index[2] = { ADJ_INDEX_INVALID, ADJ_INDEX_INVALID };
534
535   from = vlib_frame_vector_args (frame);
536   n_left_from = frame->n_vectors;
537   vlib_get_buffers (vm, from, bufs, n_left_from);
538
539   while (n_left_from >= 2)
540     {
541
542       if (PREDICT_FALSE
543           (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
544         {
545           const vnet_hw_interface_t *hi;
546           sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
547           hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
548           gt[0] = &gm->tunnels[hi->dev_instance];
549           adj_index[0] = gt[0]->l2_adj_index;
550         }
551       if (PREDICT_FALSE
552           (sw_if_index[1] != vnet_buffer (b[1])->sw_if_index[VLIB_TX]))
553         {
554           const vnet_hw_interface_t *hi;
555           sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
556           hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[1]);
557           gt[1] = &gm->tunnels[hi->dev_instance];
558           adj_index[1] = gt[1]->l2_adj_index;
559         }
560
561       vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
562       vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = adj_index[1];
563
564       if (type == GRE_TUNNEL_TYPE_ERSPAN)
565         {
566           /* Encap GRE seq# and ERSPAN type II header */
567           erspan_t2_t *h0;
568           u32 seq_num;
569           u64 hdr;
570           vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
571           h0 = vlib_buffer_get_current (b[0]);
572           seq_num = clib_atomic_fetch_add (&gt[0]->gre_sn->seq_num, 1);
573           hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
574           h0->seq_num = clib_host_to_net_u32 (seq_num);
575           h0->t2_u64 = hdr;
576           h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
577         }
578       if (type == GRE_TUNNEL_TYPE_ERSPAN)
579         {
580           /* Encap GRE seq# and ERSPAN type II header */
581           erspan_t2_t *h0;
582           u32 seq_num;
583           u64 hdr;
584           vlib_buffer_advance (b[1], -sizeof (erspan_t2_t));
585           h0 = vlib_buffer_get_current (b[1]);
586           seq_num = clib_atomic_fetch_add (&gt[1]->gre_sn->seq_num, 1);
587           hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
588           h0->seq_num = clib_host_to_net_u32 (seq_num);
589           h0->t2_u64 = hdr;
590           h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[1]->session_id);
591         }
592
593       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
594         {
595           gre_tx_trace_t *tr = vlib_add_trace (vm, node,
596                                                b[0], sizeof (*tr));
597           tr->tunnel_id = gt[0] - gm->tunnels;
598           tr->src = gt[0]->tunnel_src;
599           tr->dst = gt[0]->tunnel_dst.fp_addr;
600           tr->length = vlib_buffer_length_in_chain (vm, b[0]);
601         }
602       if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
603         {
604           gre_tx_trace_t *tr = vlib_add_trace (vm, node,
605                                                b[1], sizeof (*tr));
606           tr->tunnel_id = gt[1] - gm->tunnels;
607           tr->src = gt[1]->tunnel_src;
608           tr->dst = gt[1]->tunnel_dst.fp_addr;
609           tr->length = vlib_buffer_length_in_chain (vm, b[1]);
610         }
611
612       b += 2;
613       n_left_from -= 2;
614     }
615
616   while (n_left_from >= 1)
617     {
618
619       if (PREDICT_FALSE
620           (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX]))
621         {
622           const vnet_hw_interface_t *hi;
623           sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
624           hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]);
625           gt[0] = &gm->tunnels[hi->dev_instance];
626           adj_index[0] = gt[0]->l2_adj_index;
627         }
628
629       vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0];
630
631       if (type == GRE_TUNNEL_TYPE_ERSPAN)
632         {
633           /* Encap GRE seq# and ERSPAN type II header */
634           erspan_t2_t *h0;
635           u32 seq_num;
636           u64 hdr;
637           ASSERT (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN);
638           vlib_buffer_advance (b[0], -sizeof (erspan_t2_t));
639           h0 = vlib_buffer_get_current (b[0]);
640           seq_num = clib_atomic_fetch_add (&gt[0]->gre_sn->seq_num, 1);
641           hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
642           h0->seq_num = clib_host_to_net_u32 (seq_num);
643           h0->t2_u64 = hdr;
644           h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id);
645         }
646
647       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
648         {
649           gre_tx_trace_t *tr = vlib_add_trace (vm, node,
650                                                b[0], sizeof (*tr));
651           tr->tunnel_id = gt[0] - gm->tunnels;
652           tr->src = gt[0]->tunnel_src;
653           tr->dst = gt[0]->tunnel_dst.fp_addr;
654           tr->length = vlib_buffer_length_in_chain (vm, b[0]);
655         }
656
657       b += 1;
658       n_left_from -= 1;
659     }
660
661   vlib_buffer_enqueue_to_single_next (vm, node, from,
662                                       GRE_ENCAP_NEXT_L2_MIDCHAIN,
663                                       frame->n_vectors);
664
665   vlib_node_increment_counter (vm, node->node_index,
666                                GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
667
668   return frame->n_vectors;
669 }
670
671 static char *gre_error_strings[] = {
672 #define gre_error(n,s) s,
673 #include "error.def"
674 #undef gre_error
675 };
676
677 VLIB_NODE_FN (gre_teb_encap_node) (vlib_main_t * vm,
678                                    vlib_node_runtime_t * node,
679                                    vlib_frame_t * frame)
680 {
681   return (gre_encap_inline (vm, node, frame, GRE_TUNNEL_TYPE_TEB));
682 }
683
684 VLIB_NODE_FN (gre_erspan_encap_node) (vlib_main_t * vm,
685                                       vlib_node_runtime_t * node,
686                                       vlib_frame_t * frame)
687 {
688   return (gre_encap_inline (vm, node, frame, GRE_TUNNEL_TYPE_ERSPAN));
689 }
690
691 /* *INDENT-OFF* */
692 VLIB_REGISTER_NODE (gre_teb_encap_node) =
693 {
694   .name = "gre-teb-encap",
695   .vector_size = sizeof (u32),
696   .format_trace = format_gre_tx_trace,
697   .type = VLIB_NODE_TYPE_INTERNAL,
698   .n_errors = GRE_N_ERROR,
699   .error_strings = gre_error_strings,
700   .n_next_nodes = GRE_ENCAP_N_NEXT,
701   .next_nodes = {
702     [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
703   },
704 };
705 VLIB_REGISTER_NODE (gre_erspan_encap_node) =
706 {
707   .name = "gre-erspan-encap",
708   .vector_size = sizeof (u32),
709   .format_trace = format_gre_tx_trace,
710   .type = VLIB_NODE_TYPE_INTERNAL,
711   .n_errors = GRE_N_ERROR,
712   .error_strings = gre_error_strings,
713   .n_next_nodes = GRE_ENCAP_N_NEXT,
714   .next_nodes = {
715     [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
716   },
717 };
718 /* *INDENT-ON* */
719
720 #ifndef CLIB_MARCH_VARIANT
721 static u8 *
722 format_gre_tunnel_name (u8 * s, va_list * args)
723 {
724   u32 dev_instance = va_arg (*args, u32);
725   gre_main_t *gm = &gre_main;
726   gre_tunnel_t *t;
727
728   if (dev_instance >= vec_len (gm->tunnels))
729     return format (s, "<improperly-referenced>");
730
731   t = pool_elt_at_index (gm->tunnels, dev_instance);
732   return format (s, "gre%d", t->user_instance);
733 }
734
735 static u8 *
736 format_gre_device (u8 * s, va_list * args)
737 {
738   u32 dev_instance = va_arg (*args, u32);
739   CLIB_UNUSED (int verbose) = va_arg (*args, int);
740
741   s = format (s, "GRE tunnel: id %d\n", dev_instance);
742   return s;
743 }
744
745 static int
746 gre_tunnel_desc (u32 sw_if_index,
747                  ip46_address_t * src, ip46_address_t * dst, u8 * is_l2)
748 {
749   gre_main_t *gm = &gre_main;
750   gre_tunnel_t *t;
751   u32 ti;
752
753   ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
754
755   if (~0 == ti)
756     /* not one of ours */
757     return -1;
758
759   t = pool_elt_at_index (gm->tunnels, ti);
760
761   *src = t->tunnel_src;
762   *dst = t->tunnel_dst.fp_addr;
763   *is_l2 = t->type == GRE_TUNNEL_TYPE_TEB;
764
765   return (0);
766 }
767
768 /* *INDENT-OFF* */
769 VNET_DEVICE_CLASS (gre_device_class) = {
770   .name = "GRE tunnel device",
771   .format_device_name = format_gre_tunnel_name,
772   .format_device = format_gre_device,
773   .format_tx_trace = format_gre_tx_trace,
774   .admin_up_down_function = gre_interface_admin_up_down,
775   .ip_tun_desc = gre_tunnel_desc,
776 #ifdef SOON
777   .clear counter = 0;
778 #endif
779 };
780
781 VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
782   .name = "GRE",
783   .format_header = format_gre_header_with_length,
784   .unformat_header = unformat_gre_header,
785   .build_rewrite = gre_build_rewrite,
786   .update_adjacency = gre_update_adj,
787   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
788 };
789
790 VNET_HW_INTERFACE_CLASS (mgre_hw_interface_class) = {
791   .name = "mGRE",
792   .format_header = format_gre_header_with_length,
793   .unformat_header = unformat_gre_header,
794   .build_rewrite = gre_build_rewrite,
795   .update_adjacency = mgre_update_adj,
796   .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
797 };
798 /* *INDENT-ON* */
799 #endif /* CLIB_MARCH_VARIANT */
800
801 static void
802 add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name)
803 {
804   gre_protocol_info_t *pi;
805   u32 i;
806
807   vec_add2 (gm->protocol_infos, pi, 1);
808   i = pi - gm->protocol_infos;
809
810   pi->name = protocol_name;
811   pi->protocol = protocol;
812   pi->next_index = pi->node_index = ~0;
813
814   hash_set (gm->protocol_info_by_protocol, protocol, i);
815   hash_set_mem (gm->protocol_info_by_name, pi->name, i);
816 }
817
818 static clib_error_t *
819 gre_init (vlib_main_t * vm)
820 {
821   gre_main_t *gm = &gre_main;
822   clib_error_t *error;
823   ip_main_t *im = &ip_main;
824   ip_protocol_info_t *pi;
825
826   clib_memset (gm, 0, sizeof (gm[0]));
827   gm->vlib_main = vm;
828   gm->vnet_main = vnet_get_main ();
829
830   if ((error = vlib_call_init_function (vm, ip_main_init)))
831     return error;
832
833   if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
834     return error;
835
836   if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
837     return error;
838
839   /* Set up the ip packet generator */
840   pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
841   pi->format_header = format_gre_header;
842   pi->unformat_pg_edit = unformat_pg_gre_header;
843
844   gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
845   gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
846   gm->tunnel_by_key4 =
847     hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword));
848   gm->tunnel_by_key6 =
849     hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword));
850   gm->seq_num_by_key =
851     hash_create_mem (0, sizeof (gre_sn_key_t), sizeof (uword));
852
853 #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
854   foreach_gre_protocol
855 #undef _
856     return vlib_call_init_function (vm, gre_input_init);
857 }
858
859 VLIB_INIT_FUNCTION (gre_init);
860
861 /*
862  * fd.io coding-style-patch-verification: ON
863  *
864  * Local Variables:
865  * eval: (c-set-style "gnu")
866  * End:
867  */