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