FD.io VPP  v16.06
Vector Packet Processing
vxlan_gpe.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  */
16 
18 
19 u8 * format_vxlan_gpe_tunnel (u8 * s, va_list * args)
20 {
21  vxlan_gpe_tunnel_t * t = va_arg (*args, vxlan_gpe_tunnel_t *);
23 
24  s = format (s, "[%d] local: %U remote: %U ",
25  t - gm->tunnels,
28 
29  switch (t->protocol)
30  {
31  case VXLAN_GPE_PROTOCOL_IP4:
32  s = format (s, "next-protocol ip4");
33  break;
34  case VXLAN_GPE_PROTOCOL_IP6:
35  s = format (s, "next-protocol ip6");
36  break;
37  case VXLAN_GPE_PROTOCOL_ETHERNET:
38  s = format (s, "next-protocol ethernet");
39  break;
40  case VXLAN_GPE_PROTOCOL_NSH:
41  s = format (s, "next-protocol nsh");
42  break;
43  default:
44  s = format (s, "next-protocol unknown %d", t->protocol);
45  }
46 
47  s = format (s, " fibs: (encap %d, decap %d)",
48  t->encap_fib_index,
49  t->decap_fib_index);
50 
51  s = format (s, " vxlan VNI %d ", t->vni);
52 
53  return s;
54 }
55 
56 static u8 * format_vxlan_gpe_name (u8 * s, va_list * args)
57 {
59  u32 i = va_arg (*args, u32);
60  u32 show_dev_instance = ~0;
61 
62  if (i < vec_len (gm->dev_inst_by_real))
63  show_dev_instance = gm->dev_inst_by_real[i];
64 
65  if (show_dev_instance != ~0)
66  i = show_dev_instance;
67 
68  return format (s, "vxlan_gpe_tunnel%d", i);
69 }
70 
72  u32 new_dev_instance)
73 {
75 
77 
78  gm->dev_inst_by_real [hi->dev_instance] = new_dev_instance;
79 
80  return 0;
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 
92  u32 sw_if_index,
93  u32 l3_type,
94  void * dst_address,
95  void * rewrite,
96  uword max_rewrite_bytes)
97 {
98  return 0;
99 }
100 
101 VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
102  .name = "VXLAN_GPE",
103  .format_device_name = format_vxlan_gpe_name,
104  .format_tx_trace = format_vxlan_gpe_encap_trace,
105  .tx_function = dummy_interface_tx,
106  .name_renumber = vxlan_gpe_name_renumber,
107 };
108 
109 static u8 * format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
110 {
111  u32 dev_instance = va_arg (*args, u32);
112  s = format (s, "unimplemented dev %u", dev_instance);
113  return s;
114 }
115 
116 VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
117  .name = "VXLAN_GPE",
118  .format_header = format_vxlan_gpe_header_with_length,
119  .set_rewrite = dummy_set_rewrite,
120 };
121 
122 
123 #define foreach_gpe_copy_field \
124 _(local.as_u32) \
125 _(remote.as_u32) \
126 _(vni) \
127 _(protocol) \
128 _(encap_fib_index) \
129 _(decap_fib_index)
130 
131 #define foreach_copy_field \
132 _(src.as_u32) \
133 _(dst.as_u32) \
134 _(vni) \
135 _(encap_fib_index) \
136 _(decap_fib_index) \
137 _(decap_next_index)
138 
139 
140 
142 {
143  u8 *rw = 0;
144  ip4_header_t * ip0;
145  ip4_vxlan_gpe_header_t * h0;
146  int len;
147 
148  len = sizeof (*h0);
149 
151 
152  h0 = (ip4_vxlan_gpe_header_t *) rw;
153 
154  /* Fixed portion of the (outer) ip4 header */
155  ip0 = &h0->ip4;
156  ip0->ip_version_and_header_length = 0x45;
157  ip0->ttl = 254;
158  ip0->protocol = IP_PROTOCOL_UDP;
159 
160  /* we fix up the ip4 header length and checksum after-the-fact */
161  ip0->src_address.as_u32 = t->local.as_u32;
162  ip0->dst_address.as_u32 = t->remote.as_u32;
163  ip0->checksum = ip4_header_checksum (ip0);
164 
165  /* UDP header, randomize src port on something, maybe? */
166  h0->udp.src_port = clib_host_to_net_u16 (4790);
167  h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
168 
169  /* VXLAN header. Are we having fun yet? */
170  h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
171  h0->vxlan.ver_res = VXLAN_GPE_VERSION;
172  h0->vxlan.protocol = VXLAN_GPE_PROTOCOL_IP4;
173  h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
174 
175  t->rewrite = rw;
176  return (0);
177 }
178 
181 {
183  vxlan_gpe_tunnel_t *t = 0;
184  vnet_main_t * vnm = gm->vnet_main;
186  uword * p;
187  u32 hw_if_index = ~0;
188  u32 sw_if_index = ~0;
189  int rv;
190  vxlan_gpe_tunnel_key_t key, *key_copy;
191  hash_pair_t *hp;
192 
193  key.local = a->local.as_u32;
194  key.remote = a->remote.as_u32;
195  key.vni = clib_host_to_net_u32 (a->vni << 8);
196  key.pad = 0;
197 
198  p = hash_get_mem (gm->vxlan_gpe_tunnel_by_key, &key);
199 
200  if (a->is_add)
201  {
202  /* adding a tunnel: tunnel must not already exist */
203  if (p)
204  return VNET_API_ERROR_INVALID_VALUE;
205 
207  return VNET_API_ERROR_INVALID_DECAP_NEXT;
208 
210  memset (t, 0, sizeof (*t));
211 
212  /* copy from arg structure */
213 #define _(x) t->x = a->x;
215 #undef _
216 
217  rv = vxlan_gpe_rewrite (t);
218 
219  if (rv)
220  {
221  pool_put (gm->tunnels, t);
222  return rv;
223  }
224 
225  key_copy = clib_mem_alloc (sizeof (*key_copy));
226  clib_memcpy (key_copy, &key, sizeof (*key_copy));
227 
228  hash_set_mem (gm->vxlan_gpe_tunnel_by_key, key_copy,
229  t - gm->tunnels);
230 
232  {
233  hw_if_index = gm->free_vxlan_gpe_tunnel_hw_if_indices
235  _vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
236 
237  hi = vnet_get_hw_interface (vnm, hw_if_index);
238  hi->dev_instance = t - gm->tunnels;
239  hi->hw_instance = hi->dev_instance;
240  }
241  else
242  {
243  hw_if_index = vnet_register_interface
244  (vnm, vxlan_gpe_device_class.index, t - gm->tunnels,
245  vxlan_gpe_hw_class.index, t - gm->tunnels);
246  hi = vnet_get_hw_interface (vnm, hw_if_index);
248  }
249 
250  t->hw_if_index = hw_if_index;
251  t->sw_if_index = sw_if_index = hi->sw_if_index;
252 
255  }
256  else
257  {
258  /* deleting a tunnel: tunnel must exist */
259  if (!p)
260  return VNET_API_ERROR_NO_SUCH_ENTRY;
261 
262  t = pool_elt_at_index (gm->tunnels, p[0]);
263 
264  vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
266 
267  hp = hash_get_pair (gm->vxlan_gpe_tunnel_by_key, &key);
268  key_copy = (void *)(hp->key);
270  clib_mem_free (key_copy);
271 
272  vec_free (t->rewrite);
273  pool_put (gm->tunnels, t);
274  }
275 
276  if (sw_if_indexp)
277  *sw_if_indexp = sw_if_index;
278 
279  return 0;
280 }
281 
283 {
284  ip4_main_t * im = &ip4_main;
285  uword * p;
286 
287  p = hash_get (im->fib_index_by_table_id, fib_id);
288  if (!p)
289  return ~0;
290 
291  return p[0];
292 }
293 
294 static uword unformat_gpe_decap_next (unformat_input_t * input, va_list * args)
295 {
296  u32 * result = va_arg (*args, u32 *);
297  u32 tmp;
298 
299  if (unformat (input, "drop"))
300  *result = VXLAN_GPE_INPUT_NEXT_DROP;
301  else if (unformat (input, "ip4"))
302  *result = VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
303  else if (unformat (input, "ip6"))
304  *result = VXLAN_GPE_INPUT_NEXT_IP6_INPUT;
305  else if (unformat (input, "ethernet"))
306  *result = VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT;
307  else if (unformat (input, "%d", &tmp))
308  *result = tmp;
309  else
310  return 0;
311  return 1;
312 }
313 
314 static clib_error_t *
316  unformat_input_t * input,
317  vlib_cli_command_t * cmd)
318 {
319  unformat_input_t _line_input, * line_input = &_line_input;
320  u8 is_add = 1;
321  ip4_address_t local, remote;
322  u8 local_set = 0;
323  u8 remote_set = 0;
324  u32 encap_fib_index = 0;
325  u32 decap_fib_index = 0;
326  u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
327  u32 decap_next_index = VXLAN_GPE_INPUT_NEXT_IP4_INPUT;
328  u32 vni;
329  u8 vni_set = 0;
330  int rv;
331  u32 tmp;
333 
334  /* Get a line of input. */
335  if (! unformat_user (input, unformat_line_input, line_input))
336  return 0;
337 
338  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
339  if (unformat (line_input, "del"))
340  is_add = 0;
341  else if (unformat (line_input, "local %U",
342  unformat_ip4_address, &local))
343  local_set = 1;
344  else if (unformat (line_input, "remote %U",
345  unformat_ip4_address, &remote))
346  remote_set = 1;
347  else if (unformat (line_input, "encap-vrf-id %d", &tmp))
348  {
349  encap_fib_index = fib_index_from_fib_id (tmp);
350  if (encap_fib_index == ~0)
351  return clib_error_return (0, "nonexistent encap fib id %d", tmp);
352  }
353  else if (unformat (line_input, "decap-vrf-id %d", &tmp))
354  {
355  decap_fib_index = fib_index_from_fib_id (tmp);
356  if (decap_fib_index == ~0)
357  return clib_error_return (0, "nonexistent decap fib id %d", tmp);
358  }
359  else if (unformat (line_input, "decap-next %U", unformat_gpe_decap_next,
360  &decap_next_index))
361  ;
362  else if (unformat (line_input, "vni %d", &vni))
363  vni_set = 1;
364  else if (unformat(line_input, "next-ip4"))
365  protocol = VXLAN_GPE_PROTOCOL_IP4;
366  else if (unformat(line_input, "next-ip6"))
367  protocol = VXLAN_GPE_PROTOCOL_IP6;
368  else if (unformat(line_input, "next-ethernet"))
369  protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
370  else if (unformat(line_input, "next-nsh"))
371  protocol = VXLAN_GPE_PROTOCOL_NSH;
372  else
373  return clib_error_return (0, "parse error: '%U'",
374  format_unformat_error, line_input);
375  }
376 
377  unformat_free (line_input);
378 
379  if (local_set == 0)
380  return clib_error_return (0, "tunnel local address not specified");
381 
382  if (remote_set == 0)
383  return clib_error_return (0, "tunnel remote address not specified");
384 
385  if (vni_set == 0)
386  return clib_error_return (0, "vni not specified");
387 
388  memset (a, 0, sizeof (*a));
389 
390  a->is_add = is_add;
391 
392 #define _(x) a->x = x;
394 #undef _
395 
396  rv = vnet_vxlan_gpe_add_del_tunnel (a, 0 /* hw_if_indexp */);
397 
398  switch(rv)
399  {
400  case 0:
401  break;
402  case VNET_API_ERROR_INVALID_DECAP_NEXT:
403  return clib_error_return (0, "invalid decap-next...");
404 
405  case VNET_API_ERROR_TUNNEL_EXIST:
406  return clib_error_return (0, "tunnel already exists...");
407 
408  case VNET_API_ERROR_NO_SUCH_ENTRY:
409  return clib_error_return (0, "tunnel does not exist...");
410 
411  default:
412  return clib_error_return
413  (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
414  }
415 
416  return 0;
417 }
418 
419 VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
420  .path = "create vxlan-gpe tunnel",
421  .short_help =
422  "create vxlan-gpe tunnel local <ip4-addr> remote <ip4-addr>"
423  " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
424  " [encap-vrf-id <nn>] [decap-vrf-id <nn>]"
425  " [del]\n",
427 };
428 
429 static clib_error_t *
431  unformat_input_t * input,
432  vlib_cli_command_t * cmd)
433 {
435  vxlan_gpe_tunnel_t * t;
436 
437  if (pool_elts (gm->tunnels) == 0)
438  vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
439 
440  pool_foreach (t, gm->tunnels,
441  ({
442  vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
443  }));
444 
445  return 0;
446 }
447 
448 VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
449  .path = "show vxlan-gpe",
451 };
452 
454 {
456 
457  gm->vnet_main = vnet_get_main();
458  gm->vlib_main = vm;
459 
461  = hash_create_mem (0, sizeof(vxlan_gpe_tunnel_key_t), sizeof (uword));
462 
463  udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe,
464  vxlan_gpe_input_node.index, 1 /* is_ip4 */);
465  return 0;
466 }
467 
469 
vmrglw vmrglh hi
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
clib_error_t * vxlan_gpe_init(vlib_main_t *vm)
Definition: vxlan_gpe.c:453
ip4_address_t src_address
Definition: ip4_packet.h:138
static uword dummy_interface_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: vxlan_gpe.c:83
always_inline void clib_mem_free(void *p)
Definition: mem.h:149
always_inline void unformat_free(unformat_input_t *i)
Definition: format.h:160
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
vlib_node_registration_t vxlan_gpe_input_node
(constructor) VLIB_REGISTER_NODE (vxlan_gpe_input_node)
Definition: decap.c:429
static uword dummy_set_rewrite(vnet_main_t *vnm, u32 sw_if_index, u32 l3_type, void *dst_address, void *rewrite, uword max_rewrite_bytes)
Definition: vxlan_gpe.c:91
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
#define hash_set_mem(h, key, value)
Definition: hash.h:257
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:405
int vnet_vxlan_gpe_add_del_tunnel(vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 *sw_if_indexp)
Definition: vxlan_gpe.c:180
#define VXLAN_GPE_FLAGS_P
format_function_t format_ip4_address
Definition: format.h:71
vnet_main_t * vnet_main
Definition: vxlan_gpe.h:110
#define VXLAN_GPE_VERSION
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
#define VXLAN_GPE_FLAGS_I
#define foreach_gpe_copy_field
Definition: vxlan_gpe.c:123
ip4_address_t local
Definition: vxlan_gpe.h:59
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
static int vxlan_gpe_rewrite(vxlan_gpe_tunnel_t *t)
Definition: vxlan_gpe.c:141
u8 * format_vxlan_gpe_tunnel(u8 *s, va_list *args)
Definition: vxlan_gpe.c:19
#define pool_foreach(VAR, POOL, BODY)
Definition: pool.h:328
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
static u8 * format_vxlan_gpe_header_with_length(u8 *s, va_list *args)
Definition: vxlan_gpe.c:109
ip4_address_t dst_address
Definition: ip4_packet.h:138
u8 * format_vxlan_gpe_encap_trace(u8 *s, va_list *args)
Definition: encap.c:50
ip4_address_t remote
Definition: vxlan_gpe.h:60
always_inline uword pool_elts(void *v)
Definition: pool.h:97
#define clib_warning(format, args...)
Definition: error.h:59
#define hash_get_pair(h, key)
Definition: hash.h:234
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
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:583
unformat_function_t unformat_ip4_address
Definition: format.h:68
#define hash_create_mem(elts, key_bytes, value_bytes)
Definition: hash.h:594
#define hash_get(h, key)
Definition: hash.h:231
#define pool_elt_at_index(p, i)
Definition: pool.h:346
#define hash_unset_mem(h, key)
Definition: hash.h:263
uword * fib_index_by_table_id
Definition: ip4.h:141
static int vxlan_gpe_name_renumber(vnet_hw_interface_t *hi, u32 new_dev_instance)
Definition: vxlan_gpe.c:71
#define pool_put(P, E)
Definition: pool.h:200
vxlan_gpe_main_t vxlan_gpe_main
Definition: vxlan_gpe.c:17
vlib_main_t * vlib_main
Definition: vxlan_gpe.h:109
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:538
always_inline u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:194
#define pool_get_aligned(P, E, A)
Definition: pool.h:155
always_inline void * clib_mem_alloc(uword size)
Definition: mem.h:109
u16 n_vectors
Definition: node.h:307
VNET_DEVICE_CLASS(vxlan_gpe_device_class, static)
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
#define clib_memcpy(a, b, c)
Definition: string.h:63
static u8 * format_vxlan_gpe_name(u8 *s, va_list *args)
Definition: vxlan_gpe.c:56
always_inline vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
static clib_error_t * vxlan_gpe_add_del_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vxlan_gpe.c:315
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:373
unsigned int u32
Definition: types.h:88
VNET_HW_INTERFACE_CLASS(vxlan_gpe_hw_class)
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:87
static u32 fib_index_from_fib_id(u32 fib_id)
Definition: vxlan_gpe.c:282
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
vxlan_gpe_tunnel_t * tunnels
Definition: vxlan_gpe.h:97
u64 uword
Definition: types.h:112
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
static clib_error_t * show_vxlan_gpe_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vxlan_gpe.c:430
uword * vxlan_gpe_tunnel_by_key
Definition: vxlan_gpe.h:100
#define hash_get_mem(h, key)
Definition: hash.h:251
ip4_main_t ip4_main
Definition: ip4_forward.c:1394
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: interface.c:462
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:374
#define clib_error_return(e, args...)
Definition: error.h:112
u8 ip_version_and_header_length
Definition: ip4_packet.h:108
struct _unformat_input_t unformat_input_t
vlib_node_registration_t vxlan_gpe_encap_node
(constructor) VLIB_REGISTER_NODE (vxlan_gpe_encap_node)
Definition: encap.c:408
u32 * dev_inst_by_real
Definition: vxlan_gpe.h:106
#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:443
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
unformat_function_t unformat_line_input
Definition: format.h:279
uword key
Definition: hash.h:148
u32 * free_vxlan_gpe_tunnel_hw_if_indices
Definition: vxlan_gpe.h:103
static uword unformat_gpe_decap_next(unformat_input_t *input, va_list *args)
Definition: vxlan_gpe.c:294