FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
gre.c
Go to the documentation of this file.
1 /*
2  * gre.c: gre
3  *
4  * Copyright (c) 2012 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vnet/vnet.h>
19 #include <vnet/gre/gre.h>
20 #include <vnet/adj/adj_midchain.h>
21 
23 
24 typedef struct {
25  union {
26  ip4_and_gre_header_t ip4_and_gre;
27  u64 as_u64[3];
28  };
30 
31 
32 /* Packet trace structure */
33 typedef struct {
34  /* Tunnel-id / index in tunnel vector */
36 
37  /* pkt length */
39 
40  /* tunnel ip4 addresses */
44 
45 u8 * format_gre_tx_trace (u8 * s, va_list * args)
46 {
47  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
49  gre_tx_trace_t * t = va_arg (*args, gre_tx_trace_t *);
50 
51  s = format (s, "GRE: tunnel %d len %d src %U dst %U",
52  t->tunnel_id, clib_net_to_host_u16 (t->length),
55  return s;
56 }
57 
58 u8 * format_gre_protocol (u8 * s, va_list * args)
59 {
60  gre_protocol_t p = va_arg (*args, u32);
61  gre_main_t * gm = &gre_main;
63 
64  if (pi)
65  s = format (s, "%s", pi->name);
66  else
67  s = format (s, "0x%04x", p);
68 
69  return s;
70 }
71 
72 u8 * format_gre_header_with_length (u8 * s, va_list * args)
73 {
74  gre_main_t * gm = &gre_main;
75  gre_header_t * h = va_arg (*args, gre_header_t *);
76  u32 max_header_bytes = va_arg (*args, u32);
77  gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
78  uword indent, header_bytes;
79 
80  header_bytes = sizeof (h[0]);
81  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
82  return format (s, "gre header truncated");
83 
84  indent = format_get_indent (s);
85 
86  s = format (s, "GRE %U", format_gre_protocol, p);
87 
88  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
89  {
91  vlib_node_t * node = vlib_get_node (gm->vlib_main, pi->node_index);
92  if (node->format_buffer)
93  s = format (s, "\n%U%U",
94  format_white_space, indent,
95  node->format_buffer, (void *) (h + 1),
96  max_header_bytes - header_bytes);
97  }
98 
99  return s;
100 }
101 
102 u8 * format_gre_header (u8 * s, va_list * args)
103 {
104  gre_header_t * h = va_arg (*args, gre_header_t *);
105  return format (s, "%U", format_gre_header_with_length, h, 0);
106 }
107 
108 /* Returns gre protocol as an int in host byte order. */
109 uword
111  va_list * args)
112 {
113  u16 * result = va_arg (*args, u16 *);
114  gre_main_t * gm = &gre_main;
115  int i;
116 
117  /* Named type. */
119  gm->protocol_info_by_name, &i))
120  {
122  *result = pi->protocol;
123  return 1;
124  }
125 
126  return 0;
127 }
128 
129 uword
131  va_list * args)
132 {
133  u16 * result = va_arg (*args, u16 *);
135  return 0;
136  *result = clib_host_to_net_u16 ((u16) *result);
137  return 1;
138 }
139 
140 uword
141 unformat_gre_header (unformat_input_t * input, va_list * args)
142 {
143  u8 ** result = va_arg (*args, u8 **);
144  gre_header_t _h, * h = &_h;
145  u16 p;
146 
147  if (! unformat (input, "%U",
149  return 0;
150 
151  h->protocol = clib_host_to_net_u16 (p);
152 
153  /* Add header to result. */
154  {
155  void * p;
156  u32 n_bytes = sizeof (h[0]);
157 
158  vec_add2 (*result, p, n_bytes);
159  clib_memcpy (p, h, n_bytes);
160  }
161 
162  return 1;
163 }
164 
165 static int
167 {
168  switch (link)
169  {
170  case VNET_LINK_IP4:
171  return (GRE_PROTOCOL_ip4);
172  case VNET_LINK_IP6:
173  return (GRE_PROTOCOL_ip6);
174  case VNET_LINK_MPLS:
175  return (GRE_PROTOCOL_mpls_unicast);
176  case VNET_LINK_ETHERNET:
177  return (GRE_PROTOCOL_teb);
178  case VNET_LINK_ARP:
179  return (GRE_PROTOCOL_arp);
180  case VNET_LINK_NSH:
181  ASSERT(0);
182  break;
183  }
184  ASSERT(0);
185  return (GRE_PROTOCOL_ip4);
186 }
187 
188 static u8*
190  u32 sw_if_index,
191  vnet_link_t link_type,
192  const void *dst_address)
193 {
194  gre_main_t * gm = &gre_main;
195  ip4_and_gre_header_t * h;
196  u8* rewrite = NULL;
197  gre_tunnel_t *t;
198  u32 ti;
199 
200  ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
201 
202  if (~0 == ti)
203  /* not one of ours */
204  return (0);
205 
206  t = pool_elt_at_index(gm->tunnels, ti);
207 
208  vec_validate(rewrite, sizeof(*h)-1);
209  h = (ip4_and_gre_header_t*)rewrite;
210  h->gre.protocol = clib_host_to_net_u16(gre_proto_from_vnet_link(link_type));
211 
212  h->ip4.ip_version_and_header_length = 0x45;
213  h->ip4.ttl = 254;
214  h->ip4.protocol = IP_PROTOCOL_GRE;
215  /* fixup ip4 header length and checksum after-the-fact */
216  h->ip4.src_address.as_u32 = t->tunnel_src.as_u32;
217  h->ip4.dst_address.as_u32 = t->tunnel_dst.as_u32;
218  h->ip4.checksum = ip4_header_checksum (&h->ip4);
219 
220  return (rewrite);
221 }
222 
223 void
225  ip_adjacency_t *adj,
226  vlib_buffer_t *b0)
227 {
228  ip4_header_t * ip0;
229 
230  ip0 = vlib_buffer_get_current (b0);
231 
232  /* Fixup the checksum and len fields in the GRE tunnel encap
233  * that was applied at the midchain node */
234  ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
235  ip0->checksum = ip4_header_checksum (ip0);
236 }
237 
238 void
240  u32 sw_if_index,
241  adj_index_t ai)
242 {
247  gre_build_rewrite(vnm, sw_if_index,
248  adj_get_link_type(ai),
249  NULL));
250 
251  gre_tunnel_stack(ai);
252 }
253 
254 /**
255  * @brief TX function. Only called L2. L3 traffic uses the adj-midchains
256  */
257 static uword
259  vlib_node_runtime_t * node,
260  vlib_frame_t * frame)
261 {
262  gre_main_t * gm = &gre_main;
263  u32 next_index;
264  u32 * from, * to_next, n_left_from, n_left_to_next;
265  vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
266  const gre_tunnel_t *gt = pool_elt_at_index (gm->tunnels, rd->dev_instance);
267 
268  /* Vector of buffer / pkt indices we're supposed to process */
269  from = vlib_frame_vector_args (frame);
270 
271  /* Number of buffers / pkts */
272  n_left_from = frame->n_vectors;
273 
274  /* Speculatively send the first buffer to the last disposition we used */
275  next_index = node->cached_next_index;
276 
277  while (n_left_from > 0)
278  {
279  /* set up to enqueue to our disposition with index = next_index */
280  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
281 
282  /*
283  * FIXME DUAL LOOP
284  */
285 
286  while (n_left_from > 0 && n_left_to_next > 0)
287  {
288  vlib_buffer_t * b0;
289  u32 bi0;
290 
291  bi0 = from[0];
292  to_next[0] = bi0;
293  from += 1;
294  to_next += 1;
295  n_left_from -= 1;
296  n_left_to_next -= 1;
297 
298  b0 = vlib_get_buffer(vm, bi0);
299 
300  vnet_buffer(b0)->ip.adj_index[VLIB_TX] = gt->l2_adj_index;
301 
303  {
304  gre_tx_trace_t *tr = vlib_add_trace (vm, node,
305  b0, sizeof (*tr));
306  tr->tunnel_id = gt - gm->tunnels;
307  tr->length = vlib_buffer_length_in_chain (vm, b0);
308  tr->src.as_u32 = gt->tunnel_src.as_u32;
309  tr->dst.as_u32 = gt->tunnel_src.as_u32;
310  }
311 
312  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
313  to_next, n_left_to_next,
314  bi0, gt->l2_tx_arc);
315  }
316 
317  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
318  }
319 
321  GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
322 
323  return frame->n_vectors;
324 }
325 
326 static uword
328  vlib_node_runtime_t * node,
329  vlib_frame_t * frame)
330 {
331  return (gre_interface_tx_inline (vm, node, frame));
332 }
333 
334 static uword
336  vlib_node_runtime_t * node,
337  vlib_frame_t * frame)
338 {
339  return (gre_interface_tx_inline (vm, node, frame));
340 }
341 
342 static u8 * format_gre_tunnel_name (u8 * s, va_list * args)
343 {
344  u32 dev_instance = va_arg (*args, u32);
345  return format (s, "gre%d", dev_instance);
346 }
347 
348 static u8 * format_gre_tunnel_teb_name (u8 * s, va_list * args)
349 {
350  u32 dev_instance = va_arg (*args, u32);
351  return format (s, "teb-gre%d", dev_instance);
352 }
353 
354 static u8 * format_gre_device (u8 * s, va_list * args)
355 {
356  u32 dev_instance = va_arg (*args, u32);
357  CLIB_UNUSED (int verbose) = va_arg (*args, int);
358 
359  s = format (s, "GRE tunnel: id %d\n", dev_instance);
360  return s;
361 }
362 
364  .name = "GRE tunnel device",
365  .format_device_name = format_gre_tunnel_name,
366  .format_device = format_gre_device,
367  .format_tx_trace = format_gre_tx_trace,
368  .tx_function = gre_interface_tx,
369  .admin_up_down_function = gre_interface_admin_up_down,
370 #ifdef SOON
371  .clear counter = 0;
372 #endif
373 };
374 
377 
379  .name = "GRE TEB tunnel device",
380  .format_device_name = format_gre_tunnel_teb_name,
381  .format_device = format_gre_device,
382  .format_tx_trace = format_gre_tx_trace,
383  .tx_function = gre_teb_interface_tx,
384  .admin_up_down_function = gre_interface_admin_up_down,
385 #ifdef SOON
386  .clear counter = 0;
387 #endif
388 };
389 
392 
394  .name = "GRE",
395  .format_header = format_gre_header_with_length,
396  .unformat_header = unformat_gre_header,
397  .build_rewrite = gre_build_rewrite,
398  .update_adjacency = gre_update_adj,
400 };
401 
402 static void add_protocol (gre_main_t * gm,
403  gre_protocol_t protocol,
404  char * protocol_name)
405 {
406  gre_protocol_info_t * pi;
407  u32 i;
408 
409  vec_add2 (gm->protocol_infos, pi, 1);
410  i = pi - gm->protocol_infos;
411 
412  pi->name = protocol_name;
413  pi->protocol = protocol;
414  pi->next_index = pi->node_index = ~0;
415 
416  hash_set (gm->protocol_info_by_protocol, protocol, i);
418 }
419 
421 {
422  gre_main_t * gm = &gre_main;
423  clib_error_t * error;
424  ip_main_t * im = &ip_main;
425  ip_protocol_info_t * pi;
426 
427  memset (gm, 0, sizeof (gm[0]));
428  gm->vlib_main = vm;
429  gm->vnet_main = vnet_get_main();
430 
431  if ((error = vlib_call_init_function (vm, ip_main_init)))
432  return error;
433 
434  if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
435  return error;
436 
437  /* Set up the ip packet generator */
438  pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
441 
442  gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
443  gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
444  gm->tunnel_by_key = hash_create (0, sizeof (uword));
445 
446 #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
448 #undef _
449 
451 }
452 
454 
456 {
458  return &gre_main;
459 }
460 
vnet_main_t * vnet_main
Definition: gre.h:168
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:436
ip4_address_t src
Definition: gre.c:41
static uword gre_interface_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: gre.c:327
#define hash_set(h, key, value)
Definition: hash.h:254
GRE related global data.
Definition: gre.h:130
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
#define CLIB_UNUSED(x)
Definition: clib.h:79
ip4_address_t dst
Definition: gre.c:42
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:463
uword * protocol_info_by_protocol
Definition: gre.h:144
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define VNET_HW_INTERFACE_CLASS(x,...)
Definition: interface.h:358
u64 as_u64
Definition: bihash_doc.h:63
uword * tunnel_by_key
Hash mapping src/dst addr pair to tunnel.
Definition: gre.h:148
u32 tunnel_id
Definition: gre.c:35
static u8 * format_gre_tunnel_teb_name(u8 *s, va_list *args)
Definition: gre.c:348
vnet_link_t adj_get_link_type(adj_index_t ai)
Return the link type of the adjacency.
Definition: adj.c:340
#define NULL
Definition: clib.h:55
gre_protocol_t protocol
GRE protocol type in host byte order.
Definition: gre.h:47
char * name
Name (a c string).
Definition: gre.h:44
IP unicast adjacency.
Definition: lookup.h:193
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:459
A GRE payload protocol registration.
Definition: gre.h:42
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:561
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:982
#define hash_set_mem(h, key, value)
Definition: hash.h:274
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
Definition: ip.h:106
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:100
u32 * tunnel_index_by_sw_if_index
Mapping from sw_if_index to tunnel index.
Definition: gre.h:160
VNET_DEVICE_CLASS(gre_device_class)
void adj_nbr_midchain_update_rewrite(adj_index_t adj_index, adj_midchain_fixup_t fixup, adj_midchain_flag_t flags, u8 *rewrite)
adj_nbr_midchain_update_rewrite
Definition: adj_midchain.c:395
ip4_address_t tunnel_dst
The tunnel&#39;s destination/remote address.
Definition: gre.h:94
format_function_t format_ip4_address
Definition: format.h:79
unformat_function_t * unformat_pg_edit
Definition: ip.h:87
ip4_and_gre_header_t ip4_and_gre
Definition: gre.c:26
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
IPv4 and GRE header union.
Definition: gre.c:24
static uword format_get_indent(u8 *s)
Definition: format.h:72
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
void gre_tunnel_stack(adj_index_t ai)
gre_tunnel_stack
Definition: interface.c:118
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
unsigned long u64
Definition: types.h:89
static uword gre_interface_tx_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
TX function.
Definition: gre.c:258
#define vlib_call_init_function(vm, x)
Definition: init.h:162
uword unformat_gre_header(unformat_input_t *input, va_list *args)
Definition: gre.c:141
#define hash_create_string(elts, value_bytes)
Definition: hash.h:652
void gre_fixup(vlib_main_t *vm, ip_adjacency_t *adj, vlib_buffer_t *b0)
Definition: gre.c:224
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
A representation of a GRE tunnel.
Definition: gre.h:81
static ip_protocol_info_t * ip_get_protocol_info(ip_main_t *im, u32 protocol)
Definition: ip.h:133
format_function_t * format_header
Definition: ip.h:78
struct _unformat_input_t unformat_input_t
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:188
u32 node_index
Node which handles this type.
Definition: gre.h:50
void gre_update_adj(vnet_main_t *vnm, u32 sw_if_index, adj_index_t ai)
Definition: gre.c:239
vnet_device_class_t gre_device_teb_class
#define PREDICT_FALSE(x)
Definition: clib.h:97
format_function_t * format_buffer
Definition: node.h:311
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:216
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:350
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1115
uword * protocol_info_by_name
Hash tables mapping name/protocol to protocol info index.
Definition: gre.h:144
static u8 * format_gre_tunnel_name(u8 *s, va_list *args)
Definition: gre.c:342
Packets TX through the midchain do not increment the interface counters.
Definition: adj_midchain.h:44
static clib_error_t * gre_input_init(vlib_main_t *vm)
Definition: node.c:473
static clib_error_t * gre_init(vlib_main_t *vm)
Definition: gre.c:420
gre_main_t * gre_get_main(vlib_main_t *vm)
Definition: gre.c:455
clib_error_t * ip_main_init(vlib_main_t *vm)
Definition: ip_init.c:45
u16 n_vectors
Definition: node.h:344
vlib_main_t * vm
Definition: buffer.c:276
vec_header_t h
Definition: buffer.c:275
static u8 * format_gre_device(u8 *s, va_list *args)
Definition: gre.c:354
ip_main_t ip_main
Definition: ip_init.c:42
u16 protocol
Definition: packet.h:52
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:85
gre_protocol_t
Definition: packet.h:29
#define clib_memcpy(a, b, c)
Definition: string.h:69
ip4_address_t tunnel_src
The tunnel&#39;s source/local address.
Definition: gre.h:90
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
u8 * format_gre_header_with_length(u8 *s, va_list *args)
Definition: gre.c:72
u8 * format_gre_tx_trace(u8 *s, va_list *args)
Definition: gre.c:45
#define hash_create(elts, value_bytes)
Definition: hash.h:658
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:455
#define ASSERT(truth)
static u8 * gre_build_rewrite(vnet_main_t *vnm, u32 sw_if_index, vnet_link_t link_type, const void *dst_address)
Definition: gre.c:189
unsigned int u32
Definition: types.h:88
u32 next_index
Next index for this type.
Definition: gre.h:53
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
static int gre_proto_from_vnet_link(vnet_link_t link)
Definition: gre.c:166
gre_tunnel_t * tunnels
pool of tunnel instances
Definition: gre.h:134
uword unformat_vlib_number_by_name(unformat_input_t *input, va_list *args)
Definition: format.c:157
clib_error_t * ip4_lookup_init(vlib_main_t *vm)
Definition: ip4_forward.c:1120
vnet_hw_interface_class_t gre_hw_interface_class
vlib_node_registration_t gre_input_node
(constructor) VLIB_REGISTER_NODE (gre_input_node)
Definition: node.c:412
u64 uword
Definition: types.h:112
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
Definition: defs.h:47
vnet_device_class_t gre_device_class
VLIB_DEVICE_TX_FUNCTION_MULTIARCH(gre_device_class, gre_interface_tx)
Definition: gre.c:375
clib_error_t * gre_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:448
unsigned short u16
Definition: types.h:57
static void add_protocol(gre_main_t *gm, gre_protocol_t protocol, char *protocol_name)
Definition: gre.c:402
unsigned char u8
Definition: types.h:56
uword unformat_gre_protocol_host_byte_order(unformat_input_t *input, va_list *args)
Definition: gre.c:110
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:253
a point 2 point interface
Definition: interface.h:274
u8 * format_gre_header(u8 *s, va_list *args)
Definition: gre.c:102
unformat_function_t unformat_pg_gre_header
Definition: gre.h:218
u8 * format_gre_protocol(u8 *s, va_list *args)
Definition: gre.c:58
#define vnet_buffer(b)
Definition: buffer.h:294
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:58
static uword gre_teb_interface_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: gre.c:335
static gre_protocol_info_t * gre_get_protocol_info(gre_main_t *em, gre_protocol_t protocol)
Definition: gre.h:180
gre_protocol_info_t * protocol_infos
GRE payload protocol registrations.
Definition: gre.h:139
u32 length
Definition: gre.c:38
vlib_main_t * vlib_main
Definition: gre.h:167
uword unformat_gre_protocol_net_byte_order(unformat_input_t *input, va_list *args)
Definition: gre.c:130
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:74
gre_main_t gre_main
Definition: gre.c:22
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:238
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:971