aa6fca0f27d154a91375d666ce6c6f3fe6f220c7
[vpp.git] / vnet / 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.h>
21
22 gre_main_t gre_main;
23
24 typedef struct {
25   union {
26     ip4_and_gre_header_t ip4_and_gre;
27     u64 as_u64[3];
28   };
29 } ip4_and_gre_union_t;
30
31
32 /* Packet trace structure */
33 typedef struct {
34   /* Tunnel-id / index in tunnel vector */
35   u32 tunnel_id;
36
37   /* pkt length */
38   u32 length;
39
40   /* tunnel ip4 addresses */
41   ip4_address_t src;
42   ip4_address_t dst;
43 } gre_tx_trace_t;
44
45 u8 * format_gre_tx_trace (u8 * s, va_list * args)
46 {
47   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
49   gre_tx_trace_t * t = va_arg (*args, gre_tx_trace_t *);
50
51   s = format (s, "GRE: tunnel %d len %d src %U dst %U",
52               t->tunnel_id, clib_net_to_host_u16 (t->length),
53               format_ip4_address, &t->src.as_u8,
54               format_ip4_address, &t->dst.as_u8);
55   return s;
56 }
57
58 u8 * format_gre_protocol (u8 * s, va_list * args)
59 {
60   gre_protocol_t p = va_arg (*args, u32);
61   gre_main_t * gm = &gre_main;
62   gre_protocol_info_t * pi = gre_get_protocol_info (gm, p);
63
64   if (pi)
65     s = format (s, "%s", pi->name);
66   else
67     s = format (s, "0x%04x", p);
68
69   return s;
70 }
71
72 u8 * format_gre_header_with_length (u8 * s, va_list * args)
73 {
74   gre_main_t * gm = &gre_main;
75   gre_header_t * h = va_arg (*args, gre_header_t *);
76   u32 max_header_bytes = va_arg (*args, u32);
77   gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
78   uword indent, header_bytes;
79
80   header_bytes = sizeof (h[0]);
81   if (max_header_bytes != 0 && header_bytes > max_header_bytes)
82     return format (s, "gre header truncated");
83
84   indent = format_get_indent (s);
85
86   s = format (s, "GRE %U", format_gre_protocol, p);
87
88   if (max_header_bytes != 0 && header_bytes > max_header_bytes)
89     {
90       gre_protocol_info_t * pi = gre_get_protocol_info (gm, p);
91       vlib_node_t * node = vlib_get_node (gm->vlib_main, pi->node_index);
92       if (node->format_buffer)
93         s = format (s, "\n%U%U",
94                     format_white_space, indent,
95                     node->format_buffer, (void *) (h + 1),
96                     max_header_bytes - header_bytes);
97     }
98
99   return s;
100 }
101
102 u8 * format_gre_header (u8 * s, va_list * args)
103 {
104   gre_header_t * h = va_arg (*args, gre_header_t *);
105   return format (s, "%U", format_gre_header_with_length, h, 0);
106 }
107
108 /* Returns gre protocol as an int in host byte order. */
109 uword
110 unformat_gre_protocol_host_byte_order (unformat_input_t * input,
111                                        va_list * args)
112 {
113   u16 * result = va_arg (*args, u16 *);
114   gre_main_t * gm = &gre_main;
115   int i;
116
117   /* Named type. */
118   if (unformat_user (input, unformat_vlib_number_by_name,
119                      gm->protocol_info_by_name, &i))
120     {
121       gre_protocol_info_t * pi = vec_elt_at_index (gm->protocol_infos, i);
122       *result = pi->protocol;
123       return 1;
124     }
125
126   return 0;
127 }
128
129 uword
130 unformat_gre_protocol_net_byte_order (unformat_input_t * input,
131                                       va_list * args)
132 {
133   u16 * result = va_arg (*args, u16 *);
134   if (! unformat_user (input, unformat_gre_protocol_host_byte_order, result))
135     return 0;
136   *result = clib_host_to_net_u16 ((u16) *result);
137   return 1;
138 }
139
140 uword
141 unformat_gre_header (unformat_input_t * input, va_list * args)
142 {
143   u8 ** result = va_arg (*args, u8 **);
144   gre_header_t _h, * h = &_h;
145   u16 p;
146
147   if (! unformat (input, "%U",
148                   unformat_gre_protocol_host_byte_order, &p))
149     return 0;
150
151   h->protocol = clib_host_to_net_u16 (p);
152
153   /* Add header to result. */
154   {
155     void * p;
156     u32 n_bytes = sizeof (h[0]);
157
158     vec_add2 (*result, p, n_bytes);
159     clib_memcpy (p, h, n_bytes);
160   }
161
162   return 1;
163 }
164
165 static uword gre_set_rewrite (vnet_main_t * vnm,
166                                u32 sw_if_index,
167                                u32 l3_type,
168                                void * dst_address,
169                                void * rewrite,
170                                uword max_rewrite_bytes)
171 {
172   /*
173    * Conundrum: packets from tun/tap destined for the tunnel
174    * actually have this rewrite applied. Transit packets do not.
175    * To make the two cases equivalent, don't generate a
176    * rewrite here, build the entire header in the fast path.
177    */
178   return 0;
179
180 #ifdef THINGS_WORKED_AS_ONE_MIGHT_LIKE
181   ip4_and_gre_header_t * h = rewrite;
182   gre_protocol_t protocol;
183
184   if (max_rewrite_bytes < sizeof (h[0]))
185     return 0;
186
187   switch (l3_type) {
188 #define _(a,b) case VNET_L3_PACKET_TYPE_##a: protocol = GRE_PROTOCOL_##b; break
189     _ (IP4, ip4);
190     _ (IP6, ip6);
191 #undef _
192   default:
193     return 0;
194   }
195
196   memset (h, 0, sizeof (*h));
197   h->ip4.ip_version_and_header_length = 0x45;
198   h->ip4.ttl = 64;
199   h->ip4.protocol = IP_PROTOCOL_GRE;
200   h->gre.protocol = clib_host_to_net_u16 (protocol);
201
202   return sizeof (h[0]);
203 #endif
204 }
205
206 static uword
207 gre_interface_tx (vlib_main_t * vm,
208                   vlib_node_runtime_t * node,
209                   vlib_frame_t * frame)
210 {
211   gre_main_t * gm = &gre_main;
212   u32 next_index;
213   u32 * from, * to_next, n_left_from, n_left_to_next;
214   vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
215   gre_tunnel_t *t = pool_elt_at_index (gm->tunnels, rd->dev_instance);
216
217   /* Vector of buffer / pkt indices we're supposed to process */
218   from = vlib_frame_vector_args (frame);
219
220   /* Number of buffers / pkts */
221   n_left_from = frame->n_vectors;
222
223   /* Speculatively send the first buffer to the last disposition we used */
224   next_index = node->cached_next_index;
225
226   while (n_left_from > 0)
227     {
228       /* set up to enqueue to our disposition with index = next_index */
229       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
230
231       /*
232        * FIXME DUAL LOOP
233        */
234
235       while (n_left_from > 0 && n_left_to_next > 0)
236         {
237           u32 bi0, adj_index0, next0;
238           const ip_adjacency_t * adj0;
239           const dpo_id_t *dpo0;
240           ip4_header_t * ip0;
241           vlib_buffer_t * b0;
242
243           bi0 = from[0];
244           to_next[0] = bi0;
245           from += 1;
246           to_next += 1;
247           n_left_from -= 1;
248           n_left_to_next -= 1;
249
250           b0 = vlib_get_buffer(vm, bi0);
251           ip0 = vlib_buffer_get_current (b0);
252
253           /* Fixup the checksum and len fields in the GRE tunnel encap
254            * that was applied at the midchain node */
255           ip0->length =
256             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
257           ip0->checksum = ip4_header_checksum (ip0);
258
259           /* Follow the DPO on which the midchain is stacked */
260           adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
261           adj0 = adj_get(adj_index0);
262           dpo0 = &adj0->sub_type.midchain.next_dpo;
263           next0 = dpo0->dpoi_next_node;
264           vnet_buffer(b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
265
266           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
267             {
268               gre_tx_trace_t *tr = vlib_add_trace (vm, node,
269                                                    b0, sizeof (*tr));
270               tr->tunnel_id = t - gm->tunnels;
271               tr->length = ip0->length;
272               tr->src.as_u32 = ip0->src_address.as_u32;
273               tr->dst.as_u32 = ip0->dst_address.as_u32;
274             }
275
276           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
277                                            to_next, n_left_to_next,
278                                            bi0, next0);
279         }
280
281       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
282     }
283
284   vlib_node_increment_counter (vm, gre_input_node.index,
285                                GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
286
287   return frame->n_vectors;
288 }
289
290 static uword
291 gre_l2_interface_tx (vlib_main_t * vm,
292                      vlib_node_runtime_t * node,
293                      vlib_frame_t * frame)
294 {
295   gre_main_t * gm = &gre_main;
296   u32 next_index;
297   u32 * from, * to_next, n_left_from, n_left_to_next;
298   vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
299   const gre_tunnel_t *gt = pool_elt_at_index (gm->tunnels, rd->dev_instance);
300
301   /* Vector of buffer / pkt indices we're supposed to process */
302   from = vlib_frame_vector_args (frame);
303
304   /* Number of buffers / pkts */
305   n_left_from = frame->n_vectors;
306
307   /* Speculatively send the first buffer to the last disposition we used */
308   next_index = node->cached_next_index;
309
310   while (n_left_from > 0)
311     {
312       /* set up to enqueue to our disposition with index = next_index */
313       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
314
315       /*
316        * FIXME DUAL LOOP
317        */
318
319       while (n_left_from > 0 && n_left_to_next > 0)
320         {
321           vlib_buffer_t * b0;
322           u32 bi0;
323
324           bi0 = from[0];
325           to_next[0] = bi0;
326           from += 1;
327           to_next += 1;
328           n_left_from -= 1;
329           n_left_to_next -= 1;
330
331           b0 = vlib_get_buffer(vm, bi0);
332
333           vnet_buffer(b0)->ip.adj_index[VLIB_TX] = gt->adj_index[FIB_LINK_ETHERNET];
334
335           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
336             {
337               gre_tx_trace_t *tr = vlib_add_trace (vm, node,
338                                                    b0, sizeof (*tr));
339               tr->tunnel_id = gt - gm->tunnels;
340               tr->length = vlib_buffer_length_in_chain (vm, b0);
341               tr->src.as_u32 = gt->tunnel_src.as_u32;
342               tr->dst.as_u32 = gt->tunnel_src.as_u32;
343             }
344
345           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
346                                            to_next, n_left_to_next,
347                                            bi0, gt->l2_tx_arc);
348         }
349
350       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
351     }
352
353   vlib_node_increment_counter (vm, gre_input_node.index,
354                                GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
355
356   return frame->n_vectors;
357 }
358
359 static clib_error_t *
360 gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
361 {
362   gre_main_t * gm = &gre_main;
363   vnet_hw_interface_t * hi;
364   gre_tunnel_t *t;
365   u32 ti;
366
367   hi = vnet_get_hw_interface (vnm, hw_if_index);
368
369   if (NULL == gm->tunnel_index_by_sw_if_index ||
370       hi->sw_if_index >= vec_len(gm->tunnel_index_by_sw_if_index))
371       return (NULL);
372
373   ti = gm->tunnel_index_by_sw_if_index[hi->sw_if_index];
374
375   if (~0 == ti)
376       /* not one of ours */
377       return (NULL);
378
379   t = pool_elt_at_index(gm->tunnels, ti);
380
381   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
382     vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
383   else
384     vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */);
385
386   gre_tunnel_stack(t);
387
388   return /* no error */ 0;
389 }
390
391 static u8 * format_gre_tunnel_name (u8 * s, va_list * args)
392 {
393   u32 dev_instance = va_arg (*args, u32);
394   return format (s, "gre%d", dev_instance);
395 }
396
397 static u8 * format_gre_device (u8 * s, va_list * args)
398 {
399   u32 dev_instance = va_arg (*args, u32);
400   CLIB_UNUSED (int verbose) = va_arg (*args, int);
401
402   s = format (s, "GRE tunnel: id %d\n", dev_instance);
403   return s;
404 }
405
406 static u8 * format_gre_l2_device (u8 * s, va_list * args)
407 {
408   u32 dev_instance = va_arg (*args, u32);
409   CLIB_UNUSED (int verbose) = va_arg (*args, int);
410
411   s = format (s, "GRE L2-tunnel: id %d\n", dev_instance);
412   return s;
413 }
414
415 VNET_DEVICE_CLASS (gre_device_class) = {
416   .name = "GRE tunnel device",
417   .format_device_name = format_gre_tunnel_name,
418   .format_device = format_gre_device,
419   .format_tx_trace = format_gre_tx_trace,
420   .tx_function = gre_interface_tx,
421   .admin_up_down_function = gre_interface_admin_up_down,
422 #ifdef SOON
423   .clear counter = 0;
424 #endif
425 };
426
427 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (gre_device_class,
428                                    gre_interface_tx)
429
430 VNET_DEVICE_CLASS (gre_l2_device_class) = {
431   .name = "GRE L2 tunnel device",
432   .format_device_name = format_gre_tunnel_name,
433   .format_device = format_gre_l2_device,
434   .format_tx_trace = format_gre_tx_trace,
435   .tx_function = gre_l2_interface_tx,
436   .admin_up_down_function = gre_interface_admin_up_down,
437 #ifdef SOON
438   .clear counter = 0;
439 #endif
440 };
441
442 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (gre_l2_device_class,
443                                    gre_l2_interface_tx)
444
445
446 VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
447   .name = "GRE",
448   .format_header = format_gre_header_with_length,
449   .unformat_header = unformat_gre_header,
450   .set_rewrite = gre_set_rewrite,
451 };
452
453 static void add_protocol (gre_main_t * gm,
454                           gre_protocol_t protocol,
455                           char * protocol_name)
456 {
457   gre_protocol_info_t * pi;
458   u32 i;
459
460   vec_add2 (gm->protocol_infos, pi, 1);
461   i = pi - gm->protocol_infos;
462
463   pi->name = protocol_name;
464   pi->protocol = protocol;
465   pi->next_index = pi->node_index = ~0;
466
467   hash_set (gm->protocol_info_by_protocol, protocol, i);
468   hash_set_mem (gm->protocol_info_by_name, pi->name, i);
469 }
470
471 static clib_error_t * gre_init (vlib_main_t * vm)
472 {
473   gre_main_t * gm = &gre_main;
474   clib_error_t * error;
475   ip_main_t * im = &ip_main;
476   ip_protocol_info_t * pi;
477
478   memset (gm, 0, sizeof (gm[0]));
479   gm->vlib_main = vm;
480   gm->vnet_main = vnet_get_main();
481
482   if ((error = vlib_call_init_function (vm, ip_main_init)))
483     return error;
484
485   if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
486     return error;
487
488   /* Set up the ip packet generator */
489   pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
490   pi->format_header = format_gre_header;
491   pi->unformat_pg_edit = unformat_pg_gre_header;
492
493   gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
494   gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
495   gm->tunnel_by_key = hash_create (0, sizeof (uword));
496
497 #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
498   foreach_gre_protocol
499 #undef _
500
501   return vlib_call_init_function (vm, gre_input_init);
502 }
503
504 VLIB_INIT_FUNCTION (gre_init);
505
506 gre_main_t * gre_get_main (vlib_main_t * vm)
507 {
508   vlib_call_init_function (vm, gre_init);
509   return &gre_main;
510 }
511