3234de09858a32c941cfc0a14c065ebf7d787def
[vpp.git] / vnet / vnet / gre / interface.c
1 /*
2  * gre_interface.c: gre interfaces
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/pg/pg.h>
20 #include <vnet/gre/gre.h>
21 #include <vnet/ip/format.h>
22 #include <vnet/fib/ip4_fib.h>
23 #include <vnet/adj/adj_midchain.h>
24 #include <vnet/adj/adj_nbr.h>
25 #include <vnet/mpls/mpls.h>
26
27 static inline u64
28 gre_mk_key (const ip4_address_t *src,
29             const ip4_address_t *dst,
30             u32 out_fib_index)
31 {
32   // FIXME. the fib index should be part of the key
33   return ((u64)src->as_u32 << 32 | (u64)dst->as_u32);
34 }
35
36 static u8 *
37 format_gre_tunnel (u8 * s, va_list * args)
38 {
39   gre_tunnel_t * t = va_arg (*args, gre_tunnel_t *);
40   gre_main_t * gm = &gre_main;
41
42   s = format (s,
43               "[%d] %U (src) %U (dst) payload %s outer_fib_index %d",
44               t - gm->tunnels,
45               format_ip4_address, &t->tunnel_src,
46               format_ip4_address, &t->tunnel_dst,
47               (t->teb ? "teb" : "ip"),
48               t->outer_fib_index);
49
50   return s;
51 }
52
53 static gre_tunnel_t *
54 gre_tunnel_db_find (const ip4_address_t *src,
55                     const ip4_address_t *dst,
56                     u32 out_fib_index)
57 {
58   gre_main_t * gm = &gre_main;
59   uword * p;
60   u64 key;
61
62   key = gre_mk_key(src, dst, out_fib_index);
63
64   p = hash_get (gm->tunnel_by_key, key);
65
66   if (NULL == p)
67     return (NULL);
68
69   return (pool_elt_at_index (gm->tunnels, p[0]));
70 }
71
72 static void
73 gre_tunnel_db_add (const gre_tunnel_t *t)
74 {
75   gre_main_t * gm = &gre_main;
76   u64 key;
77
78   key = gre_mk_key(&t->tunnel_src, &t->tunnel_dst, t->outer_fib_index);
79   hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
80 }
81
82 static void
83 gre_tunnel_db_remove (const gre_tunnel_t *t)
84 {
85   gre_main_t * gm = &gre_main;
86   u64 key;
87
88   key = gre_mk_key(&t->tunnel_src, &t->tunnel_dst, t->outer_fib_index);
89   hash_unset (gm->tunnel_by_key, key);
90 }
91
92 static gre_tunnel_t *
93 gre_tunnel_from_fib_node (fib_node_t *node)
94 {
95 #if (CLIB_DEBUG > 0)
96     ASSERT(FIB_NODE_TYPE_GRE_TUNNEL == node->fn_type);
97 #endif
98     return ((gre_tunnel_t*) (((char*)node) -
99                              STRUCT_OFFSET_OF(gre_tunnel_t, node)));
100 }
101
102 /**
103  * gre_tunnel_stack
104  *
105  * 'stack' (resolve the recursion for) the tunnel's midchain adjacency
106  */
107 void
108 gre_tunnel_stack (adj_index_t ai)
109 {
110     gre_main_t * gm = &gre_main;
111     ip_adjacency_t *adj;
112     gre_tunnel_t *gt;
113     u32 sw_if_index;
114
115     adj = adj_get(ai);
116     sw_if_index = adj->rewrite_header.sw_if_index;
117
118     if ((vec_len(gm->tunnel_index_by_sw_if_index) < sw_if_index) ||
119         (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
120         return;
121
122     gt = pool_elt_at_index(gm->tunnels,
123                            gm->tunnel_index_by_sw_if_index[sw_if_index]);
124
125     /*
126      * find the adjacency that is contributed by the FIB entry
127      * that this tunnel resovles via, and use it as the next adj
128      * in the midchain
129      */
130     if (vnet_hw_interface_get_flags(vnet_get_main(),
131                                     gt->hw_if_index) &
132         VNET_HW_INTERFACE_FLAG_LINK_UP)
133     {
134         adj_nbr_midchain_stack(
135             ai,
136             fib_entry_contribute_ip_forwarding(gt->fib_entry_index));
137     }
138     else
139     {
140         adj_nbr_midchain_unstack(ai);
141     }
142 }
143
144 /**
145  * @brief Call back when restacking all adjacencies on a GRE interface
146  */
147 static adj_walk_rc_t
148 gre_adj_walk_cb (adj_index_t ai,
149                  void *ctx)
150 {
151     gre_tunnel_stack(ai);
152
153     return (ADJ_WALK_RC_CONTINUE);
154 }
155
156 static void
157 gre_tunnel_restack (gre_tunnel_t *gt)
158 {
159     fib_protocol_t proto;
160
161     /*
162      * walk all the adjacencies on th GRE interface and restack them
163      */
164     FOR_EACH_FIB_IP_PROTOCOL(proto)
165     {
166         adj_nbr_walk(gt->sw_if_index,
167                      proto,
168                      gre_adj_walk_cb,
169                      NULL);
170     }
171 }
172
173 /**
174  * Function definition to backwalk a FIB node
175  */
176 static fib_node_back_walk_rc_t
177 gre_tunnel_back_walk (fib_node_t *node,
178                       fib_node_back_walk_ctx_t *ctx)
179 {
180     gre_tunnel_restack(gre_tunnel_from_fib_node(node));
181
182     return (FIB_NODE_BACK_WALK_CONTINUE);
183 }
184
185 /**
186  * Function definition to get a FIB node from its index
187  */
188 static fib_node_t*
189 gre_tunnel_fib_node_get (fib_node_index_t index)
190 {
191     gre_tunnel_t * gt;
192     gre_main_t * gm;
193
194     gm  = &gre_main;
195     gt = pool_elt_at_index(gm->tunnels, index);
196
197     return (&gt->node);
198 }
199
200 /**
201  * Function definition to inform the FIB node that its last lock has gone.
202  */
203 static void
204 gre_tunnel_last_lock_gone (fib_node_t *node)
205 {
206     /*
207      * The MPLS GRE tunnel is a root of the graph. As such
208      * it never has children and thus is never locked.
209      */
210     ASSERT(0);
211 }
212
213 /*
214  * Virtual function table registered by MPLS GRE tunnels
215  * for participation in the FIB object graph.
216  */
217 const static fib_node_vft_t gre_vft = {
218     .fnv_get = gre_tunnel_fib_node_get,
219     .fnv_last_lock = gre_tunnel_last_lock_gone,
220     .fnv_back_walk = gre_tunnel_back_walk,
221 };
222
223 static int 
224 vnet_gre_tunnel_add (vnet_gre_add_del_tunnel_args_t *a,
225                      u32 * sw_if_indexp)
226 {
227   gre_main_t * gm = &gre_main;
228   vnet_main_t * vnm = gm->vnet_main;
229   ip4_main_t * im = &ip4_main;
230   gre_tunnel_t * t;
231   vnet_hw_interface_t * hi;
232   u32 hw_if_index, sw_if_index;
233   u32 outer_fib_index;
234   u8 address[6];
235   clib_error_t *error;
236
237   outer_fib_index = ip4_fib_index_from_table_id(a->outer_fib_id);
238
239   if (~0 == outer_fib_index)
240     return VNET_API_ERROR_NO_SUCH_FIB;
241
242   t = gre_tunnel_db_find(&a->src, &a->dst, a->outer_fib_id);
243
244   if (NULL != t)
245     return VNET_API_ERROR_INVALID_VALUE;
246
247   pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
248   memset (t, 0, sizeof (*t));
249   fib_node_init(&t->node, FIB_NODE_TYPE_GRE_TUNNEL);
250
251   if (vec_len (gm->free_gre_tunnel_hw_if_indices) > 0) {
252       vnet_interface_main_t * im = &vnm->interface_main;
253
254       hw_if_index = gm->free_gre_tunnel_hw_if_indices
255           [vec_len (gm->free_gre_tunnel_hw_if_indices)-1];
256       _vec_len (gm->free_gre_tunnel_hw_if_indices) -= 1;
257
258       hi = vnet_get_hw_interface (vnm, hw_if_index);
259       hi->dev_instance = t - gm->tunnels;
260       hi->hw_instance = hi->dev_instance;
261
262       /* clear old stats of freed tunnel before reuse */
263       sw_if_index = hi->sw_if_index;
264       vnet_interface_counter_lock(im);
265       vlib_zero_combined_counter
266           (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
267       vlib_zero_combined_counter
268           (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
269       vlib_zero_simple_counter
270           (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
271         vnet_interface_counter_unlock(im);
272       if (a->teb)
273       {
274         t->l2_tx_arc = vlib_node_add_named_next(vlib_get_main(),
275                                                 hi->tx_node_index,
276                                                 "adj-l2-midchain");
277       }
278     } else {
279       if (a->teb)
280       {
281         /* Default MAC address (d00b:eed0:0000 + sw_if_index) */
282         memset (address, 0, sizeof (address));
283         address[0] = 0xd0;
284         address[1] = 0x0b;
285         address[2] = 0xee;
286         address[3] = 0xd0;
287         address[4] = t - gm->tunnels;
288
289         error = ethernet_register_interface(vnm,
290                                             gre_device_class.index,
291                                             t - gm->tunnels, address,
292                                             &hw_if_index,
293                                             0);
294
295         if (error)
296         {
297           clib_error_report (error);
298           return VNET_API_ERROR_INVALID_REGISTRATION;
299         }
300         hi = vnet_get_hw_interface (vnm, hw_if_index);
301
302         t->l2_tx_arc = vlib_node_add_named_next(vlib_get_main(),
303                                                 hi->tx_node_index,
304                                                 "adj-l2-midchain");
305       } else {
306         hw_if_index = vnet_register_interface(vnm,
307                                               gre_device_class.index,
308                                               t - gm->tunnels,
309                                               gre_hw_interface_class.index,
310                                               t - gm->tunnels);
311       }
312       hi = vnet_get_hw_interface (vnm, hw_if_index);
313       sw_if_index = hi->sw_if_index;
314     }
315
316   t->hw_if_index = hw_if_index;
317   t->outer_fib_index = outer_fib_index;
318   t->sw_if_index = sw_if_index;
319   t->teb = a->teb;
320
321   vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
322   gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels;
323
324   vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
325   im->fib_index_by_sw_if_index[sw_if_index] = t->outer_fib_index;
326   ip4_sw_interface_enable_disable(sw_if_index, 1);
327
328   hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
329   hi->per_packet_overhead_bytes =
330       /* preamble */ 8 + /* inter frame gap */ 12;
331
332   /* Standard default gre MTU. */
333   hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
334
335   clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
336   clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
337
338   gre_tunnel_db_add(t);
339
340   /*
341    * source the FIB entry for the tunnel's destination
342    * and become a child thereof. The tunnel will then get poked
343    * when the forwarding for the entry updates, and the tunnel can
344    * re-stack accordingly
345    */
346   const fib_prefix_t tun_dst_pfx = {
347       .fp_len = 32,
348       .fp_proto = FIB_PROTOCOL_IP4,
349       .fp_addr = {
350           .ip4 = t->tunnel_dst,
351       }
352   };
353
354   t->fib_entry_index =
355       fib_table_entry_special_add(outer_fib_index,
356                                   &tun_dst_pfx,
357                                   FIB_SOURCE_RR,
358                                   FIB_ENTRY_FLAG_NONE,
359                                   ADJ_INDEX_INVALID);
360   t->sibling_index =
361       fib_entry_child_add(t->fib_entry_index,
362                           FIB_NODE_TYPE_GRE_TUNNEL,
363                           t - gm->tunnels);
364
365   clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
366   clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
367
368   if (t->teb)
369   {
370       t->l2_adj_index = adj_nbr_add_or_lock(FIB_PROTOCOL_IP4,
371                                             VNET_LINK_ETHERNET,
372                                             &zero_addr,
373                                             sw_if_index);
374
375       gre_update_adj(vnm, t->sw_if_index, t->l2_adj_index);
376   }
377
378   if (sw_if_indexp)
379     *sw_if_indexp = sw_if_index;
380
381   return 0;
382 }
383
384 static int 
385 vnet_gre_tunnel_delete (vnet_gre_add_del_tunnel_args_t *a,
386                         u32 * sw_if_indexp)
387 {
388   gre_main_t * gm = &gre_main;
389   vnet_main_t * vnm = gm->vnet_main;
390   gre_tunnel_t * t;
391   u32 sw_if_index;
392
393   t = gre_tunnel_db_find(&a->src, &a->dst, a->outer_fib_id);
394
395   if (NULL == t)
396     return VNET_API_ERROR_NO_SUCH_ENTRY;
397
398   sw_if_index = t->sw_if_index;
399   vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */);
400   /* make sure tunnel is removed from l2 bd or xconnect */
401   set_int_l2_mode(gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
402   vec_add1 (gm->free_gre_tunnel_hw_if_indices, t->hw_if_index);
403   gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
404   ip4_sw_interface_enable_disable(sw_if_index, 0);
405
406   fib_entry_child_remove(t->fib_entry_index,
407                          t->sibling_index);
408   fib_table_entry_delete_index(t->fib_entry_index,
409                                FIB_SOURCE_RR);
410
411   gre_tunnel_db_remove(t);
412   fib_node_deinit(&t->node);
413   pool_put (gm->tunnels, t);
414
415   if (sw_if_indexp)
416     *sw_if_indexp = sw_if_index;
417
418   return 0;
419 }
420
421 int
422 vnet_gre_add_del_tunnel (vnet_gre_add_del_tunnel_args_t *a,
423                          u32 * sw_if_indexp)
424 {
425   if (a->is_add)
426     return (vnet_gre_tunnel_add(a, sw_if_indexp));
427   else
428     return (vnet_gre_tunnel_delete(a, sw_if_indexp));
429 }
430
431 clib_error_t *
432 gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
433 {
434   gre_main_t * gm = &gre_main;
435   vnet_hw_interface_t * hi;
436   gre_tunnel_t *t;
437   u32 ti;
438
439   hi = vnet_get_hw_interface (vnm, hw_if_index);
440
441   if (NULL == gm->tunnel_index_by_sw_if_index ||
442       hi->sw_if_index >= vec_len(gm->tunnel_index_by_sw_if_index))
443       return (NULL);
444
445   ti = gm->tunnel_index_by_sw_if_index[hi->sw_if_index];
446
447   if (~0 == ti)
448       /* not one of ours */
449       return (NULL);
450
451   t = pool_elt_at_index(gm->tunnels, ti);
452
453   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
454     vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
455   else
456     vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */);
457
458   gre_tunnel_restack(t);
459
460   return /* no error */ 0;
461 }
462
463 static clib_error_t *
464 create_gre_tunnel_command_fn (vlib_main_t * vm,
465                  unformat_input_t * input,
466                  vlib_cli_command_t * cmd)
467 {
468   unformat_input_t _line_input, * line_input = &_line_input;
469   vnet_gre_add_del_tunnel_args_t _a, * a = &_a;
470   ip4_address_t src, dst;
471   u32 outer_fib_id = 0;
472   u8 teb = 0;
473   int rv;
474   u32 num_m_args = 0;
475   u8 is_add = 1;
476   u32 sw_if_index;
477
478   /* Get a line of input. */
479   if (! unformat_user (input, unformat_line_input, line_input))
480     return 0;
481
482   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
483     if (unformat (line_input, "del"))
484       is_add = 0;
485     else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
486       num_m_args++;
487     else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
488       num_m_args++;
489     else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
490       ;
491     else if (unformat (line_input, "teb"))
492       teb = 1;
493     else
494       return clib_error_return (0, "unknown input `%U'",
495                                 format_unformat_error, input);
496   }
497   unformat_free (line_input);
498
499   if (num_m_args < 2)
500       return clib_error_return (0, "mandatory argument(s) missing");
501
502   if (memcmp (&src, &dst, sizeof(src)) == 0)
503       return clib_error_return (0, "src and dst are identical");
504
505   memset (a, 0, sizeof (*a));
506   a->outer_fib_id = outer_fib_id;
507   a->teb = teb;
508   clib_memcpy(&a->src, &src, sizeof(src));
509   clib_memcpy(&a->dst, &dst, sizeof(dst));
510
511   if (is_add)
512     rv = vnet_gre_tunnel_add(a, &sw_if_index);
513   else
514     rv = vnet_gre_tunnel_delete(a, &sw_if_index);
515
516   switch(rv)
517     {
518     case 0:
519       vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
520       break;
521     case VNET_API_ERROR_INVALID_VALUE:
522       return clib_error_return (0, "GRE tunnel already exists...");
523     case VNET_API_ERROR_NO_SUCH_FIB:
524       return clib_error_return (0, "outer fib ID %d doesn't exist\n",
525                                 outer_fib_id);
526     default:
527       return clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
528     }
529
530   return 0;
531 }
532
533 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
534   .path = "create gre tunnel",
535   .short_help = "create gre tunnel src <addr> dst <addr> "
536                 "[outer-fib-id <fib>] [teb] [del]",
537   .function = create_gre_tunnel_command_fn,
538 };
539
540 static clib_error_t *
541 show_gre_tunnel_command_fn (vlib_main_t * vm,
542                             unformat_input_t * input,
543                             vlib_cli_command_t * cmd)
544 {
545   gre_main_t * gm = &gre_main;
546   gre_tunnel_t * t;
547   u32 ti = ~0;
548
549   if (pool_elts (gm->tunnels) == 0)
550     vlib_cli_output (vm, "No GRE tunnels configured...");
551
552   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
553     {
554       if (unformat (input, "%d", &ti))
555         ;
556       else
557         break;
558     }
559
560   if (~0 == ti)
561     {
562       pool_foreach (t, gm->tunnels,
563       ({
564           vlib_cli_output (vm, "%U", format_gre_tunnel, t);
565       }));
566     }
567   else
568   {
569       t = pool_elt_at_index(gm->tunnels, ti);
570
571       vlib_cli_output (vm, "%U", format_gre_tunnel, t);
572   }
573
574   return 0;
575 }
576
577 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
578     .path = "show gre tunnel",
579     .function = show_gre_tunnel_command_fn,
580 };
581
582 /* force inclusion from application's main.c */
583 clib_error_t *gre_interface_init (vlib_main_t *vm)
584 {
585   fib_node_register_type(FIB_NODE_TYPE_GRE_TUNNEL, &gre_vft);
586
587   return 0;
588 }
589 VLIB_INIT_FUNCTION(gre_interface_init);