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