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