bee6e7c9243ed2abecc4f1f9be81a3a1c854fbad
[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
17 vxlan_gpe_main_t vxlan_gpe_main;
18
19 u8 * format_vxlan_gpe_tunnel (u8 * s, va_list * args)
20 {
21   vxlan_gpe_tunnel_t * t = va_arg (*args, vxlan_gpe_tunnel_t *);
22   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
23
24   s = format (s, "[%d] local: %U remote: %U ",
25               t - gm->tunnels,
26               format_ip4_address, &t->local,
27               format_ip4_address, &t->remote);    
28
29   switch (t->protocol)
30     {
31     case VXLAN_GPE_PROTOCOL_IP4:
32       s = format (s, "next-protocol ip4");
33     case VXLAN_GPE_PROTOCOL_IP6:
34       s = format (s, "next-protocol ip6");
35     case VXLAN_GPE_PROTOCOL_ETHERNET:
36       s = format (s, "next-protocol ethernet");
37     case VXLAN_GPE_PROTOCOL_NSH:
38       s = format (s, "next-protocol nsh");
39     default:
40       s = format (s, "next-protocol unknown %d", t->protocol);
41     }
42
43   s = format (s, " fibs: (encap %d, decap %d)",
44               t->encap_fib_index,
45               t->decap_fib_index);
46
47   s = format (s, "  vxlan VNI %d ", t->vni);
48
49   return s;
50 }
51
52 static u8 * format_vxlan_gpe_name (u8 * s, va_list * args)
53 {
54   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
55   u32 i = va_arg (*args, u32);
56   u32 show_dev_instance = ~0;
57
58   if (i < vec_len (gm->dev_inst_by_real))
59     show_dev_instance = gm->dev_inst_by_real[i];
60
61   if (show_dev_instance != ~0)
62     i = show_dev_instance;
63
64   return format (s, "vxlan_gpe_tunnel%d", i);
65 }
66
67 static int vxlan_gpe_name_renumber (vnet_hw_interface_t * hi,
68                                         u32 new_dev_instance)
69 {
70   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
71
72   vec_validate_init_empty (gm->dev_inst_by_real, hi->dev_instance, ~0);
73
74   gm->dev_inst_by_real [hi->dev_instance] = new_dev_instance;
75
76   return 0;
77 }
78
79 static uword dummy_interface_tx (vlib_main_t * vm,
80                                  vlib_node_runtime_t * node,
81                                  vlib_frame_t * frame)
82 {
83   clib_warning ("you shouldn't be here, leaking buffers...");
84   return frame->n_vectors;
85 }
86
87 static uword dummy_set_rewrite (vnet_main_t * vnm,
88                                 u32 sw_if_index,
89                                 u32 l3_type,
90                                 void * dst_address,
91                                 void * rewrite,
92                                 uword max_rewrite_bytes)
93 {
94   return 0;
95 }
96
97 VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
98   .name = "VXLAN_GPE",
99   .format_device_name = format_vxlan_gpe_name,
100   .format_tx_trace = format_vxlan_gpe_encap_trace,
101   .tx_function = dummy_interface_tx,
102   .name_renumber = vxlan_gpe_name_renumber,
103 };
104
105 static u8 * format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
106 {
107   u32 dev_instance = va_arg (*args, u32);
108   s = format (s, "unimplemented dev %u", dev_instance);
109   return s;
110 }
111
112 VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
113   .name = "VXLAN_GPE",
114   .format_header = format_vxlan_gpe_header_with_length,
115   .set_rewrite = dummy_set_rewrite,
116 };
117
118
119 #define foreach_gpe_copy_field                  \
120 _(local.as_u32)                                 \
121 _(remote.as_u32)                                \
122 _(vni)                                          \
123 _(protocol)                                \
124 _(encap_fib_index)                              \
125 _(decap_fib_index)                              
126
127 #define foreach_copy_field                      \
128 _(src.as_u32)                                   \
129 _(dst.as_u32)                                   \
130 _(vni)                                          \
131 _(encap_fib_index)                              \
132 _(decap_fib_index)                              \
133 _(decap_next_index)
134
135
136
137 static int vxlan_gpe_rewrite (vxlan_gpe_tunnel_t * t)
138 {
139   u8 *rw = 0;
140   ip4_header_t * ip0;
141   ip4_vxlan_gpe_header_t * h0;
142   int len;
143
144   len = sizeof (*h0);
145
146   vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
147
148   h0 = (ip4_vxlan_gpe_header_t *) rw;
149
150   /* Fixed portion of the (outer) ip4 header */
151   ip0 = &h0->ip4;
152   ip0->ip_version_and_header_length = 0x45;
153   ip0->ttl = 254;
154   ip0->protocol = IP_PROTOCOL_UDP;
155
156   /* we fix up the ip4 header length and checksum after-the-fact */
157   ip0->src_address.as_u32 = t->local.as_u32; 
158   ip0->dst_address.as_u32 = t->remote.as_u32; 
159   ip0->checksum = ip4_header_checksum (ip0);
160
161   /* UDP header, randomize src port on something, maybe? */
162   h0->udp.src_port = clib_host_to_net_u16 (4790);
163   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
164
165   /* VXLAN header. Are we having fun yet? */
166   h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
167   h0->vxlan.ver_res = VXLAN_GPE_VERSION;
168   h0->vxlan.protocol = VXLAN_GPE_PROTOCOL_IP4;
169   h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
170
171   t->rewrite = rw;
172   return (0);
173 }
174
175 int vnet_vxlan_gpe_add_del_tunnel 
176 (vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
177 {
178   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
179   vxlan_gpe_tunnel_t *t = 0;
180   vnet_main_t * vnm = gm->vnet_main;
181   vnet_hw_interface_t * hi;
182   uword * p;
183   u32 hw_if_index = ~0;
184   u32 sw_if_index = ~0;
185   int rv;
186   vxlan_gpe_tunnel_key_t key, *key_copy;
187   hash_pair_t *hp;
188   
189   key.local = a->local.as_u32; 
190   key.remote = a->remote.as_u32; 
191   key.vni = clib_host_to_net_u32 (a->vni << 8);
192   key.pad = 0;
193
194   p = hash_get_mem (gm->vxlan_gpe_tunnel_by_key, &key);
195   
196   if (a->is_add)
197     {
198       /* adding a tunnel: tunnel must not already exist */
199       if (p) 
200         return VNET_API_ERROR_INVALID_VALUE;
201       
202       if (a->decap_next_index >= VXLAN_GPE_INPUT_N_NEXT)
203         return VNET_API_ERROR_INVALID_DECAP_NEXT;
204       
205       pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
206       memset (t, 0, sizeof (*t));
207       
208       /* copy from arg structure */
209 #define _(x) t->x = a->x;
210       foreach_gpe_copy_field;
211 #undef _
212
213       rv = vxlan_gpe_rewrite (t);
214
215       if (rv)
216         {
217           pool_put (gm->tunnels, t);
218           return rv;
219         }
220
221       key_copy = clib_mem_alloc (sizeof (*key_copy));
222       clib_memcpy (key_copy, &key, sizeof (*key_copy));
223
224       hash_set_mem (gm->vxlan_gpe_tunnel_by_key, key_copy, 
225                     t - gm->tunnels);
226       
227       if (vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
228         {
229           hw_if_index = gm->free_vxlan_gpe_tunnel_hw_if_indices
230             [vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices)-1];
231           _vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
232           
233           hi = vnet_get_hw_interface (vnm, hw_if_index);
234           hi->dev_instance = t - gm->tunnels;
235           hi->hw_instance = hi->dev_instance;
236         }
237       else 
238         {
239           hw_if_index = vnet_register_interface
240             (vnm, vxlan_gpe_device_class.index, t - gm->tunnels,
241              vxlan_gpe_hw_class.index, t - gm->tunnels);
242           hi = vnet_get_hw_interface (vnm, hw_if_index);
243           hi->output_node_index = vxlan_gpe_encap_node.index;
244         }
245       
246       t->hw_if_index = hw_if_index;
247       t->sw_if_index = sw_if_index = hi->sw_if_index;
248       
249       vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 
250                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
251     }
252   else
253     {
254       /* deleting a tunnel: tunnel must exist */
255       if (!p) 
256         return VNET_API_ERROR_NO_SUCH_ENTRY;
257
258       t = pool_elt_at_index (gm->tunnels, p[0]);
259
260       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
261       vec_add1 (gm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
262
263       hp = hash_get_pair (gm->vxlan_gpe_tunnel_by_key, &key);
264       key_copy = (void *)(hp->key);
265       hash_unset_mem (gm->vxlan_gpe_tunnel_by_key, &key);
266       clib_mem_free (key_copy);
267
268       vec_free (t->rewrite);
269       pool_put (gm->tunnels, t);
270     }
271
272   if (sw_if_indexp)
273       *sw_if_indexp = sw_if_index;
274
275   return 0;
276 }
277
278 static u32 fib_index_from_fib_id (u32 fib_id)
279 {
280   ip4_main_t * im = &ip4_main;
281   uword * p;
282
283   p = hash_get (im->fib_index_by_table_id, fib_id);
284   if (!p)
285     return ~0;
286
287   return p[0];
288 }
289
290 static uword unformat_gpe_decap_next (unformat_input_t * input, va_list * args)
291 {
292   u32 * result = va_arg (*args, u32 *);
293   u32 tmp;
294   
295   if (unformat (input, "drop"))
296     *result = VXLAN_GPE_INPUT_NEXT_DROP;
297   else if (unformat (input, "ip4"))
298     *result = VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
299   else if (unformat (input, "ip6"))
300     *result = VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
301   else if (unformat (input, "ethernet"))
302     *result = VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
303   else if (unformat (input, "%d", &tmp))
304     *result = tmp;
305   else
306     return 0;
307   return 1;
308 }
309
310 static clib_error_t *
311 vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
312                                    unformat_input_t * input,
313                                    vlib_cli_command_t * cmd)
314 {
315   unformat_input_t _line_input, * line_input = &_line_input;
316   u8 is_add = 1;
317   ip4_address_t local, remote;
318   u8 local_set = 0;
319   u8 remote_set = 0;
320   u32 encap_fib_index = 0;
321   u32 decap_fib_index = 0;
322   u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
323   u32 decap_next_index = VXLAN_GPE_INPUT_NEXT_IP4_INPUT; 
324   u32 vni;
325   u8 vni_set = 0;
326   int rv;
327   u32 tmp;
328   vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
329   
330   /* Get a line of input. */
331   if (! unformat_user (input, unformat_line_input, line_input))
332     return 0;
333
334   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
335     if (unformat (line_input, "del"))
336       is_add = 0;
337     else if (unformat (line_input, "local %U", 
338                        unformat_ip4_address, &local))
339       local_set = 1;
340     else if (unformat (line_input, "remote %U",
341                        unformat_ip4_address, &remote))
342       remote_set = 1;
343     else if (unformat (line_input, "encap-vrf-id %d", &tmp))
344       {
345         encap_fib_index = fib_index_from_fib_id (tmp);
346         if (encap_fib_index == ~0)
347           return clib_error_return (0, "nonexistent encap fib id %d", tmp);
348       }
349     else if (unformat (line_input, "decap-vrf-id %d", &tmp))
350       {
351         decap_fib_index = fib_index_from_fib_id (tmp);
352         if (decap_fib_index == ~0)
353           return clib_error_return (0, "nonexistent decap fib id %d", tmp);
354       }
355     else if (unformat (line_input, "decap-next %U", unformat_gpe_decap_next, 
356                        &decap_next_index))
357       ;
358     else if (unformat (line_input, "vni %d", &vni))
359       vni_set = 1;
360     else if (unformat(line_input, "next-ip4"))
361       protocol = VXLAN_GPE_PROTOCOL_IP4;
362     else if (unformat(line_input, "next-ip6"))
363       protocol = VXLAN_GPE_PROTOCOL_IP6;
364     else if (unformat(line_input, "next-ethernet"))
365       protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
366     else if (unformat(line_input, "next-nsh"))
367       protocol = VXLAN_GPE_PROTOCOL_NSH;
368     else 
369       return clib_error_return (0, "parse error: '%U'", 
370                                 format_unformat_error, line_input);
371   }
372
373   unformat_free (line_input);
374
375   if (local_set == 0)
376     return clib_error_return (0, "tunnel local address not specified");
377
378   if (remote_set == 0)
379     return clib_error_return (0, "tunnel remote address not specified");
380
381   if (vni_set == 0)
382     return clib_error_return (0, "vni not specified");
383
384   memset (a, 0, sizeof (*a));
385
386   a->is_add = is_add;
387
388 #define _(x) a->x = x;
389   foreach_gpe_copy_field;
390 #undef _
391
392   rv = vnet_vxlan_gpe_add_del_tunnel (a, 0 /* hw_if_indexp */);
393
394   switch(rv)
395     {
396     case 0:
397       break;
398     case VNET_API_ERROR_INVALID_DECAP_NEXT:
399       return clib_error_return (0, "invalid decap-next...");
400
401     case VNET_API_ERROR_TUNNEL_EXIST:
402       return clib_error_return (0, "tunnel already exists...");
403
404     case VNET_API_ERROR_NO_SUCH_ENTRY:
405       return clib_error_return (0, "tunnel does not exist...");
406
407     default:
408       return clib_error_return 
409         (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
410     }
411
412   return 0;
413 }
414
415 VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
416   .path = "create vxlan-gpe tunnel",
417   .short_help = 
418   "create vxlan-gpe tunnel local <ip4-addr> remote <ip4-addr>"
419   " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
420   " [encap-vrf-id <nn>] [decap-vrf-id <nn>]"
421   " [del]\n",
422   .function = vxlan_gpe_add_del_tunnel_command_fn,
423 };
424
425 static clib_error_t *
426 show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
427                                 unformat_input_t * input,
428                                 vlib_cli_command_t * cmd)
429 {
430   vxlan_gpe_main_t * gm = &vxlan_gpe_main;
431   vxlan_gpe_tunnel_t * t;
432   
433   if (pool_elts (gm->tunnels) == 0)
434     vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
435
436   pool_foreach (t, gm->tunnels,
437   ({
438     vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
439   }));
440   
441   return 0;
442 }
443
444 VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
445     .path = "show vxlan-gpe",
446     .function = show_vxlan_gpe_tunnel_command_fn,
447 };
448
449 clib_error_t *vxlan_gpe_init (vlib_main_t *vm)
450 {
451   vxlan_gpe_main_t *gm = &vxlan_gpe_main;
452   
453   gm->vnet_main = vnet_get_main();
454   gm->vlib_main = vm;
455   
456   gm->vxlan_gpe_tunnel_by_key 
457     = hash_create_mem (0, sizeof(vxlan_gpe_tunnel_key_t), sizeof (uword));
458
459   udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe, 
460                          vxlan_gpe_input_node.index, 1 /* is_ip4 */);
461   return 0;
462 }
463
464 VLIB_INIT_FUNCTION(vxlan_gpe_init);
465