FD.io VPP  v19.04.2-12-g66b1689
Vector Packet Processing
ipsec_gre.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 /**
16  * @file
17  * @brief L2-GRE over IPSec packet processing.
18  *
19  * Add GRE header to thr packet and send it to the esp-encrypt node.
20 */
21 
22 #include <vnet/vnet.h>
24 
26 
27 #ifndef CLIB_MARCH_VARIANT
29 #endif /* CLIB_MARCH_VARIANT */
30 
31 /**
32  * @brief IPv4 and GRE header union.
33  *
34 */
35 typedef struct
36 {
37  union
38  {
39  ip4_and_gre_header_t ip4_and_gre;
40  u64 as_u64[3];
41  };
43 
44 /**
45  * @brief Packet trace.
46  *
47 */
48 typedef struct
49 {
50  u32 tunnel_id; /**< Tunnel-id / index in tunnel vector */
51 
52  u32 length; /**< pkt length */
53 
54  ip4_address_t src; /**< tunnel src IPv4 address */
55  ip4_address_t dst; /**< tunnel dst IPv4 address */
56 
57  u32 sa_id; /**< tunnel IPSec SA id */
59 
60 static u8 *
61 format_ipsec_gre_tx_trace (u8 * s, va_list * args)
62 {
63  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
64  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
65  ipsec_gre_tx_trace_t *t = va_arg (*args, ipsec_gre_tx_trace_t *);
66 
67  s = format (s, "GRE: tunnel %d len %d src %U dst %U sa-id %d",
68  t->tunnel_id, clib_net_to_host_u16 (t->length),
71  return s;
72 }
73 
74 /**
75  * @brief IPSec-GRE tunnel interface tx function.
76  *
77  * Add GRE header to the packet.
78  *
79  * @param vm vlib_main_t corresponding to the current thread.
80  * @param node vlib_node_runtime_t data for this node.
81  * @param frame vlib_frame_t whose contents should be dispatched.
82  *
83  * @par Graph mechanics: buffer metadata, next index usage
84  *
85  * <em>Uses:</em>
86  * - <code>node->runtime_data</code>
87  * - Match tunnel by <code>rd->dev_instance</code> in IPSec-GRE tunnels
88  * pool.
89  *
90  * <em>Sets:</em>
91  * - <code>vnet_buffer(b)->output_features.ipsec_sad_index</code>
92  * - Set IPSec Security Association for packet encryption.
93  * - <code>vnet_buffer(b)->sw_if_index[VLIB_TX]</code>
94  * - Reset output sw_if_index.
95  *
96  * <em>Next Index:</em>
97  * - Dispatches the packet to the esp-encrypt node.
98 */
100  vlib_node_runtime_t * node,
101  vlib_frame_t * frame)
102 {
104  u32 next_index;
105  u32 *from, *to_next, n_left_from, n_left_to_next;
106  vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
108 
109  u16 l2_gre_protocol_ethertype = clib_net_to_host_u16 (GRE_PROTOCOL_teb);
110 
111  /* Vector of buffer / pkt indices we're supposed to process */
112  from = vlib_frame_vector_args (frame);
113 
114  /* Number of buffers / pkts */
115  n_left_from = frame->n_vectors;
116 
117  /* Speculatively send the first buffer to the last disposition we used */
118  next_index = node->cached_next_index;
119 
120  while (n_left_from > 0)
121  {
122  /* set up to enqueue to our disposition with index = next_index */
123  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
124 
125  /*
126  * As long as we have enough pkts left to process two pkts
127  * and prefetch two pkts...
128  */
129  while (n_left_from >= 4 && n_left_to_next >= 2)
130  {
131  vlib_buffer_t *b0, *b1;
132  ip4_header_t *ip0, *ip1;
133  ip4_and_gre_union_t *h0, *h1;
134  u32 bi0, next0, bi1, next1;
135  __attribute__ ((unused)) u8 error0, error1;
136 
137  /* Prefetch the next iteration */
138  {
139  vlib_buffer_t *p2, *p3;
140 
141  p2 = vlib_get_buffer (vm, from[2]);
142  p3 = vlib_get_buffer (vm, from[3]);
143 
144  vlib_prefetch_buffer_header (p2, LOAD);
145  vlib_prefetch_buffer_header (p3, LOAD);
146 
147  /*
148  * Prefetch packet data. We expect to overwrite
149  * the inbound L2 header with an ip header and a
150  * gre header. Might want to prefetch the last line
151  * of rewrite space as well; need profile data
152  */
155  }
156 
157  /* Pick up the next two buffer indices */
158  bi0 = from[0];
159  bi1 = from[1];
160 
161  /* Speculatively enqueue them where we sent the last buffer */
162  to_next[0] = bi0;
163  to_next[1] = bi1;
164  from += 2;
165  to_next += 2;
166  n_left_to_next -= 2;
167  n_left_from -= 2;
168 
169  b0 = vlib_get_buffer (vm, bi0);
170  b1 = vlib_get_buffer (vm, bi1);
171 
172  vlib_buffer_advance (b0, -sizeof (*h0));
173  vlib_buffer_advance (b1, -sizeof (*h1));
174 
175  h0 = vlib_buffer_get_current (b0);
176  h1 = vlib_buffer_get_current (b1);
177  h0->as_u64[0] = 0;
178  h0->as_u64[1] = 0;
179  h0->as_u64[2] = 0;
180 
181  h1->as_u64[0] = 0;
182  h1->as_u64[1] = 0;
183  h1->as_u64[2] = 0;
184 
185  ip0 = &h0->ip4_and_gre.ip4;
186  h0->ip4_and_gre.gre.protocol = l2_gre_protocol_ethertype;
187  ip0->ip_version_and_header_length = 0x45;
188  ip0->ttl = 254;
189  ip0->protocol = IP_PROTOCOL_GRE;
190 
191  ip1 = &h1->ip4_and_gre.ip4;
192  h1->ip4_and_gre.gre.protocol = l2_gre_protocol_ethertype;
193  ip1->ip_version_and_header_length = 0x45;
194  ip1->ttl = 254;
195  ip1->protocol = IP_PROTOCOL_GRE;
196 
197  ip0->length =
198  clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
199  ip1->length =
200  clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
201  ip0->src_address.as_u32 = t->tunnel_src.as_u32;
202  ip1->src_address.as_u32 = t->tunnel_src.as_u32;
203  ip0->dst_address.as_u32 = t->tunnel_dst.as_u32;
204  ip1->dst_address.as_u32 = t->tunnel_dst.as_u32;
205  ip0->checksum = ip4_header_checksum (ip0);
206  ip1->checksum = ip4_header_checksum (ip1);
207 
208  vnet_buffer (b0)->sw_if_index[VLIB_RX] =
209  vnet_buffer (b0)->sw_if_index[VLIB_TX];
210  vnet_buffer (b1)->sw_if_index[VLIB_RX] =
211  vnet_buffer (b1)->sw_if_index[VLIB_TX];
212 
213  vnet_buffer (b0)->ipsec.sad_index = t->local_sa;
214  vnet_buffer (b1)->ipsec.sad_index = t->local_sa;
215 
216  vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
217  vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
218 
221  error0 = IPSEC_GRE_ERROR_NONE;
222  error1 = IPSEC_GRE_ERROR_NONE;
223 
224  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
225  {
226  ipsec_gre_tx_trace_t *tr = vlib_add_trace (vm, node,
227  b0, sizeof (*tr));
228  tr->tunnel_id = t - igm->tunnels;
229  tr->length = ip0->length;
230  tr->src.as_u32 = ip0->src_address.as_u32;
231  tr->dst.as_u32 = ip0->dst_address.as_u32;
232  tr->sa_id = t->local_sa_id;
233  }
234 
235  if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
236  {
237  ipsec_gre_tx_trace_t *tr = vlib_add_trace (vm, node,
238  b1, sizeof (*tr));
239  tr->tunnel_id = t - igm->tunnels;
240  tr->length = ip1->length;
241  tr->src.as_u32 = ip1->src_address.as_u32;
242  tr->dst.as_u32 = ip1->dst_address.as_u32;
243  tr->sa_id = t->local_sa_id;
244  }
245 
246  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
247  to_next, n_left_to_next,
248  bi0, bi1, next0, next1);
249  }
250 
251  while (n_left_from > 0 && n_left_to_next > 0)
252  {
253  vlib_buffer_t *b0;
254  ip4_header_t *ip0;
256  u32 bi0, next0;
257  __attribute__ ((unused)) u8 error0;
258 
259  bi0 = to_next[0] = from[0];
260  from += 1;
261  n_left_from -= 1;
262  to_next += 1;
263  n_left_to_next -= 1;
264 
265  b0 = vlib_get_buffer (vm, bi0);
266 
267  vlib_buffer_advance (b0, -sizeof (*h0));
268 
269  h0 = vlib_buffer_get_current (b0);
270  h0->as_u64[0] = 0;
271  h0->as_u64[1] = 0;
272  h0->as_u64[2] = 0;
273 
274  ip0 = &h0->ip4_and_gre.ip4;
275  h0->ip4_and_gre.gre.protocol = l2_gre_protocol_ethertype;
276  ip0->ip_version_and_header_length = 0x45;
277  ip0->ttl = 254;
278  ip0->protocol = IP_PROTOCOL_GRE;
279  ip0->length =
280  clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
281  ip0->src_address.as_u32 = t->tunnel_src.as_u32;
282  ip0->dst_address.as_u32 = t->tunnel_dst.as_u32;
283  ip0->checksum = ip4_header_checksum (ip0);
284 
285  vnet_buffer (b0)->sw_if_index[VLIB_RX] =
286  vnet_buffer (b0)->sw_if_index[VLIB_TX];
287  vnet_buffer (b0)->ipsec.sad_index = t->local_sa;
288  vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
289 
291  error0 = IPSEC_GRE_ERROR_NONE;
292 
293  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
294  {
295  ipsec_gre_tx_trace_t *tr = vlib_add_trace (vm, node,
296  b0, sizeof (*tr));
297  tr->tunnel_id = t - igm->tunnels;
298  tr->length = ip0->length;
299  tr->src.as_u32 = ip0->src_address.as_u32;
300  tr->dst.as_u32 = ip0->dst_address.as_u32;
301  tr->sa_id = t->local_sa_id;
302  }
303 
304  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
305  to_next, n_left_to_next,
306  bi0, next0);
307  }
308 
309  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
310  }
311 
313  IPSEC_GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
314 
315  return frame->n_vectors;
316 }
317 
318 static clib_error_t *
320  u32 flags)
321 {
323  vnet_hw_interface_set_flags (vnm, hw_if_index,
325  else
326  vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
327 
328  return /* no error */ 0;
329 }
330 
331 static u8 *
332 format_ipsec_gre_tunnel_name (u8 * s, va_list * args)
333 {
334  u32 dev_instance = va_arg (*args, u32);
335  return format (s, "ipsec-gre%d", dev_instance);
336 }
337 
338 static u8 *
339 format_ipsec_gre_device (u8 * s, va_list * args)
340 {
341  u32 dev_instance = va_arg (*args, u32);
342  CLIB_UNUSED (int verbose) = va_arg (*args, int);
343 
344  s = format (s, "IPSEC-GRE tunnel: id %d\n", dev_instance);
345  return s;
346 }
347 
348 /* *INDENT-OFF* */
350  .name = "IPSec GRE tunnel device",
351  .format_device_name = format_ipsec_gre_tunnel_name,
352  .format_device = format_ipsec_gre_device,
353  .format_tx_trace = format_ipsec_gre_tx_trace,
354  .admin_up_down_function = ipsec_gre_interface_admin_up_down,
355 };
356 
357 
358 #ifndef CLIB_MARCH_VARIANT
360  .name = "IPSEC-GRE",
361 };
362 #endif /* CLIB_MARCH_VARIANT */
363 /* *INDENT-ON* */
364 
365 static clib_error_t *
367 {
369  clib_error_t *error;
370 
371  clib_memset (igm, 0, sizeof (igm[0]));
372  igm->vlib_main = vm;
373  igm->vnet_main = vnet_get_main ();
374 
375  if ((error = vlib_call_init_function (vm, ip_main_init)))
376  return error;
377 
378  if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
379  return error;
380 
381  igm->tunnel_by_key = hash_create (0, sizeof (uword));
382 
384 }
385 
387 
388 /*
389 * fd.io coding-style-patch-verification: ON
390 *
391 * Local Variables:
392 * eval: (c-set-style "gnu")
393 * End:
394 */
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
VNET_HW_INTERFACE_CLASS(ipsec_gre_hw_interface_class)
u32 flags
Definition: vhost_user.h:115
#define CLIB_UNUSED(x)
Definition: clib.h:82
ip4_address_t src_address
Definition: ip4_packet.h:170
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
static clib_error_t * ip4_lookup_init(vlib_main_t *vm)
Definition: ip4_forward.c:909
L2-GRE over IPSec packet processing.
u64 as_u64
Definition: bihash_doc.h:63
unsigned long u64
Definition: types.h:89
u8 data[0]
Packet data.
Definition: buffer.h:181
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
static clib_error_t * ipsec_gre_init(vlib_main_t *vm)
Definition: ipsec_gre.c:366
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:366
static u8 * format_ipsec_gre_tx_trace(u8 *s, va_list *args)
Definition: ipsec_gre.c:61
unsigned char u8
Definition: types.h:56
static clib_error_t * ipsec_gre_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: ipsec_gre.c:319
#define IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT
Definition: ipsec_gre.h:91
ipsec_gre_tunnel_t * tunnels
pool of tunnel instances
Definition: ipsec_gre.h:72
format_function_t format_ip4_address
Definition: format.h:75
ip4_and_gre_header_t ip4_and_gre
Definition: gre.c:31
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
IPv4 and GRE header union.
Definition: gre.c:27
ip4_address_t dst_address
Definition: ip4_packet.h:170
ipsec_gre_main_t ipsec_gre_main
Definition: ipsec_gre.c:28
Packet trace.
Definition: ipsec_gre.c:48
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
vnet_hw_interface_class_t ipsec_gre_hw_interface_class
static u8 * format_ipsec_gre_tunnel_name(u8 *s, va_list *args)
Definition: ipsec_gre.c:332
#define VNET_DEVICE_CLASS_TX_FN(devclass)
Definition: interface.h:287
unsigned int u32
Definition: types.h:88
u64 as_u64[3]
Definition: gre.c:32
#define vlib_call_init_function(vm, x)
Definition: init.h:260
ip4_address_t dst
tunnel dst IPv4 address
Definition: ipsec_gre.c:55
uword * tunnel_by_key
hash mapping src/dst addr pair to tunnel
Definition: ipsec_gre.h:74
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
u32 length
pkt length
Definition: ipsec_gre.c:52
static clib_error_t * ipsec_gre_input_init(vlib_main_t *vm)
Definition: node.c:414
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
VNET_DEVICE_CLASS(ipsec_gre_device_class)
#define PREDICT_FALSE(x)
Definition: clib.h:111
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Finish enqueueing two buffers forward in the graph.
Definition: buffer_node.h:70
#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:218
#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:368
ip4_address_t src
tunnel src IPv4 address
Definition: ipsec_gre.c:54
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1180
vlib_node_registration_t ipsec_gre_input_node
(constructor) VLIB_REGISTER_NODE (ipsec_gre_input_node)
Definition: node.c:396
clib_error_t * ip_main_init(vlib_main_t *vm)
Definition: ip_init.c:45
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
vlib_main_t * vm
Definition: buffer.c:312
u32 sa_id
tunnel IPSec SA id
Definition: ipsec_gre.c:57
IPSec-GRE state.
Definition: ipsec_gre.h:70
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:465
#define hash_create(elts, value_bytes)
Definition: hash.h:696
IPSec-GRE tunnel parameters.
Definition: ipsec_gre.h:52
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:248
vnet_main_t * vnet_main
convenience
Definition: ipsec_gre.h:82
VNET_DEVICE_CLASS_TX_FN() ipsec_gre_device_class(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
IPSec-GRE tunnel interface tx function.
Definition: ipsec_gre.c:99
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:57
Definition: defs.h:47
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_interface_flags_t flags)
Definition: interface.c:504
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:274
#define vnet_buffer(b)
Definition: buffer.h:369
u32 tunnel_id
Tunnel-id / index in tunnel vector.
Definition: ipsec_gre.c:50
u8 ip_version_and_header_length
Definition: ip4_packet.h:138
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:247
vlib_main_t * vlib_main
convenience
Definition: ipsec_gre.h:81
Definition: defs.h:46
static u8 * format_ipsec_gre_device(u8 *s, va_list *args)
Definition: ipsec_gre.c:339