74518bfcec07ff26667650d2ec5d5079ced4edc5
[vpp.git] / vnet / vnet / vxlan-gpe / vxlan_gpe.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vnet/vxlan-gpe/vxlan_gpe.h>
16 #include <vnet/ip/format.h>
17
18 vxlan_gpe_main_t vxlan_gpe_main;
19
20 u8 * format_vxlan_gpe_tunnel (u8 * s, va_list * args)
21 {
22   vxlan_gpe_tunnel_t * t = va_arg (*args, vxlan_gpe_tunnel_t *);
23   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
24
25   s = format (s, "[%d] local: %U remote: %U ",
26               t - gm->tunnels,
27               format_ip46_address, &t->local,
28               format_ip46_address, &t->remote);
29
30   s = format (s, "  vxlan VNI %d ", t->vni);
31
32   switch (t->protocol)
33     {
34     case VXLAN_GPE_PROTOCOL_IP4:
35       s = format (s, "next-protocol ip4");
36       break;
37     case VXLAN_GPE_PROTOCOL_IP6:
38       s = format (s, "next-protocol ip6");
39       break;
40     case VXLAN_GPE_PROTOCOL_ETHERNET:
41       s = format (s, "next-protocol ethernet");
42       break;
43     case VXLAN_GPE_PROTOCOL_NSH:
44       s = format (s, "next-protocol nsh");
45       break;
46     default:
47       s = format (s, "next-protocol unknown %d", t->protocol);
48     }
49
50   s = format (s, " fibs: (encap %d, decap %d)",
51               t->encap_fib_index,
52               t->decap_fib_index);
53
54   return s;
55 }
56
57 static u8 * format_vxlan_gpe_name (u8 * s, va_list * args)
58 {
59   u32 dev_instance = va_arg (*args, u32);
60   return format (s, "vxlan_gpe_tunnel%d", dev_instance);
61 }
62
63
64 static uword dummy_interface_tx (vlib_main_t * vm,
65                                  vlib_node_runtime_t * node,
66                                  vlib_frame_t * frame)
67 {
68   clib_warning ("you shouldn't be here, leaking buffers...");
69   return frame->n_vectors;
70 }
71 static clib_error_t *
72 vxlan_gpe_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
73 {
74   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
75     vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
76   else
77     vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
78
79   return 0;
80 }
81
82 VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
83   .name = "VXLAN_GPE",
84   .format_device_name = format_vxlan_gpe_name,
85   .format_tx_trace = format_vxlan_gpe_encap_trace,
86   .tx_function = dummy_interface_tx,
87   .admin_up_down_function = vxlan_gpe_interface_admin_up_down,
88 };
89
90 static uword dummy_set_rewrite (vnet_main_t * vnm,
91                                 u32 sw_if_index,
92                                 u32 l3_type,
93                                 void * dst_address,
94                                 void * rewrite,
95                                 uword max_rewrite_bytes)
96 {
97   return 0;
98 }
99
100
101 static u8 * format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
102 {
103   u32 dev_instance = va_arg (*args, u32);
104   s = format (s, "unimplemented dev %u", dev_instance);
105   return s;
106 }
107
108 VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
109   .name = "VXLAN_GPE",
110   .format_header = format_vxlan_gpe_header_with_length,
111   .set_rewrite = dummy_set_rewrite,
112 };
113
114
115 #define foreach_gpe_copy_field                  \
116 _(vni)                                          \
117 _(protocol)                                \
118 _(encap_fib_index)                              \
119 _(decap_fib_index)                              \
120 _(decap_next_index)
121
122 #define foreach_copy_ipv4 {                     \
123   _(local.ip4.as_u32)                           \
124   _(remote.ip4.as_u32)                          \
125 }
126
127 #define foreach_copy_ipv6 {                     \
128   _(local.ip6.as_u64[0])                        \
129   _(local.ip6.as_u64[1])                        \
130   _(remote.ip6.as_u64[0])                       \
131   _(remote.ip6.as_u64[1])                       \
132 }
133
134
135 static int vxlan4_gpe_rewrite (vxlan_gpe_tunnel_t * t)
136 {
137   u8 *rw = 0;
138   ip4_header_t * ip0;
139   ip4_vxlan_gpe_header_t * h0;
140   int len;
141
142   len = sizeof (*h0);
143
144   vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
145
146   h0 = (ip4_vxlan_gpe_header_t *) rw;
147
148   /* Fixed portion of the (outer) ip4 header */
149   ip0 = &h0->ip4;
150   ip0->ip_version_and_header_length = 0x45;
151   ip0->ttl = 254;
152   ip0->protocol = IP_PROTOCOL_UDP;
153
154   /* we fix up the ip4 header length and checksum after-the-fact */
155   ip0->src_address.as_u32 = t->local.ip4.as_u32;
156   ip0->dst_address.as_u32 = t->remote.ip4.as_u32;
157   ip0->checksum = ip4_header_checksum (ip0);
158
159   /* UDP header, randomize src port on something, maybe? */
160   h0->udp.src_port = clib_host_to_net_u16 (4790);
161   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
162
163   /* VXLAN header. Are we having fun yet? */
164   h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
165   h0->vxlan.ver_res = VXLAN_GPE_VERSION;
166   h0->vxlan.protocol = VXLAN_GPE_PROTOCOL_IP4;
167   h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
168
169   t->rewrite = rw;
170   return (0);
171 }
172
173 static int vxlan6_gpe_rewrite (vxlan_gpe_tunnel_t * t)
174 {
175   u8 *rw = 0;
176   ip6_header_t * ip0;
177   ip6_vxlan_gpe_header_t * h0;
178   int len;
179
180   len = sizeof (*h0);
181
182   vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
183
184   h0 = (ip6_vxlan_gpe_header_t *) rw;
185
186   /* Fixed portion of the (outer) ip4 header */
187   ip0 = &h0->ip6;
188   ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
189   ip0->hop_limit = 255;
190   ip0->protocol = IP_PROTOCOL_UDP;
191
192   ip0->src_address.as_u64[0] = t->local.ip6.as_u64[0];
193   ip0->src_address.as_u64[1] = t->local.ip6.as_u64[1];
194   ip0->dst_address.as_u64[0] = t->remote.ip6.as_u64[0];
195   ip0->dst_address.as_u64[1] = t->remote.ip6.as_u64[1];
196
197   /* UDP header, randomize src port on something, maybe? */
198   h0->udp.src_port = clib_host_to_net_u16 (4790);
199   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
200
201   /* VXLAN header. Are we having fun yet? */
202   h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
203   h0->vxlan.ver_res = VXLAN_GPE_VERSION;
204   h0->vxlan.protocol = VXLAN_GPE_PROTOCOL_IP4;
205   h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
206
207   t->rewrite = rw;
208   return (0);
209 }
210
211 int vnet_vxlan_gpe_add_del_tunnel
212 (vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
213 {
214   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
215   vxlan_gpe_tunnel_t *t = 0;
216   vnet_main_t * vnm = gm->vnet_main;
217   vnet_hw_interface_t * hi;
218   uword * p;
219   u32 hw_if_index = ~0;
220   u32 sw_if_index = ~0;
221   int rv;
222   vxlan4_gpe_tunnel_key_t key4, *key4_copy;
223   vxlan6_gpe_tunnel_key_t key6, *key6_copy;
224   hash_pair_t *hp;
225
226   if (!a->is_ip6)
227   {
228     key4.local = a->local.ip4.as_u32;
229     key4.remote = a->remote.ip4.as_u32;
230     key4.vni = clib_host_to_net_u32 (a->vni << 8);
231     key4.pad = 0;
232
233     p = hash_get_mem(gm->vxlan4_gpe_tunnel_by_key, &key4);
234   }
235   else
236   {
237     key6.local.as_u64[0] = a->local.ip6.as_u64[0];
238     key6.local.as_u64[1] = a->local.ip6.as_u64[1];
239     key6.remote.as_u64[0] = a->remote.ip6.as_u64[0];
240     key6.remote.as_u64[1] = a->remote.ip6.as_u64[1];
241     key6.vni = clib_host_to_net_u32 (a->vni << 8);
242
243     p = hash_get_mem(gm->vxlan6_gpe_tunnel_by_key, &key6);
244   }
245
246   if (a->is_add)
247     {
248       /* adding a tunnel: tunnel must not already exist */
249       if (p)
250         return VNET_API_ERROR_INVALID_VALUE;
251
252       if (a->decap_next_index >= VXLAN_GPE_INPUT_N_NEXT)
253         return VNET_API_ERROR_INVALID_DECAP_NEXT;
254
255       pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
256       memset (t, 0, sizeof (*t));
257
258       /* copy from arg structure */
259 #define _(x) t->x = a->x;
260       foreach_gpe_copy_field;
261       if (!a->is_ip6) foreach_copy_ipv4
262       else            foreach_copy_ipv6
263 #undef _
264
265       if (!a->is_ip6) t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4;
266
267       if (!a->is_ip6) {
268         rv = vxlan4_gpe_rewrite (t);
269       } else {
270         rv = vxlan6_gpe_rewrite (t);
271       }
272
273       if (rv)
274       {
275           pool_put (gm->tunnels, t);
276           return rv;
277       }
278
279       if (!a->is_ip6)
280       {
281         key4_copy = clib_mem_alloc (sizeof (*key4_copy));
282         clib_memcpy (key4_copy, &key4, sizeof (*key4_copy));
283         hash_set_mem (gm->vxlan4_gpe_tunnel_by_key, key4_copy,
284                       t - gm->tunnels);
285       }
286       else
287       {
288           key6_copy = clib_mem_alloc (sizeof (*key6_copy));
289           clib_memcpy (key6_copy, &key6, sizeof (*key6_copy));
290           hash_set_mem (gm->vxlan6_gpe_tunnel_by_key, key6_copy,
291                         t - gm->tunnels);
292       }
293
294       if (vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
295         {
296           hw_if_index = gm->free_vxlan_gpe_tunnel_hw_if_indices
297             [vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices)-1];
298           _vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
299
300           hi = vnet_get_hw_interface (vnm, hw_if_index);
301           hi->dev_instance = t - gm->tunnels;
302           hi->hw_instance = hi->dev_instance;
303         }
304       else
305         {
306           hw_if_index = vnet_register_interface
307             (vnm, vxlan_gpe_device_class.index, t - gm->tunnels,
308              vxlan_gpe_hw_class.index, t - gm->tunnels);
309           hi = vnet_get_hw_interface (vnm, hw_if_index);
310           hi->output_node_index = vxlan_gpe_encap_node.index;
311         }
312
313       t->hw_if_index = hw_if_index;
314       t->sw_if_index = sw_if_index = hi->sw_if_index;
315
316       vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
317                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
318     }
319   else
320     {
321       /* deleting a tunnel: tunnel must exist */
322       if (!p)
323         return VNET_API_ERROR_NO_SUCH_ENTRY;
324
325       t = pool_elt_at_index (gm->tunnels, p[0]);
326
327       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
328       vec_add1 (gm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
329
330       if (!a->is_ip6)
331       {
332         hp = hash_get_pair (gm->vxlan4_gpe_tunnel_by_key, &key4);
333         key4_copy = (void *)(hp->key);
334         hash_unset_mem (gm->vxlan4_gpe_tunnel_by_key, &key4);
335         clib_mem_free (key4_copy);
336       }
337       else
338       {
339         hp = hash_get_pair (gm->vxlan6_gpe_tunnel_by_key, &key6);
340         key6_copy = (void *)(hp->key);
341         hash_unset_mem (gm->vxlan4_gpe_tunnel_by_key, &key6);
342         clib_mem_free (key6_copy);
343       }
344
345       vec_free (t->rewrite);
346       pool_put (gm->tunnels, t);
347     }
348
349   if (sw_if_indexp)
350       *sw_if_indexp = sw_if_index;
351
352   return 0;
353 }
354
355 static u32 fib4_index_from_fib_id (u32 fib_id)
356 {
357   ip4_main_t * im = &ip4_main;
358   uword * p;
359
360   p = hash_get (im->fib_index_by_table_id, fib_id);
361   if (!p)
362     return ~0;
363
364   return p[0];
365 }
366
367 static u32 fib6_index_from_fib_id (u32 fib_id)
368 {
369   ip6_main_t * im = &ip6_main;
370   uword * p;
371
372   p = hash_get (im->fib_index_by_table_id, fib_id);
373   if (!p)
374     return ~0;
375
376   return p[0];
377 }
378
379 static uword unformat_gpe_decap_next (unformat_input_t * input, va_list * args)
380 {
381   u32 * result = va_arg (*args, u32 *);
382   u32 tmp;
383
384   if (unformat (input, "drop"))
385     *result = VXLAN_GPE_INPUT_NEXT_DROP;
386   else if (unformat (input, "ip4"))
387     *result = VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
388   else if (unformat (input, "ip6"))
389     *result = VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
390   else if (unformat (input, "ethernet"))
391     *result = VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
392   else if (unformat (input, "%d", &tmp))
393     *result = tmp;
394   else
395     return 0;
396   return 1;
397 }
398
399 static clib_error_t *
400 vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
401                                    unformat_input_t * input,
402                                    vlib_cli_command_t * cmd)
403 {
404   unformat_input_t _line_input, * line_input = &_line_input;
405   u8 is_add = 1;
406   ip46_address_t local, remote;
407   u8 local_set = 0;
408   u8 remote_set = 0;
409   u8 ipv4_set = 0;
410   u8 ipv6_set = 0;
411   u32 encap_fib_index = 0;
412   u32 decap_fib_index = 0;
413   u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
414   u32 decap_next_index = VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
415   u32 vni;
416   u8 vni_set = 0;
417   int rv;
418   u32 tmp;
419   vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
420   u32 sw_if_index;
421
422   /* Get a line of input. */
423   if (! unformat_user (input, unformat_line_input, line_input))
424     return 0;
425
426   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
427     if (unformat (line_input, "del"))
428       is_add = 0;
429     else if (unformat (line_input, "local %U",
430                        unformat_ip4_address, &local.ip4))
431     {
432       local_set = 1;
433       ipv4_set = 1;
434     }
435     else if (unformat (line_input, "remote %U",
436                        unformat_ip4_address, &remote.ip4))
437     {
438       remote_set = 1;
439       ipv4_set = 1;
440     }
441     else if (unformat (line_input, "local %U",
442                        unformat_ip6_address, &local.ip6))
443     {
444       local_set = 1;
445       ipv6_set = 1;
446     }
447     else if (unformat (line_input, "remote %U",
448                        unformat_ip6_address, &remote.ip6))
449     {
450       remote_set = 1;
451       ipv6_set = 1;
452     }
453     else if (unformat (line_input, "encap-vrf-id %d", &tmp))
454       {
455         if (ipv6_set)
456           encap_fib_index = fib6_index_from_fib_id (tmp);
457         else
458           encap_fib_index = fib4_index_from_fib_id (tmp);
459
460         if (encap_fib_index == ~0)
461           return clib_error_return (0, "nonexistent encap fib id %d", tmp);
462       }
463     else if (unformat (line_input, "decap-vrf-id %d", &tmp))
464       {
465
466         if (ipv6_set)
467           decap_fib_index = fib6_index_from_fib_id (tmp);
468         else
469           decap_fib_index = fib4_index_from_fib_id (tmp);
470
471         if (decap_fib_index == ~0)
472           return clib_error_return (0, "nonexistent decap fib id %d", tmp);
473       }
474     else if (unformat (line_input, "decap-next %U", unformat_gpe_decap_next,
475                        &decap_next_index))
476       ;
477     else if (unformat (line_input, "vni %d", &vni))
478       vni_set = 1;
479     else if (unformat(line_input, "next-ip4"))
480       protocol = VXLAN_GPE_PROTOCOL_IP4;
481     else if (unformat(line_input, "next-ip6"))
482       protocol = VXLAN_GPE_PROTOCOL_IP6;
483     else if (unformat(line_input, "next-ethernet"))
484       protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
485     else if (unformat(line_input, "next-nsh"))
486       protocol = VXLAN_GPE_PROTOCOL_NSH;
487     else
488       return clib_error_return (0, "parse error: '%U'",
489                                 format_unformat_error, line_input);
490   }
491
492   unformat_free (line_input);
493
494   if (local_set == 0)
495     return clib_error_return (0, "tunnel local address not specified");
496
497   if (remote_set == 0)
498     return clib_error_return (0, "tunnel remote address not specified");
499
500   if (ipv4_set && ipv6_set)
501     return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
502
503   if ((ipv4_set && memcmp(&local.ip4, &remote.ip4, sizeof(local.ip4)) == 0) ||
504       (ipv6_set && memcmp(&local.ip6, &remote.ip6, sizeof(local.ip6)) == 0))
505     return clib_error_return (0, "src and dst addresses are identical");
506
507   if (vni_set == 0)
508     return clib_error_return (0, "vni not specified");
509
510   memset (a, 0, sizeof (*a));
511
512   a->is_add = is_add;
513   a->is_ip6 = ipv6_set;
514
515 #define _(x) a->x = x;
516   foreach_gpe_copy_field;
517   if (ipv4_set) foreach_copy_ipv4
518   else          foreach_copy_ipv6
519 #undef _
520
521   rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
522
523   switch(rv)
524     {
525     case 0:
526       vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
527       break;
528     case VNET_API_ERROR_INVALID_DECAP_NEXT:
529       return clib_error_return (0, "invalid decap-next...");
530
531     case VNET_API_ERROR_TUNNEL_EXIST:
532       return clib_error_return (0, "tunnel already exists...");
533
534     case VNET_API_ERROR_NO_SUCH_ENTRY:
535       return clib_error_return (0, "tunnel does not exist...");
536
537     default:
538       return clib_error_return
539         (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
540     }
541
542   return 0;
543 }
544
545 VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
546   .path = "create vxlan-gpe tunnel",
547   .short_help =
548   "create vxlan-gpe tunnel local <local-addr> remote <remote-addr>"
549   " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
550   " [encap-vrf-id <nn>] [decap-vrf-id <nn>]"
551   " [del]\n",
552   .function = vxlan_gpe_add_del_tunnel_command_fn,
553 };
554
555 static clib_error_t *
556 show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
557                                 unformat_input_t * input,
558                                 vlib_cli_command_t * cmd)
559 {
560   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
561   vxlan_gpe_tunnel_t * t;
562
563   if (pool_elts (gm->tunnels) == 0)
564     vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
565
566   pool_foreach (t, gm->tunnels,
567   ({
568     vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
569   }));
570
571   return 0;
572 }
573
574 VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
575     .path = "show vxlan-gpe",
576     .function = show_vxlan_gpe_tunnel_command_fn,
577 };
578
579 clib_error_t *vxlan_gpe_init (vlib_main_t *vm)
580 {
581   vxlan_gpe_main_t *gm = &vxlan_gpe_main;
582
583   gm->vnet_main = vnet_get_main();
584   gm->vlib_main = vm;
585
586   gm->vxlan4_gpe_tunnel_by_key
587     = hash_create_mem (0, sizeof(vxlan4_gpe_tunnel_key_t), sizeof (uword));
588
589   gm->vxlan6_gpe_tunnel_by_key
590     = hash_create_mem (0, sizeof(vxlan6_gpe_tunnel_key_t), sizeof (uword));
591
592
593   udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe,
594                          vxlan4_gpe_input_node.index, 1 /* is_ip4 */);
595   udp_register_dst_port (vm, UDP_DST_PORT_vxlan6_gpe,
596                          vxlan6_gpe_input_node.index, 0 /* is_ip4 */);
597   return 0;
598 }
599
600 VLIB_INIT_FUNCTION(vxlan_gpe_init);
601