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