c049b87b6a71fe66e8ff2aca34206bfe16d4af83
[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
22 gre_main_t gre_main;
23
24 typedef struct
25 {
26   union
27   {
28     ip4_and_gre_header_t ip4_and_gre;
29     u64 as_u64[3];
30   };
31 } ip4_and_gre_union_t;
32
33 typedef struct
34 {
35   union
36   {
37     ip6_and_gre_header_t ip6_and_gre;
38     u64 as_u64[3];
39   };
40 } ip6_and_gre_union_t;
41
42
43 /* Packet trace structure */
44 typedef struct
45 {
46   /* Tunnel-id / index in tunnel vector */
47   u32 tunnel_id;
48
49   /* pkt length */
50   u32 length;
51
52   /* tunnel ip addresses */
53   ip46_address_t src;
54   ip46_address_t dst;
55 } gre_tx_trace_t;
56
57 u8 *
58 format_gre_tx_trace (u8 * s, va_list * args)
59 {
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 *);
63
64   s = format (s, "GRE: tunnel %d len %d src %U dst %U",
65               t->tunnel_id, clib_net_to_host_u16 (t->length),
66               format_ip46_address, &t->src, IP46_TYPE_ANY,
67               format_ip46_address, &t->dst, IP46_TYPE_ANY);
68   return s;
69 }
70
71 u8 *
72 format_gre_protocol (u8 * s, va_list * args)
73 {
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);
77
78   if (pi)
79     s = format (s, "%s", pi->name);
80   else
81     s = format (s, "0x%04x", p);
82
83   return s;
84 }
85
86 u8 *
87 format_gre_header_with_length (u8 * s, va_list * args)
88 {
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;
94
95   header_bytes = sizeof (h[0]);
96   if (max_header_bytes != 0 && header_bytes > max_header_bytes)
97     return format (s, "gre header truncated");
98
99   indent = format_get_indent (s);
100
101   s = format (s, "GRE %U", format_gre_protocol, p);
102
103   if (max_header_bytes != 0 && header_bytes > max_header_bytes)
104     {
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);
112     }
113
114   return s;
115 }
116
117 u8 *
118 format_gre_header (u8 * s, va_list * args)
119 {
120   gre_header_t *h = va_arg (*args, gre_header_t *);
121   return format (s, "%U", format_gre_header_with_length, h, 0);
122 }
123
124 /* Returns gre protocol as an int in host byte order. */
125 uword
126 unformat_gre_protocol_host_byte_order (unformat_input_t * input,
127                                        va_list * args)
128 {
129   u16 *result = va_arg (*args, u16 *);
130   gre_main_t *gm = &gre_main;
131   int i;
132
133   /* Named type. */
134   if (unformat_user (input, unformat_vlib_number_by_name,
135                      gm->protocol_info_by_name, &i))
136     {
137       gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i);
138       *result = pi->protocol;
139       return 1;
140     }
141
142   return 0;
143 }
144
145 uword
146 unformat_gre_protocol_net_byte_order (unformat_input_t * input,
147                                       va_list * args)
148 {
149   u16 *result = va_arg (*args, u16 *);
150   if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result))
151     return 0;
152   *result = clib_host_to_net_u16 ((u16) * result);
153   return 1;
154 }
155
156 uword
157 unformat_gre_header (unformat_input_t * input, va_list * args)
158 {
159   u8 **result = va_arg (*args, u8 **);
160   gre_header_t _h, *h = &_h;
161   u16 p;
162
163   if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p))
164     return 0;
165
166   h->protocol = clib_host_to_net_u16 (p);
167
168   /* Add header to result. */
169   {
170     void *p;
171     u32 n_bytes = sizeof (h[0]);
172
173     vec_add2 (*result, p, n_bytes);
174     clib_memcpy (p, h, n_bytes);
175   }
176
177   return 1;
178 }
179
180 static int
181 gre_proto_from_vnet_link (vnet_link_t link)
182 {
183   switch (link)
184     {
185     case VNET_LINK_IP4:
186       return (GRE_PROTOCOL_ip4);
187     case VNET_LINK_IP6:
188       return (GRE_PROTOCOL_ip6);
189     case VNET_LINK_MPLS:
190       return (GRE_PROTOCOL_mpls_unicast);
191     case VNET_LINK_ETHERNET:
192       return (GRE_PROTOCOL_teb);
193     case VNET_LINK_ARP:
194       return (GRE_PROTOCOL_arp);
195     case VNET_LINK_NSH:
196       ASSERT (0);
197       break;
198     }
199   ASSERT (0);
200   return (GRE_PROTOCOL_ip4);
201 }
202
203 static u8 *
204 gre_build_rewrite (vnet_main_t * vnm,
205                    u32 sw_if_index,
206                    vnet_link_t link_type, const void *dst_address)
207 {
208   gre_main_t *gm = &gre_main;
209   ip4_and_gre_header_t *h4;
210   ip6_and_gre_header_t *h6;
211   u8 *rewrite = NULL;
212   gre_tunnel_t *t;
213   u32 ti;
214   u8 is_ipv6;
215
216   ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
217
218   if (~0 == ti)
219     /* not one of ours */
220     return (0);
221
222   t = pool_elt_at_index (gm->tunnels, ti);
223
224   is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
225
226   if (!is_ipv6)
227     {
228       vec_validate (rewrite, sizeof (*h4) - 1);
229       h4 = (ip4_and_gre_header_t *) rewrite;
230       h4->gre.protocol =
231         clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
232
233       h4->ip4.ip_version_and_header_length = 0x45;
234       h4->ip4.ttl = 254;
235       h4->ip4.protocol = IP_PROTOCOL_GRE;
236       /* fixup ip4 header length and checksum after-the-fact */
237       h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32;
238       h4->ip4.dst_address.as_u32 = t->tunnel_dst.fp_addr.ip4.as_u32;
239       h4->ip4.checksum = ip4_header_checksum (&h4->ip4);
240     }
241   else
242     {
243       vec_validate (rewrite, sizeof (*h6) - 1);
244       h6 = (ip6_and_gre_header_t *) rewrite;
245       h6->gre.protocol =
246         clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
247
248       h6->ip6.ip_version_traffic_class_and_flow_label =
249         clib_host_to_net_u32 (6 << 28);
250       h6->ip6.hop_limit = 255;
251       h6->ip6.protocol = IP_PROTOCOL_GRE;
252       /* fixup ip6 header length and checksum after-the-fact */
253       h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
254       h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
255       h6->ip6.dst_address.as_u64[0] = t->tunnel_dst.fp_addr.ip6.as_u64[0];
256       h6->ip6.dst_address.as_u64[1] = t->tunnel_dst.fp_addr.ip6.as_u64[1];
257     }
258
259   return (rewrite);
260 }
261
262 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
263
264 static void
265 gre4_fixup (vlib_main_t * vm,
266             ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
267 {
268   ip4_header_t *ip0;
269
270   ip0 = vlib_buffer_get_current (b0);
271
272   /* Fixup the checksum and len fields in the GRE tunnel encap
273    * that was applied at the midchain node */
274   ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
275   ip0->checksum = ip4_header_checksum (ip0);
276 }
277
278 static void
279 gre6_fixup (vlib_main_t * vm,
280             ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
281 {
282   ip6_header_t *ip0;
283
284   ip0 = vlib_buffer_get_current (b0);
285
286   /* Fixup the payload length field in the GRE tunnel encap that was applied
287    * at the midchain node */
288   ip0->payload_length =
289     clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0))
290     - sizeof (*ip0);
291 }
292
293 void
294 gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
295 {
296   gre_main_t *gm = &gre_main;
297   gre_tunnel_t *t;
298   u32 ti;
299   u8 is_ipv6;
300
301   ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
302   t = pool_elt_at_index (gm->tunnels, ti);
303   is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
304
305   adj_nbr_midchain_update_rewrite (ai, !is_ipv6 ? gre4_fixup : gre6_fixup,
306                                    NULL,
307                                    (VNET_LINK_ETHERNET ==
308                                     adj_get_link_type (ai) ?
309                                     ADJ_FLAG_MIDCHAIN_NO_COUNT :
310                                     ADJ_FLAG_NONE), gre_build_rewrite (vnm,
311                                                                        sw_if_index,
312                                                                        adj_get_link_type
313                                                                        (ai),
314                                                                        NULL));
315
316   gre_tunnel_stack (ai);
317 }
318
319 /**
320  * @brief TX function. Only called L2. L3 traffic uses the adj-midchains
321  */
322 static uword
323 gre_interface_tx_inline (vlib_main_t * vm,
324                          vlib_node_runtime_t * node, vlib_frame_t * frame)
325 {
326   gre_main_t *gm = &gre_main;
327   u32 next_index;
328   u32 *from, *to_next, n_left_from, n_left_to_next;
329   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
330   const gre_tunnel_t *gt = pool_elt_at_index (gm->tunnels, rd->dev_instance);
331   u8 is_ipv6 = gt->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
332
333   /* Vector of buffer / pkt indices we're supposed to process */
334   from = vlib_frame_vector_args (frame);
335
336   /* Number of buffers / pkts */
337   n_left_from = frame->n_vectors;
338
339   /* Speculatively send the first buffer to the last disposition we used */
340   next_index = node->cached_next_index;
341
342   while (n_left_from > 0)
343     {
344       /* set up to enqueue to our disposition with index = next_index */
345       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
346
347       /*
348        * FIXME DUAL LOOP
349        */
350
351       while (n_left_from > 0 && n_left_to_next > 0)
352         {
353           vlib_buffer_t *b0;
354           u32 bi0;
355
356           bi0 = from[0];
357           to_next[0] = bi0;
358           from += 1;
359           to_next += 1;
360           n_left_from -= 1;
361           n_left_to_next -= 1;
362
363           b0 = vlib_get_buffer (vm, bi0);
364
365           vnet_buffer (b0)->ip.adj_index[VLIB_TX] = gt->l2_adj_index;
366
367           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
368             {
369               gre_tx_trace_t *tr = vlib_add_trace (vm, node,
370                                                    b0, sizeof (*tr));
371               tr->tunnel_id = gt - gm->tunnels;
372               tr->src = gt->tunnel_src;
373               tr->dst = gt->tunnel_src;
374               tr->length = vlib_buffer_length_in_chain (vm, b0);
375             }
376
377           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
378                                            to_next, n_left_to_next,
379                                            bi0, gt->l2_tx_arc);
380         }
381
382       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
383     }
384
385   vlib_node_increment_counter (vm, !is_ipv6 ? gre4_input_node.index :
386                                gre6_input_node.index,
387                                GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
388
389   return frame->n_vectors;
390 }
391
392 static uword
393 gre_interface_tx (vlib_main_t * vm,
394                   vlib_node_runtime_t * node, vlib_frame_t * frame)
395 {
396   return (gre_interface_tx_inline (vm, node, frame));
397 }
398
399 static uword
400 gre_teb_interface_tx (vlib_main_t * vm,
401                       vlib_node_runtime_t * node, vlib_frame_t * frame)
402 {
403   return (gre_interface_tx_inline (vm, node, frame));
404 }
405
406 static u8 *
407 format_gre_tunnel_name (u8 * s, va_list * args)
408 {
409   u32 dev_instance = va_arg (*args, u32);
410   return format (s, "gre%d", dev_instance);
411 }
412
413 static u8 *
414 format_gre_tunnel_teb_name (u8 * s, va_list * args)
415 {
416   u32 dev_instance = va_arg (*args, u32);
417   return format (s, "teb-gre%d", dev_instance);
418 }
419
420 static u8 *
421 format_gre_device (u8 * s, va_list * args)
422 {
423   u32 dev_instance = va_arg (*args, u32);
424   CLIB_UNUSED (int verbose) = va_arg (*args, int);
425
426   s = format (s, "GRE tunnel: id %d\n", dev_instance);
427   return s;
428 }
429
430 /* *INDENT-OFF* */
431 VNET_DEVICE_CLASS (gre_device_class) = {
432   .name = "GRE tunnel device",
433   .format_device_name = format_gre_tunnel_name,
434   .format_device = format_gre_device,
435   .format_tx_trace = format_gre_tx_trace,
436   .tx_function = gre_interface_tx,
437   .admin_up_down_function = gre_interface_admin_up_down,
438 #ifdef SOON
439   .clear counter = 0;
440 #endif
441 };
442 /* *INDENT-ON* */
443
444
445 /* *INDENT-OFF* */
446 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (gre_device_class,
447                                    gre_interface_tx)
448
449 VNET_DEVICE_CLASS (gre_device_teb_class) = {
450   .name = "GRE TEB tunnel device",
451   .format_device_name = format_gre_tunnel_teb_name,
452   .format_device = format_gre_device,
453   .format_tx_trace = format_gre_tx_trace,
454   .tx_function = gre_teb_interface_tx,
455   .admin_up_down_function = gre_interface_admin_up_down,
456 #ifdef SOON
457   .clear counter = 0;
458 #endif
459 };
460
461 /* *INDENT-ON* */
462
463 /* *INDENT-OFF* */
464 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (gre_device_teb_class,
465                                    gre_teb_interface_tx)
466
467 VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
468   .name = "GRE",
469   .format_header = format_gre_header_with_length,
470   .unformat_header = unformat_gre_header,
471   .build_rewrite = gre_build_rewrite,
472   .update_adjacency = gre_update_adj,
473   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
474 };
475 /* *INDENT-ON* */
476
477 static void
478 add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name)
479 {
480   gre_protocol_info_t *pi;
481   u32 i;
482
483   vec_add2 (gm->protocol_infos, pi, 1);
484   i = pi - gm->protocol_infos;
485
486   pi->name = protocol_name;
487   pi->protocol = protocol;
488   pi->next_index = pi->node_index = ~0;
489
490   hash_set (gm->protocol_info_by_protocol, protocol, i);
491   hash_set_mem (gm->protocol_info_by_name, pi->name, i);
492 }
493
494 static clib_error_t *
495 gre_init (vlib_main_t * vm)
496 {
497   gre_main_t *gm = &gre_main;
498   clib_error_t *error;
499   ip_main_t *im = &ip_main;
500   ip_protocol_info_t *pi;
501
502   memset (gm, 0, sizeof (gm[0]));
503   gm->vlib_main = vm;
504   gm->vnet_main = vnet_get_main ();
505
506   if ((error = vlib_call_init_function (vm, ip_main_init)))
507     return error;
508
509   if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
510     return error;
511
512   if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
513     return error;
514
515   /* Set up the ip packet generator */
516   pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
517   pi->format_header = format_gre_header;
518   pi->unformat_pg_edit = unformat_pg_gre_header;
519
520   gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
521   gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
522   gm->tunnel_by_key4 =
523     hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword));
524   gm->tunnel_by_key6 =
525     hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword));
526
527 #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
528   foreach_gre_protocol
529 #undef _
530     return vlib_call_init_function (vm, gre_input_init);
531 }
532
533 VLIB_INIT_FUNCTION (gre_init);
534
535 gre_main_t *
536 gre_get_main (vlib_main_t * vm)
537 {
538   vlib_call_init_function (vm, gre_init);
539   return &gre_main;
540 }
541
542
543 /*
544  * fd.io coding-style-patch-verification: ON
545  *
546  * Local Variables:
547  * eval: (c-set-style "gnu")
548  * End:
549  */