FD.io VPP  v17.01.1-3-gc6833f8
Vector Packet Processing
vxlan.c
Go to the documentation of this file.
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 #include <vnet/ip/format.h>
17 #include <vnet/fib/fib_entry.h>
18 #include <vnet/fib/fib_table.h>
19 #include <vnet/dpo/receive_dpo.h>
20 #include <vlib/vlib.h>
21 
22 /**
23  * @file
24  * @brief VXLAN.
25  *
26  * VXLAN provides the features needed to allow L2 bridge domains (BDs)
27  * to span multiple servers. This is done by building an L2 overlay on
28  * top of an L3 network underlay using VXLAN tunnels.
29  *
30  * This makes it possible for servers to be co-located in the same data
31  * center or be separated geographically as long as they are reachable
32  * through the underlay L3 network.
33  *
34  * You can refer to this kind of L2 overlay bridge domain as a VXLAN
35  * (Virtual eXtensible VLAN) segment.
36  */
37 
38 
40 
41 static u8 * format_decap_next (u8 * s, va_list * args)
42 {
43  u32 next_index = va_arg (*args, u32);
44 
45  switch (next_index)
46  {
47  case VXLAN_INPUT_NEXT_DROP:
48  return format (s, "drop");
49  case VXLAN_INPUT_NEXT_L2_INPUT:
50  return format (s, "l2");
51  default:
52  return format (s, "index %d", next_index);
53  }
54  return s;
55 }
56 
57 u8 * format_vxlan_tunnel (u8 * s, va_list * args)
58 {
59  vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *);
60  vxlan_main_t * ngm = &vxlan_main;
61 
62  s = format (s, "[%d] src %U dst %U vni %d sw_if_index %d ",
63  t - ngm->tunnels,
66  t->vni, t->sw_if_index);
67 
69  s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index);
70 
71  s = format (s, "encap_fib_index %d fib_entry_index %d decap_next %U\n",
74  return s;
75 }
76 
77 static u8 * format_vxlan_name (u8 * s, va_list * args)
78 {
79  u32 dev_instance = va_arg (*args, u32);
80  return format (s, "vxlan_tunnel%d", dev_instance);
81 }
82 
84  vlib_node_runtime_t * node,
85  vlib_frame_t * frame)
86 {
87  clib_warning ("you shouldn't be here, leaking buffers...");
88  return frame->n_vectors;
89 }
90 
91 static clib_error_t *
93 {
96  else
97  vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
98 
99  return /* no error */ 0;
100 }
101 
102 VNET_DEVICE_CLASS (vxlan_device_class,static) = {
103  .name = "VXLAN",
104  .format_device_name = format_vxlan_name,
105  .format_tx_trace = format_vxlan_encap_trace,
106  .tx_function = dummy_interface_tx,
107  .admin_up_down_function = vxlan_interface_admin_up_down,
108 };
109 
110 static u8 * format_vxlan_header_with_length (u8 * s, va_list * args)
111 {
112  u32 dev_instance = va_arg (*args, u32);
113  s = format (s, "unimplemented dev %u", dev_instance);
114  return s;
115 }
116 
117 VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = {
118  .name = "VXLAN",
119  .format_header = format_vxlan_header_with_length,
120  .build_rewrite = default_build_rewrite,
121 };
122 
123 static void
125 {
126  dpo_id_t dpo = DPO_INVALID;
127  u32 encap_index = ip46_address_is_ip4(&t->dst) ?
128  vxlan4_encap_node.index : vxlan6_encap_node.index;
131 
132  fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
133  dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
134  dpo_reset(&dpo);
135 }
136 
137 static vxlan_tunnel_t *
139 {
140 #if (CLIB_DEBUG > 0)
142 #endif
143  return ((vxlan_tunnel_t*) (((char*)node) -
145 }
146 
147 /**
148  * Function definition to backwalk a FIB node -
149  * Here we will restack the new dpo of VXLAN DIP to encap node.
150  */
154 {
157 }
158 
159 /**
160  * Function definition to get a FIB node from its index
161  */
162 static fib_node_t*
164 {
165  vxlan_tunnel_t * t;
166  vxlan_main_t * vxm = &vxlan_main;
167 
168  t = pool_elt_at_index(vxm->tunnels, index);
169 
170  return (&t->node);
171 }
172 
173 /**
174  * Function definition to inform the FIB node that its last lock has gone.
175  */
176 static void
178 {
179  /*
180  * The VXLAN tunnel is a root of the graph. As such
181  * it never has children and thus is never locked.
182  */
183  ASSERT(0);
184 }
185 
186 /*
187  * Virtual function table registered by VXLAN tunnels
188  * for participation in the FIB object graph.
189  */
190 const static fib_node_vft_t vxlan_vft = {
192  .fnv_last_lock = vxlan_tunnel_last_lock_gone,
193  .fnv_back_walk = vxlan_tunnel_back_walk,
194 };
195 
196 
197 #define foreach_copy_field \
198 _(vni) \
199 _(mcast_sw_if_index) \
200 _(encap_fib_index) \
201 _(decap_next_index) \
202 _(src) \
203 _(dst)
204 
206 {
207  u8 *rw = 0;
208  ip4_header_t * ip0;
209  ip4_vxlan_header_t * h0;
210  int len = sizeof (*h0);
211 
213 
214  h0 = (ip4_vxlan_header_t *) rw;
215 
216  /* Fixed portion of the (outer) ip4 header */
217  ip0 = &h0->ip4;
218  ip0->ip_version_and_header_length = 0x45;
219  ip0->ttl = 254;
220  ip0->protocol = IP_PROTOCOL_UDP;
221 
222  /* we fix up the ip4 header length and checksum after-the-fact */
223  ip0->src_address.as_u32 = t->src.ip4.as_u32;
224  ip0->dst_address.as_u32 = t->dst.ip4.as_u32;
225  ip0->checksum = ip4_header_checksum (ip0);
226 
227  /* UDP header, randomize src port on something, maybe? */
228  h0->udp.src_port = clib_host_to_net_u16 (4789);
229  h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
230 
231  /* VXLAN header */
232  vnet_set_vni_and_flags(&h0->vxlan, t->vni);
233 
234  t->rewrite = rw;
235  return (0);
236 }
237 
239 {
240  u8 *rw = 0;
241  ip6_header_t * ip0;
242  ip6_vxlan_header_t * h0;
243  int len = sizeof (*h0);
244 
246 
247  h0 = (ip6_vxlan_header_t *) rw;
248 
249  /* Fixed portion of the (outer) ip6 header */
250  ip0 = &h0->ip6;
251  ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
252  ip0->hop_limit = 255;
253  ip0->protocol = IP_PROTOCOL_UDP;
254 
255  ip0->src_address.as_u64[0] = t->src.ip6.as_u64[0];
256  ip0->src_address.as_u64[1] = t->src.ip6.as_u64[1];
257  ip0->dst_address.as_u64[0] = t->dst.ip6.as_u64[0];
258  ip0->dst_address.as_u64[1] = t->dst.ip6.as_u64[1];
259 
260  /* UDP header, randomize src port on something, maybe? */
261  h0->udp.src_port = clib_host_to_net_u16 (4789);
262  h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
263 
264  /* VXLAN header */
265  vnet_set_vni_and_flags(&h0->vxlan, t->vni);
266 
267  t->rewrite = rw;
268  return (0);
269 }
270 
271 static int vxlan_check_decap_next(vxlan_main_t * vxm, u32 is_ip6, u32 decap_next_index)
272 {
273  vlib_main_t * vm = vxm->vlib_main;
275 
276  if(!is_ip6)
277  {
279  if(decap_next_index >= r->n_next_nodes)
280  return 1;
281  }
282  else
283  {
285  if(decap_next_index >= r->n_next_nodes)
286  return 1;
287  }
288 
289  return 0;
290 }
291 
292 static uword
293 vtep_addr_ref(ip46_address_t *ip)
294 {
295  if (!ip46_address_is_ip4(ip))
296  return 1; /* always create */
297  uword *pvtep = hash_get (vxlan_main.vtep4, ip->ip4.as_u32);
298  if (pvtep)
299  return ++(*pvtep);
300  hash_set (vxlan_main.vtep4, ip->ip4.as_u32, 1);
301  return 1;
302 }
303 
304 static uword
305 vtep_addr_unref(ip46_address_t *ip)
306 {
307  if (!ip46_address_is_ip4(ip))
308  return 0; /* alwways destroy */
309  uword *pvtep = hash_get (vxlan_main.vtep4, ip->ip4.as_u32);
310  ASSERT(pvtep);
311  if (--(*pvtep) == 0)
312  {
313  hash_unset (vxlan_main.vtep4, ip->ip4.as_u32);
314  return 0;
315  }
316  return *pvtep;
317 }
318 
319 static
321 mcast_ep_get(ip46_address_t * ip)
322 {
324  uword * ep_idx = hash_get_mem (vxlan_main.mcast_ep_by_ip, ip);
325  ASSERT(ep_idx);
326  return pool_elt_at_index(vxlan_main.mcast_eps, *ep_idx);
327 }
328 
329 static void
331 {
332  mcast_remote_t * ep;
333 
335  *ep = *new_ep;
336  hash_set_mem (vxlan_main.mcast_ep_by_ip, ep->ip, ep - vxlan_main.mcast_eps);
337 }
338 
339 static void
341 {
342  hash_unset_mem (vxlan_main.mcast_ep_by_ip, ep->ip);
343  pool_put (vxlan_main.mcast_eps, ep);
344 }
345 
346 static void
347 ip46_multicast_ethernet_address(u8 * ethernet_address, ip46_address_t * ip) {
348  if (ip46_address_is_ip4(ip))
349  ip4_multicast_ethernet_address(ethernet_address, &ip->ip4);
350  else
351  ip6_multicast_ethernet_address(ethernet_address, ip->ip6.as_u32[0]);
352 }
353 
356 {
357  vxlan_main_t * vxm = &vxlan_main;
358  vxlan_tunnel_t *t = 0;
359  vnet_main_t * vnm = vxm->vnet_main;
360  uword * p;
361  u32 hw_if_index = ~0;
362  u32 sw_if_index = ~0;
363  int rv;
364  vxlan4_tunnel_key_t key4;
365  vxlan6_tunnel_key_t key6;
366  u32 is_ip6 = a->is_ip6;
367 
368  if (!is_ip6)
369  {
370  key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
371  key4.vni = clib_host_to_net_u32 (a->vni << 8);
372  p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64);
373  }
374  else
375  {
376  key6.src.as_u64[0] = a->dst.ip6.as_u64[0];
377  key6.src.as_u64[1] = a->dst.ip6.as_u64[1];
378  key6.vni = clib_host_to_net_u32 (a->vni << 8);
379  p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6);
380  }
381 
382  if (a->is_add)
383  {
384  l2input_main_t * l2im = &l2input_main;
385 
386  /* adding a tunnel: tunnel must not already exist */
387  if (p)
388  return VNET_API_ERROR_TUNNEL_EXIST;
389 
390  /*if not set explicitly, default to l2 */
391  if(a->decap_next_index == ~0)
392  a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
393  if (vxlan_check_decap_next(vxm, is_ip6, a->decap_next_index))
394  return VNET_API_ERROR_INVALID_DECAP_NEXT;
395 
397  memset (t, 0, sizeof (*t));
398 
399  /* copy from arg structure */
400 #define _(x) t->x = a->x;
402 #undef _
403 
404  if (!is_ip6)
405  rv = vxlan4_rewrite (t);
406  else
407  rv = vxlan6_rewrite (t);
408 
409  if (rv)
410  {
411  pool_put (vxm->tunnels, t);
412  return rv;
413  }
414 
415  /* copy the key */
416  if (is_ip6)
417  {
418  t->key6 = clib_mem_alloc (sizeof(vxlan6_tunnel_key_t));
419  clib_memcpy (t->key6, &key6, sizeof(key6));
420  hash_set_mem (vxm->vxlan6_tunnel_by_key, t->key6, t - vxm->tunnels);
421  }
422  else
423  {
424  t->key4 = 0; /* not yet used */
425  hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
426  }
427 
430  {
432  hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
434  _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
435 
436  hi = vnet_get_hw_interface (vnm, hw_if_index);
437  hi->dev_instance = t - vxm->tunnels;
438  hi->hw_instance = hi->dev_instance;
439 
440  /* clear old stats of freed tunnel before reuse */
441  sw_if_index = hi->sw_if_index;
448  (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
450  }
451  else
452  {
453  hw_if_index = vnet_register_interface
454  (vnm, vxlan_device_class.index, t - vxm->tunnels,
455  vxlan_hw_class.index, t - vxm->tunnels);
456  hi = vnet_get_hw_interface (vnm, hw_if_index);
457  }
458 
459  t->hw_if_index = hw_if_index;
460  t->sw_if_index = sw_if_index = hi->sw_if_index;
461 
463  vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
464 
465  /* setup l2 input config with l2 feature and bd 0 to drop packet */
466  vec_validate (l2im->configs, sw_if_index);
467  l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
468  l2im->configs[sw_if_index].bd_index = 0;
469 
470  vnet_sw_interface_set_flags (vnm, sw_if_index,
473  fib_prefix_t tun_dst_pfx;
474  u32 encap_index = !is_ip6 ?
475  vxlan4_encap_node.index : vxlan6_encap_node.index;
477 
478  fib_prefix_from_ip46_addr(&t->dst, &tun_dst_pfx);
479  if (!ip46_address_is_multicast(&t->dst))
480  {
481  /* Unicast tunnel -
482  * source the FIB entry for the tunnel's destination
483  * and become a child thereof. The tunnel will then get poked
484  * when the forwarding for the entry updates, and the tunnel can
485  * re-stack accordingly
486  */
487  vtep_addr_ref(&t->src);
489  (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_RR,
494  }
495  else
496  {
497  /* Multicast tunnel -
498  * as the same mcast group can be used for mutiple mcast tunnels
499  * with different VNIs, create the output fib adjecency only if
500  * it does not already exist
501  */
503  dpo_id_t dpo = DPO_INVALID;
504  dpo_proto_t dproto = fib_proto_to_dpo(fp);
505 
506  if (vtep_addr_ref(&t->dst) == 1)
507  {
508  u8 mcast_mac[6];
509 
510  ip46_multicast_ethernet_address(mcast_mac, &t->dst);
511  receive_dpo_add_or_lock(dproto, ~0, NULL, &dpo);
512  ip46_address_t * dst_copy = clib_mem_alloc (sizeof(ip46_address_t));
513  *dst_copy = t->dst;
514  mcast_remote_t new_ep = {
515  .ip = dst_copy,
516  .mcast_adj_index = adj_rewrite_add_and_lock
517  (fp, fib_proto_to_link(fp), a->mcast_sw_if_index, mcast_mac),
518  /* Add VRF local mcast adj. */
519  .fib_entry_index = fib_table_entry_special_dpo_add
520  (t->encap_fib_index, &tun_dst_pfx,
522  };
523  mcast_ep_add(&new_ep);
524  dpo_reset(&dpo);
525  }
526  /* Stack shared mcast dst mac addr rewrite on encap */
527  dpo_set (&dpo, DPO_ADJACENCY, dproto,
529  dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
530  dpo_reset (&dpo);
531  flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
532  }
533 
534  /* Set vxlan tunnel output node */
535  hi->output_node_index = encap_index;
536 
537  vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
538  }
539  else
540  {
541  /* deleting a tunnel: tunnel must exist */
542  if (!p)
543  return VNET_API_ERROR_NO_SUCH_ENTRY;
544 
545  t = pool_elt_at_index (vxm->tunnels, p[0]);
546 
547  vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
548  /* make sure tunnel is removed from l2 bd or xconnect */
549  set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
551 
553 
554  if (!is_ip6)
555  hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64);
556  else
557  {
559  clib_mem_free (t->key6);
560  }
561 
562  if (!ip46_address_is_multicast(&t->dst))
563  {
564  vtep_addr_unref(&t->src);
567  }
568  else if (vtep_addr_unref(&t->dst) == 0)
569  {
570  mcast_remote_t * ep = mcast_ep_get(&t->dst);
571  ip46_address_t * ip = ep->ip;
574  mcast_ep_remove(ep);
575  clib_mem_free(ip);
576  }
577 
578  fib_node_deinit(&t->node);
579  vec_free (t->rewrite);
580  pool_put (vxm->tunnels, t);
581  }
582 
583  if (sw_if_indexp)
584  *sw_if_indexp = sw_if_index;
585 
586  return 0;
587 }
588 
590 {
591  ip4_main_t * im = &ip4_main;
592  uword * p;
593 
594  p = hash_get (im->fib_index_by_table_id, fib_id);
595  if (!p)
596  return ~0;
597 
598  return p[0];
599 }
600 
602 {
603  ip6_main_t * im = &ip6_main;
604  uword * p;
605 
606  p = hash_get (im->fib_index_by_table_id, fib_id);
607  if (!p)
608  return ~0;
609 
610  return p[0];
611 }
612 
613 static uword get_decap_next_for_node(u32 node_index, u32 ipv4_set)
614 {
615  vxlan_main_t * vxm = &vxlan_main;
616  vlib_main_t * vm = vxm->vlib_main;
617  uword next_index = ~0;
618 
619  if (ipv4_set)
620  {
621  next_index = vlib_node_add_next (vm, vxlan4_input_node.index, node_index);
622  }
623  else
624  {
625  next_index = vlib_node_add_next (vm, vxlan6_input_node.index, node_index);
626  }
627 
628  return next_index;
629 }
630 
631 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
632 {
633  u32 * result = va_arg (*args, u32 *);
634  u32 ipv4_set = va_arg (*args, int);
635  vxlan_main_t * vxm = &vxlan_main;
636  vlib_main_t * vm = vxm->vlib_main;
637  u32 node_index;
638  u32 tmp;
639 
640  if (unformat (input, "l2"))
641  *result = VXLAN_INPUT_NEXT_L2_INPUT;
642  else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index))
643  *result = get_decap_next_for_node(node_index, ipv4_set);
644  else if (unformat (input, "%d", &tmp))
645  *result = tmp;
646  else
647  return 0;
648  return 1;
649 }
650 
651 static clib_error_t *
653  unformat_input_t * input,
654  vlib_cli_command_t * cmd)
655 {
656  unformat_input_t _line_input, * line_input = &_line_input;
657  ip46_address_t src , dst;
658  u8 is_add = 1;
659  u8 src_set = 0;
660  u8 dst_set = 0;
661  u8 grp_set = 0;
662  u8 ipv4_set = 0;
663  u8 ipv6_set = 0;
664  u32 encap_fib_index = 0;
665  u32 mcast_sw_if_index = ~0;
666  u32 decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
667  u32 vni = 0;
668  u32 tmp;
669  int rv;
670  vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
671  u32 tunnel_sw_if_index;
672 
673  /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
674  memset(&src, 0, sizeof src);
675  memset(&dst, 0, sizeof dst);
676 
677  /* Get a line of input. */
678  if (! unformat_user (input, unformat_line_input, line_input))
679  return 0;
680 
681  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
682  if (unformat (line_input, "del"))
683  {
684  is_add = 0;
685  }
686  else if (unformat (line_input, "src %U",
687  unformat_ip4_address, &src.ip4))
688  {
689  src_set = 1;
690  ipv4_set = 1;
691  }
692  else if (unformat (line_input, "dst %U",
693  unformat_ip4_address, &dst.ip4))
694  {
695  dst_set = 1;
696  ipv4_set = 1;
697  }
698  else if (unformat (line_input, "src %U",
699  unformat_ip6_address, &src.ip6))
700  {
701  src_set = 1;
702  ipv6_set = 1;
703  }
704  else if (unformat (line_input, "dst %U",
705  unformat_ip6_address, &dst.ip6))
706  {
707  dst_set = 1;
708  ipv6_set = 1;
709  }
710  else if (unformat (line_input, "group %U %U",
711  unformat_ip4_address, &dst.ip4,
713  vnet_get_main(), &mcast_sw_if_index))
714  {
715  grp_set = dst_set = 1;
716  ipv4_set = 1;
717  }
718  else if (unformat (line_input, "group %U %U",
719  unformat_ip6_address, &dst.ip6,
721  vnet_get_main(), &mcast_sw_if_index))
722  {
723  grp_set = dst_set = 1;
724  ipv6_set = 1;
725  }
726  else if (unformat (line_input, "encap-vrf-id %d", &tmp))
727  {
728  if (ipv6_set)
729  encap_fib_index = fib6_index_from_fib_id (tmp);
730  else
731  encap_fib_index = fib4_index_from_fib_id (tmp);
732  if (encap_fib_index == ~0)
733  return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
734  }
735  else if (unformat (line_input, "decap-next %U", unformat_decap_next,
736  &decap_next_index, ipv4_set))
737  ;
738  else if (unformat (line_input, "vni %d", &vni))
739  {
740  if (vni >> 24)
741  return clib_error_return (0, "vni %d out of range", vni);
742  }
743  else
744  return clib_error_return (0, "parse error: '%U'",
745  format_unformat_error, line_input);
746  }
747 
748  unformat_free (line_input);
749 
750  if (src_set == 0)
751  return clib_error_return (0, "tunnel src address not specified");
752 
753  if (dst_set == 0)
754  return clib_error_return (0, "tunnel dst address not specified");
755 
756  if (grp_set && !ip46_address_is_multicast(&dst))
757  return clib_error_return (0, "tunnel group address not multicast");
758 
759  if (grp_set == 0 && ip46_address_is_multicast(&dst))
760  return clib_error_return (0, "dst address must be unicast");
761 
762  if (grp_set && mcast_sw_if_index == ~0)
763  return clib_error_return (0, "tunnel nonexistent multicast device");
764 
765  if (ipv4_set && ipv6_set)
766  return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
767 
768  if (ip46_address_cmp(&src, &dst) == 0)
769  return clib_error_return (0, "src and dst addresses are identical");
770 
771  if (decap_next_index == ~0)
772  return clib_error_return (0, "next node not found");
773 
774  if (vni == 0)
775  return clib_error_return (0, "vni not specified");
776 
777  memset (a, 0, sizeof (*a));
778 
779  a->is_add = is_add;
780  a->is_ip6 = ipv6_set;
781 
782 #define _(x) a->x = x;
784 #undef _
785 
786  rv = vnet_vxlan_add_del_tunnel (a, &tunnel_sw_if_index);
787 
788  switch(rv)
789  {
790  case 0:
791  if (is_add)
793  vnet_get_main(), tunnel_sw_if_index);
794  break;
795 
796  case VNET_API_ERROR_TUNNEL_EXIST:
797  return clib_error_return (0, "tunnel already exists...");
798 
799  case VNET_API_ERROR_NO_SUCH_ENTRY:
800  return clib_error_return (0, "tunnel does not exist...");
801 
802  default:
803  return clib_error_return
804  (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
805  }
806 
807  return 0;
808 }
809 
810 /*?
811  * Add or delete a VXLAN Tunnel.
812  *
813  * VXLAN provides the features needed to allow L2 bridge domains (BDs)
814  * to span multiple servers. This is done by building an L2 overlay on
815  * top of an L3 network underlay using VXLAN tunnels.
816  *
817  * This makes it possible for servers to be co-located in the same data
818  * center or be separated geographically as long as they are reachable
819  * through the underlay L3 network.
820  *
821  * You can refer to this kind of L2 overlay bridge domain as a VXLAN
822  * (Virtual eXtensible VLAN) segment.
823  *
824  * @cliexpar
825  * Example of how to create a VXLAN Tunnel:
826  * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 encap-vrf-id 7}
827  * Example of how to delete a VXLAN Tunnel:
828  * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 del}
829  ?*/
830 /* *INDENT-OFF* */
831 VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
832  .path = "create vxlan tunnel",
833  .short_help =
834  "create vxlan tunnel src <local-vtep-addr>"
835  " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>"
836  " [encap-vrf-id <nn>] [decap-next [l2|node <name>]] [del]",
838 };
839 /* *INDENT-ON* */
840 
841 static clib_error_t *
843  unformat_input_t * input,
844  vlib_cli_command_t * cmd)
845 {
846  vxlan_main_t * vxm = &vxlan_main;
847  vxlan_tunnel_t * t;
848 
849  if (pool_elts (vxm->tunnels) == 0)
850  vlib_cli_output (vm, "No vxlan tunnels configured...");
851 
852  pool_foreach (t, vxm->tunnels,
853  ({
854  vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
855  }));
856 
857  return 0;
858 }
859 
860 /*?
861  * Display all the VXLAN Tunnel entries.
862  *
863  * @cliexpar
864  * Example of how to display the VXLAN Tunnel entries:
865  * @cliexstart{show vxlan tunnel}
866  * [0] src 10.0.3.1 dst 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
867  * @cliexend
868  ?*/
869 /* *INDENT-OFF* */
870 VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
871  .path = "show vxlan tunnel",
872  .short_help = "show vxlan tunnel",
873  .function = show_vxlan_tunnel_command_fn,
874 };
875 /* *INDENT-ON* */
876 
877 
879 {
880  vxlan_main_t * vxm = &vxlan_main;
881 
882  vxm->vnet_main = vnet_get_main();
883  vxm->vlib_main = vm;
884 
885  /* initialize the ip6 hash */
887  sizeof(vxlan6_tunnel_key_t),
888  sizeof(uword));
890  sizeof(ip46_address_t),
891  sizeof(uword));
892 
893  udp_register_dst_port (vm, UDP_DST_PORT_vxlan,
894  vxlan4_input_node.index, /* is_ip4 */ 1);
895  udp_register_dst_port (vm, UDP_DST_PORT_vxlan6,
896  vxlan6_input_node.index, /* is_ip4 */ 0);
897 
899 
900  return 0;
901 }
902 
format_function_t format_ip46_address
Definition: format.h:61
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:396
void dpo_stack_from_node(u32 child_node_index, dpo_id_t *dpo, const dpo_id_t *parent)
Stack one DPO object on another, and thus establish a child parent relationship.
Definition: dpo.c:413
vmrglw vmrglh hi
Recursive resolution source.
Definition: fib_entry.h:105
Contribute an object that is to be used to forward IP6 packets.
Definition: fib_types.h:85
#define hash_set(h, key, value)
Definition: hash.h:254
l2_input_config_t * configs
Definition: l2_input.h:66
void receive_dpo_add_or_lock(dpo_proto_t proto, u32 sw_if_index, const ip46_address_t *nh_addr, dpo_id_t *dpo)
Definition: receive_dpo.c:56
mcast_remote_t * mcast_eps
Definition: vxlan.h:160
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
u32 sibling_index
The tunnel is a child of the FIB entry for its desintion.
Definition: vxlan.h:119
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:531
#define hash_unset(h, key)
Definition: hash.h:260
static uword ip46_address_is_multicast(ip46_address_t *a)
Definition: ip6_packet.h:147
ip4_address_t src_address
Definition: ip4_packet.h:163
vnet_interface_main_t interface_main
Definition: vnet.h:57
clib_error_t * vxlan_init(vlib_main_t *vm)
Definition: vxlan.c:878
void fib_node_init(fib_node_t *node, fib_node_type_t type)
Definition: fib_node.c:183
vlib_node_registration_t vxlan4_input_node
(constructor) VLIB_REGISTER_NODE (vxlan4_input_node)
Definition: decap.c:22
u32 fib_entry_child_add(fib_node_index_t fib_entry_index, fib_node_type_t child_type, fib_node_index_t child_index)
Definition: fib_entry.c:472
u64 as_u64[2]
Definition: ip6_packet.h:51
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
adj_index_t adj_rewrite_add_and_lock(fib_protocol_t nh_proto, vnet_link_t link_type, u32 sw_if_index, u8 *rewrite)
adj_rewrite_add_and_lock
Definition: adj_rewrite.c:25
#define NULL
Definition: clib.h:55
enum fib_node_back_walk_rc_t_ fib_node_back_walk_rc_t
Return code from a back walk function.
void fib_entry_contribute_forwarding(fib_node_index_t fib_entry_index, fib_forward_chain_type_t fct, dpo_id_t *dpo)
Definition: fib_entry.c:391
ip46_address_t dst
Definition: vxlan.h:86
static int vxlan_check_decap_next(vxlan_main_t *vxm, u32 is_ip6, u32 decap_next_index)
Definition: vxlan.c:271
static u32 fib4_index_from_fib_id(u32 fib_id)
Definition: vxlan.c:589
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
void fib_entry_child_remove(fib_node_index_t fib_entry_index, u32 sibling_index)
Definition: fib_entry.c:483
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
vxlan6_tunnel_key_t * key6
Definition: vxlan.h:78
Contribute an object that is to be used to forward IP4 packets.
Definition: fib_types.h:81
#define hash_set_mem(h, key, value)
Definition: hash.h:274
#define STRUCT_OFFSET_OF(t, f)
Definition: clib.h:62
void fib_node_deinit(fib_node_t *node)
Definition: fib_node.c:198
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
unformat_function_t unformat_vnet_sw_interface
static u32 fib6_index_from_fib_id(u32 fib_id)
Definition: vxlan.c:601
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:377
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:407
static vxlan_tunnel_t * vxlan_tunnel_from_fib_node(fib_node_t *node)
Definition: vxlan.c:138
ip6_address_t src_address
Definition: ip6_packet.h:337
#define ip46_address_cmp(ip46_1, ip46_2)
Definition: ip6_packet.h:80
format_function_t format_vnet_sw_if_index_name
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1063
static clib_error_t * show_vxlan_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vxlan.c:842
static fib_node_back_walk_rc_t vxlan_tunnel_back_walk(fib_node_t *node, fib_node_back_walk_ctx_t *ctx)
Function definition to backwalk a FIB node - Here we will restack the new dpo of VXLAN DIP to encap n...
Definition: vxlan.c:152
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
void fib_node_register_type(fib_node_type_t type, const fib_node_vft_t *vft)
fib_node_register_type
Definition: fib_node.c:58
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vnet_flood_class_t flood_class
Definition: interface.h:561
vnet_main_t * vnet_main
Definition: vxlan.h:171
VNET_DEVICE_CLASS(vxlan_device_class, static)
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:348
static void mcast_ep_add(mcast_remote_t *new_ep)
Definition: vxlan.c:330
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
ip4_address_t dst_address
Definition: ip4_packet.h:163
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:615
static void ip46_multicast_ethernet_address(u8 *ethernet_address, ip46_address_t *ip)
Definition: vxlan.c:347
Aggregrate type for a prefix.
Definition: fib_types.h:145
static void vxlan_tunnel_last_lock_gone(fib_node_t *node)
Function definition to inform the FIB node that its last lock has gone.
Definition: vxlan.c:177
vlib_main_t * vlib_main
Definition: vxlan.h:170
static void mcast_ep_remove(mcast_remote_t *ep)
Definition: vxlan.c:340
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
#define clib_warning(format, args...)
Definition: error.h:59
void adj_unlock(adj_index_t adj_index)
Release a reference counting lock on the adjacency.
Definition: adj.c:209
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:977
enum dpo_proto_t_ dpo_proto_t
Data path protocol.
static clib_error_t * vxlan_add_del_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vxlan.c:652
fib_node_t node
Linkage into the FIB object graph.
Definition: vxlan.h:104
u32 vnet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u32 hw_class_index, u32 hw_instance)
Definition: interface.c:688
u32 set_int_l2_mode(vlib_main_t *vm, vnet_main_t *vnet_main, u32 mode, u32 sw_if_index, u32 bd_index, u32 bvi, u32 shg, u32 xc_sw_if_index)
Set the subinterface to run in l2 or l3 mode.
Definition: l2_input.c:509
Definition: fib_entry.h:216
unformat_function_t unformat_ip4_address
Definition: format.h:76
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:138
#define hash_create_mem(elts, key_bytes, value_bytes)
Definition: hash.h:637
#define hash_get(h, key)
Definition: hash.h:248
u8 * rewrite
Definition: vxlan.h:70
#define ADJ_INDEX_INVALID
Invalid ADJ index - used when no adj is known likewise blazoned capitals INVALID speak volumes where ...
Definition: adj_types.h:36
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
static void vlib_zero_combined_counter(vlib_combined_counter_main_t *cm, u32 index)
Clear a combined counter Clears the set of per-thread u16 counters, and the shared vlib_counter_t...
Definition: counter.h:321
#define hash_unset_mem(h, key)
Definition: hash.h:280
fib_node_index_t fib_table_entry_special_add(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, adj_index_t adj_index)
Add a &#39;special&#39; entry to the FIB that links to the adj passed A special entry is an entry that the FI...
Definition: fib_table.c:369
uword * fib_index_by_table_id
Hash table mapping table id to fib index.
Definition: ip4.h:112
static clib_error_t * vxlan_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: vxlan.c:92
uword * vxlan4_tunnel_by_key
Definition: vxlan.h:151
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:214
vxlan_main_t vxlan_main
Definition: vxlan.c:39
vlib_simple_counter_main_t * sw_if_counters
Definition: interface.h:614
vlib_node_registration_t vxlan6_input_node
(constructor) VLIB_REGISTER_NODE (vxlan6_input_node)
Definition: decap.c:23
fib_node_type_t fn_type
The node&#39;s type.
Definition: fib_node.h:278
An node in the FIB graph.
Definition: fib_node.h:273
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:576
u8 * format_vxlan_encap_trace(u8 *s, va_list *args)
Definition: encap.c:49
unformat_function_t unformat_ip6_address
Definition: format.h:94
static uword vtep_addr_unref(ip46_address_t *ip)
Definition: vxlan.c:305
uword * fib_index_by_table_id
Definition: ip6.h:151
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:76
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:169
int vnet_vxlan_add_del_tunnel(vnet_vxlan_add_del_tunnel_args_t *a, u32 *sw_if_indexp)
Definition: vxlan.c:355
static void ip4_multicast_ethernet_address(u8 *ethernet_address, ip4_address_t *a)
Definition: ip4_packet.h:323
u16 n_vectors
Definition: node.h:344
void fib_table_entry_delete_index(fib_node_index_t fib_entry_index, fib_source_t source)
Delete a FIB entry.
Definition: fib_table.c:831
static uword dummy_interface_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: vxlan.c:83
u32 mcast_sw_if_index
Definition: vxlan.h:89
static int vxlan4_rewrite(vxlan_tunnel_t *t)
Definition: vxlan.c:205
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
vnet_flood_class_t
Definition: interface.h:509
uword * mcast_ep_by_ip
Definition: vxlan.h:161
static vlib_node_runtime_t * vlib_node_get_runtime(vlib_main_t *vm, u32 node_index)
Get node runtime by node index.
Definition: node_funcs.h:88
fib_node_get_t fnv_get
Definition: fib_node.h:261
#define clib_memcpy(a, b, c)
Definition: string.h:69
static void vnet_interface_counter_unlock(vnet_interface_main_t *im)
Definition: interface.h:640
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:28
static void vnet_set_vni_and_flags(vxlan_header_t *h, u32 vni)
Definition: vxlan_packet.h:62
static uword unformat_decap_next(unformat_input_t *input, va_list *args)
Definition: vxlan.c:631
u32 decap_next_index
Definition: vxlan.h:92
void fib_prefix_from_ip46_addr(const ip46_address_t *addr, fib_prefix_t *pfx)
Host prefix from ip.
Definition: fib_types.c:54
void dpo_set(dpo_id_t *dpo, dpo_type_t type, dpo_proto_t proto, index_t index)
Set/create a DPO ID The DPO will be locked.
Definition: dpo.c:154
u8 * default_build_rewrite(vnet_main_t *vnm, u32 sw_if_index, vnet_link_t link_type, const void *dst_address)
Return a complete, zero-length (aka dummy) rewrite.
Definition: interface.c:1382
Context passed between object during a back walk.
Definition: fib_node.h:186
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
static u8 * format_vxlan_header_with_length(u8 *s, va_list *args)
Definition: vxlan.c:110
fib_node_index_t fib_table_entry_special_dpo_add(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, const dpo_id_t *dpo)
Add a &#39;special&#39; entry to the FIB that links to the DPO passed A special entry is an entry that the FI...
Definition: fib_table.c:288
static void vnet_interface_counter_lock(vnet_interface_main_t *im)
Definition: interface.h:632
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:528
static void vxlan_tunnel_restack_dpo(vxlan_tunnel_t *t)
Definition: vxlan.c:124
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
ip6_main_t ip6_main
Definition: ip6_forward.c:2828
static fib_node_t * vxlan_tunnel_fib_node_get(fib_node_index_t index)
Function definition to get a FIB node from its index.
Definition: vxlan.c:163
IPv4 main type.
Definition: ip4.h:95
static void clib_mem_free(void *p)
Definition: mem.h:176
uword * vtep4
Definition: vxlan.h:156
enum fib_forward_chain_type_t_ fib_forward_chain_type_t
FIB output chain type.
uword * vxlan6_tunnel_by_key
Definition: vxlan.h:152
u32 sw_if_index
Definition: vxlan.h:98
static void vlib_zero_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
Clear a simple counter Clears the set of per-thread u16 counters, and the u64 counter.
Definition: counter.h:143
dpo_proto_t fib_proto_to_dpo(fib_protocol_t fib_proto)
Definition: fib_types.c:220
static uword get_decap_next_for_node(u32 node_index, u32 ipv4_set)
Definition: vxlan.c:613
u32 hw_if_index
Definition: vxlan.h:99
static void * clib_mem_alloc(uword size)
Definition: mem.h:109
static uword vtep_addr_ref(ip46_address_t *ip)
Definition: vxlan.c:293
adj_index_t mcast_adj_index
Definition: vxlan.h:143
u64 uword
Definition: types.h:112
#define foreach_copy_field
Definition: vxlan.c:197
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:324
static int vxlan6_rewrite(vxlan_tunnel_t *t)
Definition: vxlan.c:238
l2input_main_t l2input_main
Definition: l2_input.c:87
static mcast_remote_t * mcast_ep_get(ip46_address_t *ip)
Definition: vxlan.c:321
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
fib_node_index_t fib_entry_index
Definition: vxlan.h:110
u32 encap_fib_index
Definition: vxlan.h:95
#define DPO_INVALID
An initialiser for DPOs declared on the stack.
Definition: dpo.h:165
Special sources.
Definition: fib_entry.h:40
unformat_function_t unformat_vlib_node
Definition: node_funcs.h:1110
u32 * tunnel_index_by_sw_if_index
Definition: vxlan.h:167
#define hash_get_mem(h, key)
Definition: hash.h:268
A FIB graph nodes virtual function table.
Definition: fib_node.h:260
static u8 * format_decap_next(u8 *s, va_list *args)
Definition: vxlan.c:41
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
vlib_node_registration_t vxlan4_encap_node
(constructor) VLIB_REGISTER_NODE (vxlan4_encap_node)
Definition: encap.c:522
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1099
void dpo_reset(dpo_id_t *dpo)
reset a DPO ID The DPO will be unlocked.
Definition: dpo.c:191
vlib_node_registration_t vxlan6_encap_node
(constructor) VLIB_REGISTER_NODE (vxlan6_encap_node)
Definition: encap.c:538
vnet_link_t fib_proto_to_link(fib_protocol_t proto)
Convert from a protocol to a link type.
Definition: fib_types.c:254
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: interface.c:539
dpo_id_t next_dpo
Definition: vxlan.h:73
static void ip6_multicast_ethernet_address(u8 *ethernet_address, u32 group_id)
Definition: ip6_packet.h:193
void udp_register_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u32 node_index, u8 is_ip4)
Definition: udp_local.c:495
vxlan4_tunnel_key_t * key4
Definition: vxlan.h:77
#define clib_error_return(e, args...)
Definition: error.h:111
VNET_HW_INTERFACE_CLASS(vxlan_hw_class)
u8 ip_version_and_header_length
Definition: ip4_packet.h:131
struct _unformat_input_t unformat_input_t
u32 flags
Definition: vhost-user.h:75
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:445
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
unformat_function_t unformat_line_input
Definition: format.h:281
ip46_address_t * ip
Definition: vxlan.h:141
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:238
vxlan_tunnel_t * tunnels
Definition: vxlan.h:148
u32 * free_vxlan_tunnel_hw_if_indices
Definition: vxlan.h:164
ip46_address_t src
Definition: vxlan.h:85
u8 * format_vxlan_tunnel(u8 *s, va_list *args)
Definition: vxlan.c:57
static u8 * format_vxlan_name(u8 *s, va_list *args)
Definition: vxlan.c:77
ip6_address_t dst_address
Definition: ip6_packet.h:337
fib_node_index_t fib_entry_index
Definition: vxlan.h:142
#define MODE_L3
Definition: l2_input.h:178
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:109