blob: 133fc6f450ce7521d847196fbe02093a784fee70 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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>
Chris Luke99cb3352016-04-26 10:49:53 -040016#include <vnet/ip/format.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070017
18vxlan_main_t vxlan_main;
19
20static u8 * format_decap_next (u8 * s, va_list * args)
21{
22 u32 next_index = va_arg (*args, u32);
23
24 switch (next_index)
25 {
26 case VXLAN_INPUT_NEXT_DROP:
27 return format (s, "drop");
28 case VXLAN_INPUT_NEXT_L2_INPUT:
29 return format (s, "l2");
30 case VXLAN_INPUT_NEXT_IP4_INPUT:
31 return format (s, "ip4");
32 case VXLAN_INPUT_NEXT_IP6_INPUT:
33 return format (s, "ip6");
34 default:
35 return format (s, "unknown %d", next_index);
36 }
37 return s;
38}
39
40u8 * format_vxlan_tunnel (u8 * s, va_list * args)
41{
42 vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *);
43 vxlan_main_t * ngm = &vxlan_main;
44
45 s = format (s,
46 "[%d] %U (src) %U (dst) vni %d encap_fib_index %d",
47 t - ngm->tunnels,
Chris Luke99cb3352016-04-26 10:49:53 -040048 format_ip46_address, &t->src,
49 format_ip46_address, &t->dst,
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 t->vni,
51 t->encap_fib_index);
52 s = format (s, " decap_next %U\n", format_decap_next, t->decap_next_index);
53 return s;
54}
55
56static u8 * format_vxlan_name (u8 * s, va_list * args)
57{
58 u32 dev_instance = va_arg (*args, u32);
59 return format (s, "vxlan_tunnel%d", dev_instance);
60}
61
62static uword dummy_interface_tx (vlib_main_t * vm,
63 vlib_node_runtime_t * node,
64 vlib_frame_t * frame)
65{
66 clib_warning ("you shouldn't be here, leaking buffers...");
67 return frame->n_vectors;
68}
69
Pavel Kotucek988a7c42016-02-29 15:03:08 +010070static clib_error_t *
71vxlan_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
72{
73 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
74 vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
75 else
76 vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
77
78 return /* no error */ 0;
79}
80
Ed Warnickecb9cada2015-12-08 15:45:58 -070081VNET_DEVICE_CLASS (vxlan_device_class,static) = {
82 .name = "VXLAN",
83 .format_device_name = format_vxlan_name,
84 .format_tx_trace = format_vxlan_encap_trace,
85 .tx_function = dummy_interface_tx,
Pavel Kotucek988a7c42016-02-29 15:03:08 +010086 .admin_up_down_function = vxlan_interface_admin_up_down,
Ed Warnickecb9cada2015-12-08 15:45:58 -070087};
88
89static uword dummy_set_rewrite (vnet_main_t * vnm,
90 u32 sw_if_index,
91 u32 l3_type,
92 void * dst_address,
93 void * rewrite,
94 uword max_rewrite_bytes)
95{
96 return 0;
97}
98
99static u8 * format_vxlan_header_with_length (u8 * s, va_list * args)
100{
101 u32 dev_instance = va_arg (*args, u32);
102 s = format (s, "unimplemented dev %u", dev_instance);
103 return s;
104}
105
106VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = {
107 .name = "VXLAN",
108 .format_header = format_vxlan_header_with_length,
109 .set_rewrite = dummy_set_rewrite,
110};
111
112#define foreach_copy_field \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113_(vni) \
114_(encap_fib_index) \
115_(decap_next_index)
116
Chris Luke99cb3352016-04-26 10:49:53 -0400117#define foreach_copy_ipv4 { \
118 _(src.ip4.as_u32) \
119 _(dst.ip4.as_u32) \
120}
121
122#define foreach_copy_ipv6 { \
123 _(src.ip6.as_u64[0]) \
124 _(src.ip6.as_u64[1]) \
125 _(dst.ip6.as_u64[0]) \
126 _(dst.ip6.as_u64[1]) \
127}
128
129static int vxlan4_rewrite (vxlan_tunnel_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130{
131 u8 *rw = 0;
132 ip4_header_t * ip0;
133 ip4_vxlan_header_t * h0;
134 int len = sizeof (*h0);
135
136 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
137
138 h0 = (ip4_vxlan_header_t *) rw;
139
140 /* Fixed portion of the (outer) ip4 header */
141 ip0 = &h0->ip4;
142 ip0->ip_version_and_header_length = 0x45;
143 ip0->ttl = 254;
144 ip0->protocol = IP_PROTOCOL_UDP;
145
146 /* we fix up the ip4 header length and checksum after-the-fact */
Chris Luke99cb3352016-04-26 10:49:53 -0400147 ip0->src_address.as_u32 = t->src.ip4.as_u32;
148 ip0->dst_address.as_u32 = t->dst.ip4.as_u32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 ip0->checksum = ip4_header_checksum (ip0);
150
151 /* UDP header, randomize src port on something, maybe? */
152 h0->udp.src_port = clib_host_to_net_u16 (4789);
153 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
154
155 /* VXLAN header */
156 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
157
158 t->rewrite = rw;
159 return (0);
160}
161
Chris Luke99cb3352016-04-26 10:49:53 -0400162static int vxlan6_rewrite (vxlan_tunnel_t * t)
163{
164 u8 *rw = 0;
165 ip6_header_t * ip0;
166 ip6_vxlan_header_t * h0;
167 int len = sizeof (*h0);
168
169 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
170
171 h0 = (ip6_vxlan_header_t *) rw;
172
173 /* Fixed portion of the (outer) ip6 header */
174 ip0 = &h0->ip6;
175 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
176 ip0->hop_limit = 255;
177 ip0->protocol = IP_PROTOCOL_UDP;
178
179 ip0->src_address.as_u64[0] = t->src.ip6.as_u64[0];
180 ip0->src_address.as_u64[1] = t->src.ip6.as_u64[1];
181 ip0->dst_address.as_u64[0] = t->dst.ip6.as_u64[0];
182 ip0->dst_address.as_u64[1] = t->dst.ip6.as_u64[1];
183
184 /* UDP header, randomize src port on something, maybe? */
185 h0->udp.src_port = clib_host_to_net_u16 (4789);
186 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
187
188 /* VXLAN header */
189 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
190
191 t->rewrite = rw;
192 return (0);
193}
194
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195int vnet_vxlan_add_del_tunnel
196(vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
197{
198 vxlan_main_t * vxm = &vxlan_main;
199 vxlan_tunnel_t *t = 0;
200 vnet_main_t * vnm = vxm->vnet_main;
John Lo2d343742016-01-19 17:27:17 -0500201 ip4_main_t * im4 = &ip4_main;
Chris Luke99cb3352016-04-26 10:49:53 -0400202 ip6_main_t * im6 = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203 vnet_hw_interface_t * hi;
204 uword * p;
205 u32 hw_if_index = ~0;
206 u32 sw_if_index = ~0;
207 int rv;
Chris Luke99cb3352016-04-26 10:49:53 -0400208 vxlan4_tunnel_key_t key4;
209 vxlan6_tunnel_key_t key6;
John Lo3ef822e2016-06-07 09:14:07 -0400210 l2output_main_t * l2om = &l2output_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
Chris Luke99cb3352016-04-26 10:49:53 -0400212 if (!a->is_ip6) {
213 key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
214 key4.vni = clib_host_to_net_u32 (a->vni << 8);
215
216 p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64);
217 } else {
218 key6.src.as_u64[0] = a->dst.ip6.as_u64[0];
219 key6.src.as_u64[1] = a->dst.ip6.as_u64[1];
220 key6.vni = clib_host_to_net_u32 (a->vni << 8);
221
Chris Lukec7949152016-05-31 10:42:14 -0400222 p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6);
Chris Luke99cb3352016-04-26 10:49:53 -0400223 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224
225 if (a->is_add)
226 {
227 /* adding a tunnel: tunnel must not already exist */
228 if (p)
229 return VNET_API_ERROR_TUNNEL_EXIST;
230
231 if (a->decap_next_index == ~0)
232 a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
233
234 if (a->decap_next_index >= VXLAN_INPUT_N_NEXT)
235 return VNET_API_ERROR_INVALID_DECAP_NEXT;
236
237 pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
238 memset (t, 0, sizeof (*t));
239
240 /* copy from arg structure */
241#define _(x) t->x = a->x;
242 foreach_copy_field;
Chris Luke99cb3352016-04-26 10:49:53 -0400243 if (!a->is_ip6) foreach_copy_ipv4
244 else foreach_copy_ipv6
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245#undef _
246
Chris Lukec7949152016-05-31 10:42:14 -0400247 /* copy the key */
248 if (a->is_ip6)
249 {
250 t->key6 = clib_mem_alloc (sizeof(vxlan6_tunnel_key_t));
251 clib_memcpy (t->key6, &key6, sizeof(key6));
252 }
253 else
254 {
255 t->key4 = 0; /* not yet used */
256 }
Chris Luke99cb3352016-04-26 10:49:53 -0400257
258 if (!a->is_ip6) t->flags |= VXLAN_TUNNEL_IS_IPV4;
259
260 if (!a->is_ip6) {
261 rv = vxlan4_rewrite (t);
262 } else {
263 rv = vxlan6_rewrite (t);
264 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
266 if (rv)
267 {
268 pool_put (vxm->tunnels, t);
269 return rv;
270 }
271
Chris Luke99cb3352016-04-26 10:49:53 -0400272 if (!a->is_ip6)
273 hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
274 else
Chris Lukec7949152016-05-31 10:42:14 -0400275 hash_set_mem (vxm->vxlan6_tunnel_by_key, t->key6, t - vxm->tunnels);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276
277 if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0)
278 {
279 vnet_interface_main_t * im = &vnm->interface_main;
280 hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
281 [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1];
282 _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
283
284 hi = vnet_get_hw_interface (vnm, hw_if_index);
285 hi->dev_instance = t - vxm->tunnels;
286 hi->hw_instance = hi->dev_instance;
287
288 /* clear old stats of freed tunnel before reuse */
289 sw_if_index = hi->sw_if_index;
290 vnet_interface_counter_lock(im);
291 vlib_zero_combined_counter
292 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
293 vlib_zero_combined_counter
294 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
295 vlib_zero_simple_counter
296 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
297 vnet_interface_counter_unlock(im);
298 }
299 else
300 {
301 hw_if_index = vnet_register_interface
302 (vnm, vxlan_device_class.index, t - vxm->tunnels,
303 vxlan_hw_class.index, t - vxm->tunnels);
304 hi = vnet_get_hw_interface (vnm, hw_if_index);
305 hi->output_node_index = vxlan_encap_node.index;
306 }
307
308 t->hw_if_index = hw_if_index;
309 t->sw_if_index = sw_if_index = hi->sw_if_index;
310
Dave Wallace60231f32015-12-17 21:04:30 -0500311 vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
312 vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
313
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 if (a->decap_next_index == VXLAN_INPUT_NEXT_L2_INPUT)
315 {
316 l2input_main_t * l2im = &l2input_main;
317 /* setup l2 input config with l2 feature and bd 0 to drop packet */
318 vec_validate (l2im->configs, sw_if_index);
319 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
320 l2im->configs[sw_if_index].bd_index = 0;
321 }
John Lo3ef822e2016-06-07 09:14:07 -0400322
323 /*
324 * Directs the l2 output path to work out the interface
325 * output next-arc itself. Needed when recycling a tunnel.
326 */
327 vec_validate_init_empty(l2om->next_nodes.output_node_index_vec,
328 sw_if_index, ~0);
329 l2om->next_nodes.output_node_index_vec[t->sw_if_index]
330 = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331 vnet_sw_interface_set_flags (vnm, sw_if_index,
332 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
Chris Luke99cb3352016-04-26 10:49:53 -0400333 if (!a->is_ip6) {
John Lo2d343742016-01-19 17:27:17 -0500334 vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
335 im4->fib_index_by_sw_if_index[sw_if_index] = t->encap_fib_index;
Chris Luke99cb3352016-04-26 10:49:53 -0400336 } else {
337 vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
338 im6->fib_index_by_sw_if_index[sw_if_index] = t->encap_fib_index;
339 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 }
341 else
342 {
343 /* deleting a tunnel: tunnel must exist */
344 if (!p)
345 return VNET_API_ERROR_NO_SUCH_ENTRY;
346
347 t = pool_elt_at_index (vxm->tunnels, p[0]);
348
349 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
350 /* make sure tunnel is removed from l2 bd or xconnect */
351 set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
352 vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
353
Dave Wallace60231f32015-12-17 21:04:30 -0500354 vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
355
John Lo3ef822e2016-06-07 09:14:07 -0400356 /* Directs the l2 path to turf packets sent to this sw_if_index */
357 l2om->next_nodes.output_node_index_vec[t->sw_if_index]
358 = L2OUTPUT_NEXT_DEL_TUNNEL;
359
Chris Luke99cb3352016-04-26 10:49:53 -0400360 if (!a->is_ip6)
361 hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64);
362 else
Chris Lukec7949152016-05-31 10:42:14 -0400363 hash_unset_mem (vxm->vxlan6_tunnel_by_key, t->key6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364
365 vec_free (t->rewrite);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366 pool_put (vxm->tunnels, t);
367 }
368
369 if (sw_if_indexp)
370 *sw_if_indexp = sw_if_index;
371
372 return 0;
373}
374
Chris Luke99cb3352016-04-26 10:49:53 -0400375static u32 fib4_index_from_fib_id (u32 fib_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376{
377 ip4_main_t * im = &ip4_main;
378 uword * p;
379
380 p = hash_get (im->fib_index_by_table_id, fib_id);
381 if (!p)
382 return ~0;
383
384 return p[0];
385}
386
Chris Luke99cb3352016-04-26 10:49:53 -0400387static u32 fib6_index_from_fib_id (u32 fib_id)
388{
389 ip6_main_t * im = &ip6_main;
390 uword * p;
391
392 p = hash_get (im->fib_index_by_table_id, fib_id);
393 if (!p)
394 return ~0;
395
396 return p[0];
397}
398
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399static uword unformat_decap_next (unformat_input_t * input, va_list * args)
400{
401 u32 * result = va_arg (*args, u32 *);
402 u32 tmp;
403
404 if (unformat (input, "l2"))
405 *result = VXLAN_INPUT_NEXT_L2_INPUT;
406 else if (unformat (input, "drop"))
407 *result = VXLAN_INPUT_NEXT_DROP;
408 else if (unformat (input, "ip4"))
409 *result = VXLAN_INPUT_NEXT_IP4_INPUT;
410 else if (unformat (input, "ip6"))
411 *result = VXLAN_INPUT_NEXT_IP6_INPUT;
412 else if (unformat (input, "%d", &tmp))
413 *result = tmp;
414 else
415 return 0;
416 return 1;
417}
418
419static clib_error_t *
420vxlan_add_del_tunnel_command_fn (vlib_main_t * vm,
421 unformat_input_t * input,
422 vlib_cli_command_t * cmd)
423{
424 unformat_input_t _line_input, * line_input = &_line_input;
Chris Luke99cb3352016-04-26 10:49:53 -0400425 ip46_address_t src, dst;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426 u8 is_add = 1;
427 u8 src_set = 0;
428 u8 dst_set = 0;
Chris Luke99cb3352016-04-26 10:49:53 -0400429 u8 ipv4_set = 0;
430 u8 ipv6_set = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 u32 encap_fib_index = 0;
432 u32 decap_next_index = ~0;
433 u32 vni = 0;
434 u32 tmp;
435 int rv;
436 vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100437 u32 sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438
439 /* Get a line of input. */
440 if (! unformat_user (input, unformat_line_input, line_input))
441 return 0;
442
443 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
444 if (unformat (line_input, "del"))
Chris Luke99cb3352016-04-26 10:49:53 -0400445 {
446 is_add = 0;
447 }
448 else if (unformat (line_input, "src %U",
449 unformat_ip4_address, &src.ip4))
450 {
451 src_set = 1;
452 ipv4_set = 1;
453 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454 else if (unformat (line_input, "dst %U",
Chris Luke99cb3352016-04-26 10:49:53 -0400455 unformat_ip4_address, &dst.ip4))
456 {
457 dst_set = 1;
458 ipv4_set = 1;
459 }
460 else if (unformat (line_input, "src %U",
461 unformat_ip6_address, &src.ip6))
462 {
463 src_set = 1;
464 ipv6_set = 1;
465 }
466 else if (unformat (line_input, "dst %U",
467 unformat_ip6_address, &dst.ip6))
468 {
469 dst_set = 1;
470 ipv6_set = 1;
471 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
473 {
Chris Luke99cb3352016-04-26 10:49:53 -0400474 if (ipv6_set)
475 encap_fib_index = fib6_index_from_fib_id (tmp);
476 else
477 encap_fib_index = fib4_index_from_fib_id (tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 if (encap_fib_index == ~0)
479 return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
480 }
481 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
482 &decap_next_index))
483 ;
484 else if (unformat (line_input, "vni %d", &vni))
485 {
486 if (vni >> 24)
487 return clib_error_return (0, "vni %d out of range", vni);
488 }
489 else
490 return clib_error_return (0, "parse error: '%U'",
491 format_unformat_error, line_input);
492 }
493
494 unformat_free (line_input);
495
496 if (src_set == 0)
497 return clib_error_return (0, "tunnel src address not specified");
498
499 if (dst_set == 0)
500 return clib_error_return (0, "tunnel dst address not specified");
501
Chris Luke99cb3352016-04-26 10:49:53 -0400502 if (ipv4_set && ipv6_set)
503 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
504
505 if ((ipv4_set && memcmp(&src.ip4, &dst.ip4, sizeof(src.ip4)) == 0) ||
506 (ipv6_set && memcmp(&src.ip6, &dst.ip6, sizeof(src.ip6)) == 0))
507 return clib_error_return (0, "src and dst addresses are identical");
508
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509 if (vni == 0)
510 return clib_error_return (0, "vni not specified");
511
512 memset (a, 0, sizeof (*a));
513
514 a->is_add = is_add;
Chris Luke99cb3352016-04-26 10:49:53 -0400515 a->is_ip6 = ipv6_set;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516
517#define _(x) a->x = x;
518 foreach_copy_field;
Chris Luke99cb3352016-04-26 10:49:53 -0400519 if (ipv4_set) foreach_copy_ipv4
520 else foreach_copy_ipv6
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521#undef _
522
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100523 rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
525 switch(rv)
526 {
527 case 0:
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100528 if (is_add)
529 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530 break;
531 case VNET_API_ERROR_INVALID_DECAP_NEXT:
532 return clib_error_return (0, "invalid decap-next...");
533
534 case VNET_API_ERROR_TUNNEL_EXIST:
535 return clib_error_return (0, "tunnel already exists...");
536
537 case VNET_API_ERROR_NO_SUCH_ENTRY:
538 return clib_error_return (0, "tunnel does not exist...");
539
540 default:
541 return clib_error_return
542 (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
543 }
544
545 return 0;
546}
547
548VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
549 .path = "create vxlan tunnel",
550 .short_help =
551 "create vxlan tunnel src <local-vtep-addr> dst <remote-vtep-addr> vni <nn>"
552 " [encap-vrf-id <nn>] [decap-next [l2|ip4|ip6] [del]\n",
553 .function = vxlan_add_del_tunnel_command_fn,
554};
555
556static clib_error_t *
557show_vxlan_tunnel_command_fn (vlib_main_t * vm,
558 unformat_input_t * input,
559 vlib_cli_command_t * cmd)
560{
561 vxlan_main_t * vxm = &vxlan_main;
562 vxlan_tunnel_t * t;
563
564 if (pool_elts (vxm->tunnels) == 0)
565 vlib_cli_output (vm, "No vxlan tunnels configured...");
566
567 pool_foreach (t, vxm->tunnels,
568 ({
569 vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
570 }));
571
572 return 0;
573}
574
575VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
576 .path = "show vxlan tunnel",
577 .function = show_vxlan_tunnel_command_fn,
578};
579
Chris Luke99cb3352016-04-26 10:49:53 -0400580
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581clib_error_t *vxlan_init (vlib_main_t *vm)
582{
583 vxlan_main_t * vxm = &vxlan_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584
585 vxm->vnet_main = vnet_get_main();
586 vxm->vlib_main = vm;
587
Chris Luke99cb3352016-04-26 10:49:53 -0400588 /* initialize the ip6 hash */
589 vxm->vxlan6_tunnel_by_key = hash_create_mem(0,
590 sizeof(vxlan6_tunnel_key_t),
591 sizeof(uword));
592
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593 udp_register_dst_port (vm, UDP_DST_PORT_vxlan,
Chris Luke99cb3352016-04-26 10:49:53 -0400594 vxlan4_input_node.index, /* is_ip4 */ 1);
595 udp_register_dst_port (vm, UDP_DST_PORT_vxlan6,
596 vxlan6_input_node.index, /* is_ip4 */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597 return 0;
598}
599
600VLIB_INIT_FUNCTION(vxlan_init);