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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/vxlan/vxlan.h>
17 vxlan_main_t vxlan_main;
19 static u8 * format_decap_next (u8 * s, va_list * args)
21 u32 next_index = va_arg (*args, u32);
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");
34 return format (s, "unknown %d", next_index);
39 u8 * format_vxlan_tunnel (u8 * s, va_list * args)
41 vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *);
42 vxlan_main_t * ngm = &vxlan_main;
45 "[%d] %U (src) %U (dst) vni %d encap_fib_index %d",
47 format_ip4_address, &t->src,
48 format_ip4_address, &t->dst,
51 s = format (s, " decap_next %U\n", format_decap_next, t->decap_next_index);
55 static u8 * format_vxlan_name (u8 * s, va_list * args)
57 u32 dev_instance = va_arg (*args, u32);
58 return format (s, "vxlan_tunnel%d", dev_instance);
61 static uword dummy_interface_tx (vlib_main_t * vm,
62 vlib_node_runtime_t * node,
65 clib_warning ("you shouldn't be here, leaking buffers...");
66 return frame->n_vectors;
69 VNET_DEVICE_CLASS (vxlan_device_class,static) = {
71 .format_device_name = format_vxlan_name,
72 .format_tx_trace = format_vxlan_encap_trace,
73 .tx_function = dummy_interface_tx,
76 static uword dummy_set_rewrite (vnet_main_t * vnm,
81 uword max_rewrite_bytes)
86 static u8 * format_vxlan_header_with_length (u8 * s, va_list * args)
88 u32 dev_instance = va_arg (*args, u32);
89 s = format (s, "unimplemented dev %u", dev_instance);
93 VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = {
95 .format_header = format_vxlan_header_with_length,
96 .set_rewrite = dummy_set_rewrite,
99 #define foreach_copy_field \
106 static int vxlan_rewrite (vxlan_tunnel_t * t)
110 ip4_vxlan_header_t * h0;
111 int len = sizeof (*h0);
113 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
115 h0 = (ip4_vxlan_header_t *) rw;
117 /* Fixed portion of the (outer) ip4 header */
119 ip0->ip_version_and_header_length = 0x45;
121 ip0->protocol = IP_PROTOCOL_UDP;
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);
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);
133 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
139 int vnet_vxlan_add_del_tunnel
140 (vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
142 vxlan_main_t * vxm = &vxlan_main;
143 vxlan_tunnel_t *t = 0;
144 vnet_main_t * vnm = vxm->vnet_main;
145 ip4_main_t * im4 = &ip4_main;
146 vnet_hw_interface_t * hi;
148 u32 hw_if_index = ~0;
149 u32 sw_if_index = ~0;
151 vxlan_tunnel_key_t key;
153 key.src = a->dst.as_u32; /* decap src in key is encap dst in config */
154 key.vni = clib_host_to_net_u32 (a->vni << 8);
156 p = hash_get (vxm->vxlan_tunnel_by_key, key.as_u64);
160 /* adding a tunnel: tunnel must not already exist */
162 return VNET_API_ERROR_TUNNEL_EXIST;
164 if (a->decap_next_index == ~0)
165 a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
167 if (a->decap_next_index >= VXLAN_INPUT_N_NEXT)
168 return VNET_API_ERROR_INVALID_DECAP_NEXT;
170 pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
171 memset (t, 0, sizeof (*t));
173 /* copy from arg structure */
174 #define _(x) t->x = a->x;
178 rv = vxlan_rewrite (t);
182 pool_put (vxm->tunnels, t);
186 hash_set (vxm->vxlan_tunnel_by_key, key.as_u64, t - vxm->tunnels);
188 if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0)
190 vnet_interface_main_t * im = &vnm->interface_main;
191 hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
192 [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1];
193 _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
195 hi = vnet_get_hw_interface (vnm, hw_if_index);
196 hi->dev_instance = t - vxm->tunnels;
197 hi->hw_instance = hi->dev_instance;
199 /* clear old stats of freed tunnel before reuse */
200 sw_if_index = hi->sw_if_index;
201 vnet_interface_counter_lock(im);
202 vlib_zero_combined_counter
203 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
204 vlib_zero_combined_counter
205 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
206 vlib_zero_simple_counter
207 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
208 vnet_interface_counter_unlock(im);
212 hw_if_index = vnet_register_interface
213 (vnm, vxlan_device_class.index, t - vxm->tunnels,
214 vxlan_hw_class.index, t - vxm->tunnels);
215 hi = vnet_get_hw_interface (vnm, hw_if_index);
216 hi->output_node_index = vxlan_encap_node.index;
219 t->hw_if_index = hw_if_index;
220 t->sw_if_index = sw_if_index = hi->sw_if_index;
222 vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
223 vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
225 if (a->decap_next_index == VXLAN_INPUT_NEXT_L2_INPUT)
227 l2input_main_t * l2im = &l2input_main;
228 /* setup l2 input config with l2 feature and bd 0 to drop packet */
229 vec_validate (l2im->configs, sw_if_index);
230 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
231 l2im->configs[sw_if_index].bd_index = 0;
233 vnet_sw_interface_set_flags (vnm, sw_if_index,
234 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
235 vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
236 im4->fib_index_by_sw_if_index[sw_if_index] = t->encap_fib_index;
240 /* deleting a tunnel: tunnel must exist */
242 return VNET_API_ERROR_NO_SUCH_ENTRY;
244 t = pool_elt_at_index (vxm->tunnels, p[0]);
246 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
247 /* make sure tunnel is removed from l2 bd or xconnect */
248 set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
249 vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
251 vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
253 hash_unset (vxm->vxlan_tunnel_by_key, key.as_u64);
255 vec_free (t->rewrite);
256 t->rewrite = vxlan_dummy_rewrite;
257 pool_put (vxm->tunnels, t);
261 *sw_if_indexp = sw_if_index;
266 static u32 fib_index_from_fib_id (u32 fib_id)
268 ip4_main_t * im = &ip4_main;
271 p = hash_get (im->fib_index_by_table_id, fib_id);
278 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
280 u32 * result = va_arg (*args, u32 *);
283 if (unformat (input, "l2"))
284 *result = VXLAN_INPUT_NEXT_L2_INPUT;
285 else if (unformat (input, "drop"))
286 *result = VXLAN_INPUT_NEXT_DROP;
287 else if (unformat (input, "ip4"))
288 *result = VXLAN_INPUT_NEXT_IP4_INPUT;
289 else if (unformat (input, "ip6"))
290 *result = VXLAN_INPUT_NEXT_IP6_INPUT;
291 else if (unformat (input, "%d", &tmp))
298 static clib_error_t *
299 vxlan_add_del_tunnel_command_fn (vlib_main_t * vm,
300 unformat_input_t * input,
301 vlib_cli_command_t * cmd)
303 unformat_input_t _line_input, * line_input = &_line_input;
304 ip4_address_t src, dst;
308 u32 encap_fib_index = 0;
309 u32 decap_next_index = ~0;
313 vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
315 /* Get a line of input. */
316 if (! unformat_user (input, unformat_line_input, line_input))
319 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
320 if (unformat (line_input, "del"))
322 else if (unformat (line_input, "src %U",
323 unformat_ip4_address, &src))
325 else if (unformat (line_input, "dst %U",
326 unformat_ip4_address, &dst))
328 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
330 encap_fib_index = fib_index_from_fib_id (tmp);
331 if (encap_fib_index == ~0)
332 return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
334 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
337 else if (unformat (line_input, "vni %d", &vni))
340 return clib_error_return (0, "vni %d out of range", vni);
343 return clib_error_return (0, "parse error: '%U'",
344 format_unformat_error, line_input);
347 unformat_free (line_input);
350 return clib_error_return (0, "tunnel src address not specified");
353 return clib_error_return (0, "tunnel dst address not specified");
356 return clib_error_return (0, "vni not specified");
358 memset (a, 0, sizeof (*a));
362 #define _(x) a->x = x;
366 rv = vnet_vxlan_add_del_tunnel (a, 0 /* hw_if_indexp */);
372 case VNET_API_ERROR_INVALID_DECAP_NEXT:
373 return clib_error_return (0, "invalid decap-next...");
375 case VNET_API_ERROR_TUNNEL_EXIST:
376 return clib_error_return (0, "tunnel already exists...");
378 case VNET_API_ERROR_NO_SUCH_ENTRY:
379 return clib_error_return (0, "tunnel does not exist...");
382 return clib_error_return
383 (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
389 VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
390 .path = "create vxlan tunnel",
392 "create vxlan tunnel src <local-vtep-addr> dst <remote-vtep-addr> vni <nn>"
393 " [encap-vrf-id <nn>] [decap-next [l2|ip4|ip6] [del]\n",
394 .function = vxlan_add_del_tunnel_command_fn,
397 static clib_error_t *
398 show_vxlan_tunnel_command_fn (vlib_main_t * vm,
399 unformat_input_t * input,
400 vlib_cli_command_t * cmd)
402 vxlan_main_t * vxm = &vxlan_main;
405 if (pool_elts (vxm->tunnels) == 0)
406 vlib_cli_output (vm, "No vxlan tunnels configured...");
408 pool_foreach (t, vxm->tunnels,
410 vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
416 VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
417 .path = "show vxlan tunnel",
418 .function = show_vxlan_tunnel_command_fn,
421 clib_error_t *vxlan_init (vlib_main_t *vm)
423 vxlan_main_t * vxm = &vxlan_main;
424 ip4_vxlan_header_t * hdr;
427 vxm->vnet_main = vnet_get_main();
430 /* init dummy rewrite string for deleted vxlan tunnels */
431 _vec_len(vxlan_dummy_rewrite) = sizeof(ip4_vxlan_header_t);
432 hdr = (ip4_vxlan_header_t *) vxlan_dummy_rewrite;
434 /* minimal rewrite setup, see vxlan_rewite() above as reference */
435 ip->ip_version_and_header_length = 0x45;
436 ip->checksum = ip4_header_checksum (ip);
438 udp_register_dst_port (vm, UDP_DST_PORT_vxlan,
439 vxlan_input_node.index, 1 /* is_ip4 */);
443 VLIB_INIT_FUNCTION(vxlan_init);