GRE: API update
[vpp.git] / src / 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/fib/ip6_fib.h>
24 #include <vnet/adj/adj_midchain.h>
25 #include <vnet/adj/adj_nbr.h>
26 #include <vnet/mpls/mpls.h>
27 #include <vnet/l2/l2_input.h>
28
29 static const char *gre_tunnel_type_names[] = GRE_TUNNEL_TYPE_NAMES;
30
31 static u8 *
32 format_gre_tunnel (u8 * s, va_list * args)
33 {
34   gre_tunnel_t *t = va_arg (*args, gre_tunnel_t *);
35
36   s = format (s, "[%d] instance %d src %U dst %U fib-idx %d sw-if-idx %d ",
37               t->dev_instance, t->user_instance,
38               format_ip46_address, &t->tunnel_src, IP46_TYPE_ANY,
39               format_ip46_address, &t->tunnel_dst.fp_addr, IP46_TYPE_ANY,
40               t->outer_fib_index, t->sw_if_index);
41
42   s = format (s, "payload %s ", gre_tunnel_type_names[t->type]);
43
44   if (t->type == GRE_TUNNEL_TYPE_ERSPAN)
45     s = format (s, "session %d ", t->session_id);
46
47   if (t->type != GRE_TUNNEL_TYPE_L3)
48     s = format (s, "l2-adj-idx %d ", t->l2_adj_index);
49
50   return s;
51 }
52
53 static gre_tunnel_t *
54 gre_tunnel_db_find (const vnet_gre_tunnel_add_del_args_t * a,
55                     u32 outer_fib_index, gre_tunnel_key_t * key)
56 {
57   gre_main_t *gm = &gre_main;
58   uword *p;
59
60   if (!a->is_ipv6)
61     {
62       gre_mk_key4 (a->src.ip4, a->dst.ip4, outer_fib_index,
63                    a->type, a->session_id, &key->gtk_v4);
64       p = hash_get_mem (gm->tunnel_by_key4, &key->gtk_v4);
65     }
66   else
67     {
68       gre_mk_key6 (&a->src.ip6, &a->dst.ip6, outer_fib_index,
69                    a->type, a->session_id, &key->gtk_v6);
70       p = hash_get_mem (gm->tunnel_by_key6, &key->gtk_v6);
71     }
72
73   if (NULL == p)
74     return (NULL);
75
76   return (pool_elt_at_index (gm->tunnels, p[0]));
77 }
78
79 static void
80 gre_tunnel_db_add (gre_tunnel_t * t, gre_tunnel_key_t * key)
81 {
82   gre_main_t *gm = &gre_main;
83
84   t->key = clib_mem_alloc (sizeof (*t->key));
85   clib_memcpy (t->key, key, sizeof (*key));
86
87   if (t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6)
88     {
89       hash_set_mem (gm->tunnel_by_key6, &t->key->gtk_v6, t->dev_instance);
90     }
91   else
92     {
93       hash_set_mem (gm->tunnel_by_key4, &t->key->gtk_v4, t->dev_instance);
94     }
95 }
96
97 static void
98 gre_tunnel_db_remove (gre_tunnel_t * t)
99 {
100   gre_main_t *gm = &gre_main;
101
102   if (t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6)
103     {
104       hash_unset_mem (gm->tunnel_by_key6, &t->key->gtk_v6);
105     }
106   else
107     {
108       hash_unset_mem (gm->tunnel_by_key4, &t->key->gtk_v4);
109     }
110
111   clib_mem_free (t->key);
112   t->key = NULL;
113 }
114
115 /**
116  * gre_tunnel_stack
117  *
118  * 'stack' (resolve the recursion for) the tunnel's midchain adjacency
119  */
120 void
121 gre_tunnel_stack (adj_index_t ai)
122 {
123   gre_main_t *gm = &gre_main;
124   ip_adjacency_t *adj;
125   gre_tunnel_t *gt;
126   u32 sw_if_index;
127
128   adj = adj_get (ai);
129   sw_if_index = adj->rewrite_header.sw_if_index;
130
131   if ((vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index) ||
132       (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
133     return;
134
135   gt = pool_elt_at_index (gm->tunnels,
136                           gm->tunnel_index_by_sw_if_index[sw_if_index]);
137
138   if ((vnet_hw_interface_get_flags (vnet_get_main (), gt->hw_if_index) &
139        VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
140     {
141       adj_midchain_delegate_unstack (ai);
142     }
143   else
144     {
145       adj_midchain_delegate_stack (ai, gt->outer_fib_index, &gt->tunnel_dst);
146     }
147 }
148
149 /**
150  * @brief Call back when restacking all adjacencies on a GRE interface
151  */
152 static adj_walk_rc_t
153 gre_adj_walk_cb (adj_index_t ai, void *ctx)
154 {
155   gre_tunnel_stack (ai);
156
157   return (ADJ_WALK_RC_CONTINUE);
158 }
159
160 static void
161 gre_tunnel_restack (gre_tunnel_t * gt)
162 {
163   fib_protocol_t proto;
164
165   /*
166    * walk all the adjacencies on th GRE interface and restack them
167    */
168   FOR_EACH_FIB_IP_PROTOCOL (proto)
169   {
170     adj_nbr_walk (gt->sw_if_index, proto, gre_adj_walk_cb, NULL);
171   }
172 }
173
174 static int
175 vnet_gre_tunnel_add (vnet_gre_tunnel_add_del_args_t * a,
176                      u32 outer_fib_index, u32 * sw_if_indexp)
177 {
178   gre_main_t *gm = &gre_main;
179   vnet_main_t *vnm = gm->vnet_main;
180   ip4_main_t *im4 = &ip4_main;
181   ip6_main_t *im6 = &ip6_main;
182   gre_tunnel_t *t;
183   vnet_hw_interface_t *hi;
184   u32 hw_if_index, sw_if_index;
185   clib_error_t *error;
186   u8 is_ipv6 = a->is_ipv6;
187   gre_tunnel_key_t key;
188
189   t = gre_tunnel_db_find (a, outer_fib_index, &key);
190   if (NULL != t)
191     return VNET_API_ERROR_IF_ALREADY_EXISTS;
192
193   pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
194   clib_memset (t, 0, sizeof (*t));
195
196   /* Reconcile the real dev_instance and a possible requested instance */
197   u32 t_idx = t - gm->tunnels;  /* tunnel index (or instance) */
198   u32 u_idx = a->instance;      /* user specified instance */
199   if (u_idx == ~0)
200     u_idx = t_idx;
201   if (hash_get (gm->instance_used, u_idx))
202     {
203       pool_put (gm->tunnels, t);
204       return VNET_API_ERROR_INSTANCE_IN_USE;
205     }
206   hash_set (gm->instance_used, u_idx, 1);
207
208   t->dev_instance = t_idx;      /* actual */
209   t->user_instance = u_idx;     /* name */
210
211   t->type = a->type;
212   if (t->type == GRE_TUNNEL_TYPE_ERSPAN)
213     t->session_id = a->session_id;
214
215   if (t->type == GRE_TUNNEL_TYPE_L3)
216     hw_if_index = vnet_register_interface (vnm, gre_device_class.index, t_idx,
217                                            gre_hw_interface_class.index,
218                                            t_idx);
219   else
220     {
221       /* Default MAC address (d00b:eed0:0000 + sw_if_index) */
222       u8 address[6] =
223         { 0xd0, 0x0b, 0xee, 0xd0, (u8) (t_idx >> 8), (u8) t_idx };
224       error =
225         ethernet_register_interface (vnm, gre_device_class.index, t_idx,
226                                      address, &hw_if_index, 0);
227       if (error)
228         {
229           clib_error_report (error);
230           return VNET_API_ERROR_INVALID_REGISTRATION;
231         }
232     }
233
234   /* Set GRE tunnel interface output node (not used for L3 payload) */
235   vnet_set_interface_output_node (vnm, hw_if_index, gre_encap_node.index);
236
237   hi = vnet_get_hw_interface (vnm, hw_if_index);
238   sw_if_index = hi->sw_if_index;
239
240   t->hw_if_index = hw_if_index;
241   t->outer_fib_index = outer_fib_index;
242   t->sw_if_index = sw_if_index;
243   t->l2_adj_index = ADJ_INDEX_INVALID;
244
245   vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
246   gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx;
247
248   if (!is_ipv6)
249     {
250       vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
251       hi->min_packet_bytes =
252         64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
253     }
254   else
255     {
256       vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
257       hi->min_packet_bytes =
258         64 + sizeof (gre_header_t) + sizeof (ip6_header_t);
259     }
260
261   /* Standard default gre MTU. */
262   vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000);
263
264   /*
265    * source the FIB entry for the tunnel's destination
266    * and become a child thereof. The tunnel will then get poked
267    * when the forwarding for the entry updates, and the tunnel can
268    * re-stack accordingly
269    */
270
271   clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
272   t->tunnel_dst.fp_len = !is_ipv6 ? 32 : 128;
273   t->tunnel_dst.fp_proto = !is_ipv6 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
274   t->tunnel_dst.fp_addr = a->dst;
275
276   gre_tunnel_db_add (t, &key);
277   if (t->type == GRE_TUNNEL_TYPE_ERSPAN)
278     {
279       gre_sn_key_t skey;
280       gre_sn_t *gre_sn;
281
282       gre_mk_sn_key (t, &skey);
283       gre_sn = (gre_sn_t *) hash_get_mem (gm->seq_num_by_key, &skey);
284       if (gre_sn != NULL)
285         {
286           gre_sn->ref_count++;
287           t->gre_sn = gre_sn;
288         }
289       else
290         {
291           gre_sn = clib_mem_alloc (sizeof (gre_sn_t));
292           gre_sn->seq_num = 0;
293           gre_sn->ref_count = 1;
294           t->gre_sn = gre_sn;
295           hash_set_mem_alloc (&gm->seq_num_by_key, &skey, (uword) gre_sn);
296         }
297     }
298
299   if (t->type != GRE_TUNNEL_TYPE_L3)
300     {
301       t->l2_adj_index = adj_nbr_add_or_lock
302         (t->tunnel_dst.fp_proto, VNET_LINK_ETHERNET, &zero_addr, sw_if_index);
303       gre_update_adj (vnm, t->sw_if_index, t->l2_adj_index);
304     }
305
306   if (sw_if_indexp)
307     *sw_if_indexp = sw_if_index;
308
309   return 0;
310 }
311
312 static int
313 vnet_gre_tunnel_delete (vnet_gre_tunnel_add_del_args_t * a,
314                         u32 outer_fib_index, u32 * sw_if_indexp)
315 {
316   gre_main_t *gm = &gre_main;
317   vnet_main_t *vnm = gm->vnet_main;
318   gre_tunnel_t *t;
319   gre_tunnel_key_t key;
320   u32 sw_if_index;
321
322   t = gre_tunnel_db_find (a, outer_fib_index, &key);
323   if (NULL == t)
324     return VNET_API_ERROR_NO_SUCH_ENTRY;
325
326   sw_if_index = t->sw_if_index;
327   vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
328
329   /* make sure tunnel is removed from l2 bd or xconnect */
330   set_int_l2_mode (gm->vlib_main, vnm, MODE_L3, sw_if_index, 0,
331                    L2_BD_PORT_TYPE_NORMAL, 0, 0);
332   gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
333
334   if (t->type == GRE_TUNNEL_TYPE_L3)
335     vnet_delete_hw_interface (vnm, t->hw_if_index);
336   else
337     ethernet_delete_interface (vnm, t->hw_if_index);
338
339   if (t->l2_adj_index != ADJ_INDEX_INVALID)
340     {
341       adj_midchain_delegate_unstack (t->l2_adj_index);
342       adj_unlock (t->l2_adj_index);
343     }
344
345   ASSERT ((t->type != GRE_TUNNEL_TYPE_ERSPAN) || (t->gre_sn != NULL));
346   if ((t->type == GRE_TUNNEL_TYPE_ERSPAN) && (t->gre_sn->ref_count-- == 1))
347     {
348       gre_sn_key_t skey;
349       gre_mk_sn_key (t, &skey);
350       hash_unset_mem_free (&gm->seq_num_by_key, &skey);
351       clib_mem_free (t->gre_sn);
352     }
353
354   hash_unset (gm->instance_used, t->user_instance);
355   gre_tunnel_db_remove (t);
356   pool_put (gm->tunnels, t);
357
358   if (sw_if_indexp)
359     *sw_if_indexp = sw_if_index;
360
361   return 0;
362 }
363
364 int
365 vnet_gre_tunnel_add_del (vnet_gre_tunnel_add_del_args_t * a,
366                          u32 * sw_if_indexp)
367 {
368   u32 outer_fib_index;
369
370   if (!a->is_ipv6)
371     outer_fib_index = ip4_fib_index_from_table_id (a->outer_fib_id);
372   else
373     outer_fib_index = ip6_fib_index_from_table_id (a->outer_fib_id);
374
375   if (~0 == outer_fib_index)
376     return VNET_API_ERROR_NO_SUCH_FIB;
377
378   if (a->session_id > GTK_SESSION_ID_MAX)
379     return VNET_API_ERROR_INVALID_SESSION_ID;
380
381   if (a->is_add)
382     return (vnet_gre_tunnel_add (a, outer_fib_index, sw_if_indexp));
383   else
384     return (vnet_gre_tunnel_delete (a, outer_fib_index, sw_if_indexp));
385 }
386
387 clib_error_t *
388 gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
389 {
390   gre_main_t *gm = &gre_main;
391   vnet_hw_interface_t *hi;
392   gre_tunnel_t *t;
393   u32 ti;
394
395   hi = vnet_get_hw_interface (vnm, hw_if_index);
396
397   if (NULL == gm->tunnel_index_by_sw_if_index ||
398       hi->sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index))
399     return (NULL);
400
401   ti = gm->tunnel_index_by_sw_if_index[hi->sw_if_index];
402
403   if (~0 == ti)
404     /* not one of ours */
405     return (NULL);
406
407   t = pool_elt_at_index (gm->tunnels, ti);
408
409   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
410     vnet_hw_interface_set_flags (vnm, hw_if_index,
411                                  VNET_HW_INTERFACE_FLAG_LINK_UP);
412   else
413     vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
414
415   gre_tunnel_restack (t);
416
417   return /* no error */ 0;
418 }
419
420 static clib_error_t *
421 create_gre_tunnel_command_fn (vlib_main_t * vm,
422                               unformat_input_t * input,
423                               vlib_cli_command_t * cmd)
424 {
425   unformat_input_t _line_input, *line_input = &_line_input;
426   vnet_gre_tunnel_add_del_args_t _a, *a = &_a;
427   ip46_address_t src, dst;
428   u32 instance = ~0;
429   u32 outer_fib_id = 0;
430   gre_tunnel_type_t t_type = GRE_TUNNEL_TYPE_L3;
431   u32 session_id = 0;
432   int rv;
433   u32 num_m_args = 0;
434   u8 is_add = 1;
435   u32 sw_if_index;
436   clib_error_t *error = NULL;
437   u8 ipv4_set = 0;
438   u8 ipv6_set = 0;
439
440   /* Get a line of input. */
441   if (!unformat_user (input, unformat_line_input, line_input))
442     return 0;
443
444   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
445     {
446       if (unformat (line_input, "del"))
447         is_add = 0;
448       else if (unformat (line_input, "instance %d", &instance))
449         ;
450       else
451         if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
452         {
453           num_m_args++;
454           ipv4_set = 1;
455         }
456       else
457         if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
458         {
459           num_m_args++;
460           ipv4_set = 1;
461         }
462       else
463         if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
464         {
465           num_m_args++;
466           ipv6_set = 1;
467         }
468       else
469         if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
470         {
471           num_m_args++;
472           ipv6_set = 1;
473         }
474       else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
475         ;
476       else if (unformat (line_input, "teb"))
477         t_type = GRE_TUNNEL_TYPE_TEB;
478       else if (unformat (line_input, "erspan %d", &session_id))
479         t_type = GRE_TUNNEL_TYPE_ERSPAN;
480       else
481         {
482           error = clib_error_return (0, "unknown input `%U'",
483                                      format_unformat_error, line_input);
484           goto done;
485         }
486     }
487
488   if (num_m_args < 2)
489     {
490       error = clib_error_return (0, "mandatory argument(s) missing");
491       goto done;
492     }
493
494   if ((ipv4_set && memcmp (&src.ip4, &dst.ip4, sizeof (src.ip4)) == 0) ||
495       (ipv6_set && memcmp (&src.ip6, &dst.ip6, sizeof (src.ip6)) == 0))
496     {
497       error = clib_error_return (0, "src and dst are identical");
498       goto done;
499     }
500
501   if (ipv4_set && ipv6_set)
502     return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
503
504   if ((ipv4_set && memcmp (&dst.ip4, &zero_addr.ip4, sizeof (dst.ip4)) == 0)
505       || (ipv6_set
506           && memcmp (&dst.ip6, &zero_addr.ip6, sizeof (dst.ip6)) == 0))
507     {
508       error = clib_error_return (0, "dst address cannot be zero");
509       goto done;
510     }
511
512   clib_memset (a, 0, sizeof (*a));
513   a->is_add = is_add;
514   a->outer_fib_id = outer_fib_id;
515   a->type = t_type;
516   a->session_id = session_id;
517   a->is_ipv6 = ipv6_set;
518   a->instance = instance;
519   if (!ipv6_set)
520     {
521       clib_memcpy (&a->src.ip4, &src.ip4, sizeof (src.ip4));
522       clib_memcpy (&a->dst.ip4, &dst.ip4, sizeof (dst.ip4));
523     }
524   else
525     {
526       clib_memcpy (&a->src.ip6, &src.ip6, sizeof (src.ip6));
527       clib_memcpy (&a->dst.ip6, &dst.ip6, sizeof (dst.ip6));
528     }
529
530   rv = vnet_gre_tunnel_add_del (a, &sw_if_index);
531
532   switch (rv)
533     {
534     case 0:
535       vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
536                        vnet_get_main (), sw_if_index);
537       break;
538     case VNET_API_ERROR_IF_ALREADY_EXISTS:
539       error = clib_error_return (0, "GRE tunnel already exists...");
540       goto done;
541     case VNET_API_ERROR_NO_SUCH_FIB:
542       error = clib_error_return (0, "outer fib ID %d doesn't exist\n",
543                                  outer_fib_id);
544       goto done;
545     case VNET_API_ERROR_NO_SUCH_ENTRY:
546       error = clib_error_return (0, "GRE tunnel doesn't exist");
547       goto done;
548     case VNET_API_ERROR_INVALID_SESSION_ID:
549       error = clib_error_return (0, "session ID %d out of range\n",
550                                  session_id);
551       goto done;
552     case VNET_API_ERROR_INSTANCE_IN_USE:
553       error = clib_error_return (0, "Instance is in use");
554       goto done;
555     default:
556       error =
557         clib_error_return (0, "vnet_gre_tunnel_add_del returned %d", rv);
558       goto done;
559     }
560
561 done:
562   unformat_free (line_input);
563
564   return error;
565 }
566
567 /* *INDENT-OFF* */
568 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
569   .path = "create gre tunnel",
570   .short_help = "create gre tunnel src <addr> dst <addr> [instance <n>] "
571                 "[outer-fib-id <fib>] [teb | erspan <session-id>] [del]",
572   .function = create_gre_tunnel_command_fn,
573 };
574 /* *INDENT-ON* */
575
576 static clib_error_t *
577 show_gre_tunnel_command_fn (vlib_main_t * vm,
578                             unformat_input_t * input,
579                             vlib_cli_command_t * cmd)
580 {
581   gre_main_t *gm = &gre_main;
582   gre_tunnel_t *t;
583   u32 ti = ~0;
584
585   if (pool_elts (gm->tunnels) == 0)
586     vlib_cli_output (vm, "No GRE tunnels configured...");
587
588   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
589     {
590       if (unformat (input, "%d", &ti))
591         ;
592       else
593         break;
594     }
595
596   if (~0 == ti)
597     {
598       /* *INDENT-OFF* */
599       pool_foreach (t, gm->tunnels,
600       ({
601           vlib_cli_output (vm, "%U", format_gre_tunnel, t);
602       }));
603       /* *INDENT-ON* */
604     }
605   else
606     {
607       t = pool_elt_at_index (gm->tunnels, ti);
608
609       vlib_cli_output (vm, "%U", format_gre_tunnel, t);
610     }
611
612   return 0;
613 }
614
615 /* *INDENT-OFF* */
616 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
617     .path = "show gre tunnel",
618     .function = show_gre_tunnel_command_fn,
619 };
620 /* *INDENT-ON* */
621
622 /* force inclusion from application's main.c */
623 clib_error_t *
624 gre_interface_init (vlib_main_t * vm)
625 {
626   return (NULL);
627 }
628
629 VLIB_INIT_FUNCTION (gre_interface_init);
630
631 /*
632  * fd.io coding-style-patch-verification: ON
633  *
634  * Local Variables:
635  * eval: (c-set-style "gnu")
636  * End:
637  */