Merge "Add vpe-api message to get vxlan tunnel details."
[vpp.git] / vnet / vnet / vxlan / vxlan.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/vxlan.h>
16
17 vxlan_main_t vxlan_main;
18
19 static u8 * format_decap_next (u8 * s, va_list * args)
20 {
21   u32 next_index = va_arg (*args, u32);
22
23   switch (next_index)
24     {
25     case VXLAN_INPUT_NEXT_DROP:
26       return format (s, "drop");
27     case VXLAN_INPUT_NEXT_L2_INPUT:
28       return format (s, "l2");
29     case VXLAN_INPUT_NEXT_IP4_INPUT:
30       return format (s, "ip4");
31     case VXLAN_INPUT_NEXT_IP6_INPUT:
32       return format (s, "ip6");
33     default:
34       return format (s, "unknown %d", next_index);
35     }
36   return s;
37 }
38
39 u8 * format_vxlan_tunnel (u8 * s, va_list * args)
40 {
41   vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *);
42   vxlan_main_t * ngm = &vxlan_main;
43
44   s = format (s, 
45               "[%d] %U (src) %U (dst) vni %d encap_fib_index %d",
46               t - ngm->tunnels,
47               format_ip4_address, &t->src,
48               format_ip4_address, &t->dst,
49               t->vni,
50               t->encap_fib_index);
51   s = format (s, " decap_next %U\n", format_decap_next, t->decap_next_index);
52   return s;
53 }
54
55 static u8 * format_vxlan_name (u8 * s, va_list * args)
56 {
57   u32 dev_instance = va_arg (*args, u32);
58   return format (s, "vxlan_tunnel%d", dev_instance);
59 }
60
61 static uword dummy_interface_tx (vlib_main_t * vm,
62                                  vlib_node_runtime_t * node,
63                                  vlib_frame_t * frame)
64 {
65   clib_warning ("you shouldn't be here, leaking buffers...");
66   return frame->n_vectors;
67 }
68
69 VNET_DEVICE_CLASS (vxlan_device_class,static) = {
70   .name = "VXLAN",
71   .format_device_name = format_vxlan_name,
72   .format_tx_trace = format_vxlan_encap_trace,
73   .tx_function = dummy_interface_tx,
74 };
75
76 static uword dummy_set_rewrite (vnet_main_t * vnm,
77                                 u32 sw_if_index,
78                                 u32 l3_type,
79                                 void * dst_address,
80                                 void * rewrite,
81                                 uword max_rewrite_bytes)
82 {
83   return 0;
84 }
85
86 static u8 * format_vxlan_header_with_length (u8 * s, va_list * args)
87 {
88   u32 dev_instance = va_arg (*args, u32);
89   s = format (s, "unimplemented dev %u", dev_instance);
90   return s;
91 }
92
93 VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = {
94   .name = "VXLAN",
95   .format_header = format_vxlan_header_with_length,
96   .set_rewrite = dummy_set_rewrite,
97 };
98
99 #define foreach_copy_field                      \
100 _(src.as_u32)                                   \
101 _(dst.as_u32)                                   \
102 _(vni)                                          \
103 _(encap_fib_index)                              \
104 _(decap_next_index)
105
106 static int vxlan_rewrite (vxlan_tunnel_t * t)
107 {
108   u8 *rw = 0;
109   ip4_header_t * ip0;
110   ip4_vxlan_header_t * h0;
111   int len = sizeof (*h0);
112
113   vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
114
115   h0 = (ip4_vxlan_header_t *) rw;
116
117   /* Fixed portion of the (outer) ip4 header */
118   ip0 = &h0->ip4;
119   ip0->ip_version_and_header_length = 0x45;
120   ip0->ttl = 254;
121   ip0->protocol = IP_PROTOCOL_UDP;
122
123   /* we fix up the ip4 header length and checksum after-the-fact */
124   ip0->src_address.as_u32 = t->src.as_u32;
125   ip0->dst_address.as_u32 = t->dst.as_u32;
126   ip0->checksum = ip4_header_checksum (ip0);
127
128   /* UDP header, randomize src port on something, maybe? */
129   h0->udp.src_port = clib_host_to_net_u16 (4789);
130   h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
131
132   /* VXLAN header */
133   vnet_set_vni_and_flags(&h0->vxlan, t->vni);
134
135   t->rewrite = rw;
136   return (0);
137 }
138
139 int vnet_vxlan_add_del_tunnel 
140 (vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
141 {
142   vxlan_main_t * vxm = &vxlan_main;
143   vxlan_tunnel_t *t = 0;
144   vnet_main_t * vnm = vxm->vnet_main;
145   vnet_hw_interface_t * hi;
146   uword * p;
147   u32 hw_if_index = ~0;
148   u32 sw_if_index = ~0;
149   int rv;
150   vxlan_tunnel_key_t key;
151   
152   key.src = a->dst.as_u32; /* decap src in key is encap dst in config */
153   key.vni = clib_host_to_net_u32 (a->vni << 8);
154
155   p = hash_get (vxm->vxlan_tunnel_by_key, key.as_u64);
156   
157   if (a->is_add)
158     {
159       /* adding a tunnel: tunnel must not already exist */
160       if (p)
161         return VNET_API_ERROR_TUNNEL_EXIST;
162
163       if (a->decap_next_index == ~0)
164           a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
165
166       if (a->decap_next_index >= VXLAN_INPUT_N_NEXT)
167         return VNET_API_ERROR_INVALID_DECAP_NEXT;
168       
169       pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
170       memset (t, 0, sizeof (*t));
171       
172       /* copy from arg structure */
173 #define _(x) t->x = a->x;
174       foreach_copy_field;
175 #undef _
176       
177       rv = vxlan_rewrite (t);
178
179       if (rv)
180         {
181           pool_put (vxm->tunnels, t);
182           return rv;
183         }
184
185       hash_set (vxm->vxlan_tunnel_by_key, key.as_u64, t - vxm->tunnels);
186       
187       if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0)
188         {
189           vnet_interface_main_t * im = &vnm->interface_main;
190           hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
191             [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1];
192           _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
193           
194           hi = vnet_get_hw_interface (vnm, hw_if_index);
195           hi->dev_instance = t - vxm->tunnels;
196           hi->hw_instance = hi->dev_instance;
197
198           /* clear old stats of freed tunnel before reuse */
199           sw_if_index = hi->sw_if_index;
200           vnet_interface_counter_lock(im);
201           vlib_zero_combined_counter 
202             (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
203           vlib_zero_combined_counter 
204             (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
205           vlib_zero_simple_counter 
206             (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
207           vnet_interface_counter_unlock(im);
208         }
209       else 
210         {
211           hw_if_index = vnet_register_interface
212             (vnm, vxlan_device_class.index, t - vxm->tunnels,
213              vxlan_hw_class.index, t - vxm->tunnels);
214           hi = vnet_get_hw_interface (vnm, hw_if_index);
215           hi->output_node_index = vxlan_encap_node.index;
216         }
217       
218       t->hw_if_index = hw_if_index;
219       t->sw_if_index = sw_if_index = hi->sw_if_index;
220       
221       vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
222       vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
223
224       if (a->decap_next_index == VXLAN_INPUT_NEXT_L2_INPUT)
225         {
226           l2input_main_t * l2im = &l2input_main;
227           /* setup l2 input config with l2 feature and bd 0 to drop packet */
228           vec_validate (l2im->configs, sw_if_index);
229           l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
230           l2im->configs[sw_if_index].bd_index = 0;
231         }
232       vnet_sw_interface_set_flags (vnm, sw_if_index, 
233                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
234     }
235   else
236     {
237       /* deleting a tunnel: tunnel must exist */
238       if (!p) 
239         return VNET_API_ERROR_NO_SUCH_ENTRY;
240
241       t = pool_elt_at_index (vxm->tunnels, p[0]);
242
243       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
244       /* make sure tunnel is removed from l2 bd or xconnect */
245       set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
246       vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
247
248       vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
249
250       hash_unset (vxm->vxlan_tunnel_by_key, key.as_u64);
251
252       vec_free (t->rewrite);
253       t->rewrite = vxlan_dummy_rewrite;
254       pool_put (vxm->tunnels, t);
255     }
256
257   if (sw_if_indexp)
258       *sw_if_indexp = sw_if_index;
259
260   return 0;
261 }
262
263 static u32 fib_index_from_fib_id (u32 fib_id)
264 {
265   ip4_main_t * im = &ip4_main;
266   uword * p;
267
268   p = hash_get (im->fib_index_by_table_id, fib_id);
269   if (!p)
270     return ~0;
271
272   return p[0];
273 }
274
275 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
276 {
277   u32 * result = va_arg (*args, u32 *);
278   u32 tmp;
279   
280   if (unformat (input, "l2"))
281     *result = VXLAN_INPUT_NEXT_L2_INPUT;
282   else if (unformat (input, "drop"))
283     *result = VXLAN_INPUT_NEXT_DROP;
284   else if (unformat (input, "ip4"))
285     *result = VXLAN_INPUT_NEXT_IP4_INPUT;
286   else if (unformat (input, "ip6"))
287     *result = VXLAN_INPUT_NEXT_IP6_INPUT;
288   else if (unformat (input, "%d", &tmp))
289     *result = tmp;
290   else
291     return 0;
292   return 1;
293 }
294
295 static clib_error_t *
296 vxlan_add_del_tunnel_command_fn (vlib_main_t * vm,
297                                    unformat_input_t * input,
298                                    vlib_cli_command_t * cmd)
299 {
300   unformat_input_t _line_input, * line_input = &_line_input;
301   ip4_address_t src, dst;
302   u8 is_add = 1;
303   u8 src_set = 0;
304   u8 dst_set = 0;
305   u32 encap_fib_index = 0;
306   u32 decap_next_index = ~0;
307   u32 vni = 0;
308   u32 tmp;
309   int rv;
310   vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
311   
312   /* Get a line of input. */
313   if (! unformat_user (input, unformat_line_input, line_input))
314     return 0;
315
316   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
317     if (unformat (line_input, "del"))
318       is_add = 0;
319     else if (unformat (line_input, "src %U", 
320                        unformat_ip4_address, &src))
321       src_set = 1;
322     else if (unformat (line_input, "dst %U",
323                        unformat_ip4_address, &dst))
324       dst_set = 1;
325     else if (unformat (line_input, "encap-vrf-id %d", &tmp))
326       {
327         encap_fib_index = fib_index_from_fib_id (tmp);
328         if (encap_fib_index == ~0)
329           return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
330       }
331     else if (unformat (line_input, "decap-next %U", unformat_decap_next, 
332                        &decap_next_index))
333       ;
334     else if (unformat (line_input, "vni %d", &vni))
335       {
336         if (vni >> 24)  
337           return clib_error_return (0, "vni %d out of range", vni);
338       }
339     else 
340       return clib_error_return (0, "parse error: '%U'", 
341                                 format_unformat_error, line_input);
342   }
343
344   unformat_free (line_input);
345
346   if (src_set == 0)
347     return clib_error_return (0, "tunnel src address not specified");
348
349   if (dst_set == 0)
350     return clib_error_return (0, "tunnel dst address not specified");
351
352   if (vni == 0)
353     return clib_error_return (0, "vni not specified");
354
355   memset (a, 0, sizeof (*a));
356
357   a->is_add = is_add;
358
359 #define _(x) a->x = x;
360   foreach_copy_field;
361 #undef _
362   
363   rv = vnet_vxlan_add_del_tunnel (a, 0 /* hw_if_indexp */);
364
365   switch(rv)
366     {
367     case 0:
368       break;
369     case VNET_API_ERROR_INVALID_DECAP_NEXT:
370       return clib_error_return (0, "invalid decap-next...");
371
372     case VNET_API_ERROR_TUNNEL_EXIST:
373       return clib_error_return (0, "tunnel already exists...");
374
375     case VNET_API_ERROR_NO_SUCH_ENTRY:
376       return clib_error_return (0, "tunnel does not exist...");
377
378     default:
379       return clib_error_return 
380         (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
381     }
382
383   return 0;
384 }
385
386 VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
387   .path = "create vxlan tunnel",
388   .short_help = 
389   "create vxlan tunnel src <local-vtep-addr> dst <remote-vtep-addr> vni <nn>" 
390   " [encap-vrf-id <nn>] [decap-next [l2|ip4|ip6] [del]\n",
391   .function = vxlan_add_del_tunnel_command_fn,
392 };
393
394 static clib_error_t *
395 show_vxlan_tunnel_command_fn (vlib_main_t * vm,
396                                 unformat_input_t * input,
397                                 vlib_cli_command_t * cmd)
398 {
399   vxlan_main_t * vxm = &vxlan_main;
400   vxlan_tunnel_t * t;
401   
402   if (pool_elts (vxm->tunnels) == 0)
403     vlib_cli_output (vm, "No vxlan tunnels configured...");
404
405   pool_foreach (t, vxm->tunnels,
406   ({
407     vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
408   }));
409   
410   return 0;
411 }
412
413 VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
414     .path = "show vxlan tunnel",
415     .function = show_vxlan_tunnel_command_fn,
416 };
417
418 clib_error_t *vxlan_init (vlib_main_t *vm)
419 {
420   vxlan_main_t * vxm = &vxlan_main;
421   ip4_vxlan_header_t * hdr;
422   ip4_header_t * ip;
423   
424   vxm->vnet_main = vnet_get_main();
425   vxm->vlib_main = vm;
426
427   /* init dummy rewrite string for deleted vxlan tunnels */
428   _vec_len(vxlan_dummy_rewrite) = sizeof(ip4_vxlan_header_t);
429   hdr = (ip4_vxlan_header_t *) vxlan_dummy_rewrite;
430   ip = &hdr->ip4;
431   /* minimal rewrite setup, see vxlan_rewite() above as reference */
432   ip->ip_version_and_header_length = 0x45;
433   ip->checksum = ip4_header_checksum (ip);
434  
435   udp_register_dst_port (vm, UDP_DST_PORT_vxlan, 
436                          vxlan_input_node.index, 1 /* is_ip4 */);
437   return 0;
438 }
439
440 VLIB_INIT_FUNCTION(vxlan_init);
441