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