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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief L2-GRE over IPSec packet processing.
19 * Add GRE header to thr packet and send it to the esp-encrypt node.
22 #include <vnet/vnet.h>
23 #include <vnet/ipsec-gre/ipsec_gre.h>
25 ipsec_gre_main_t ipsec_gre_main;
28 * @brief IPv4 and GRE header union.
35 ip4_and_gre_header_t ip4_and_gre;
38 } ip4_and_gre_union_t;
41 * @brief Packet trace.
46 u32 tunnel_id; /**< Tunnel-id / index in tunnel vector */
48 u32 length; /**< pkt length */
50 ip4_address_t src; /**< tunnel src IPv4 address */
51 ip4_address_t dst; /**< tunnel dst IPv4 address */
53 u32 sa_id; /**< tunnel IPSec SA id */
54 } ipsec_gre_tx_trace_t;
57 format_ipsec_gre_tx_trace (u8 * s, va_list * args)
59 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
60 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
61 ipsec_gre_tx_trace_t *t = va_arg (*args, ipsec_gre_tx_trace_t *);
63 s = format (s, "GRE: tunnel %d len %d src %U dst %U sa-id %d",
64 t->tunnel_id, clib_net_to_host_u16 (t->length),
65 format_ip4_address, &t->src.as_u8,
66 format_ip4_address, &t->dst.as_u8, t->sa_id);
71 * @brief IPSec-GRE tunnel interface tx function.
73 * Add GRE header to the packet.
75 * @param vm vlib_main_t corresponding to the current thread.
76 * @param node vlib_node_runtime_t data for this node.
77 * @param frame vlib_frame_t whose contents should be dispatched.
79 * @par Graph mechanics: buffer metadata, next index usage
82 * - <code>node->runtime_data</code>
83 * - Match tunnel by <code>rd->dev_instance</code> in IPSec-GRE tunnels
87 * - <code>vnet_buffer(b)->output_features.ipsec_sad_index</code>
88 * - Set IPSec Security Association for packet encryption.
89 * - <code>vnet_buffer(b)->sw_if_index[VLIB_TX]</code>
90 * - Reset output sw_if_index.
92 * <em>Nexd Index:</em>
93 * - Dispatches the packet to the esp-encrypt node.
96 ipsec_gre_interface_tx (vlib_main_t * vm,
97 vlib_node_runtime_t * node, vlib_frame_t * frame)
99 ipsec_gre_main_t *igm = &ipsec_gre_main;
101 u32 *from, *to_next, n_left_from, n_left_to_next;
102 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
103 ipsec_gre_tunnel_t *t = pool_elt_at_index (igm->tunnels, rd->dev_instance);
105 /* Vector of buffer / pkt indices we're supposed to process */
106 from = vlib_frame_vector_args (frame);
108 /* Number of buffers / pkts */
109 n_left_from = frame->n_vectors;
111 /* Speculatively send the first buffer to the last disposition we used */
112 next_index = node->cached_next_index;
114 while (n_left_from > 0)
116 /* set up to enqueue to our disposition with index = next_index */
117 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
120 * As long as we have enough pkts left to process two pkts
121 * and prefetch two pkts...
123 while (n_left_from >= 4 && n_left_to_next >= 2)
125 vlib_buffer_t *b0, *b1;
126 ip4_header_t *ip0, *ip1;
127 ip4_and_gre_union_t *h0, *h1;
128 u32 bi0, next0, bi1, next1;
129 __attribute__ ((unused)) u8 error0, error1;
130 u16 gre_protocol0, gre_protocol1;
132 /* Prefetch the next iteration */
134 vlib_buffer_t *p2, *p3;
136 p2 = vlib_get_buffer (vm, from[2]);
137 p3 = vlib_get_buffer (vm, from[3]);
139 vlib_prefetch_buffer_header (p2, LOAD);
140 vlib_prefetch_buffer_header (p3, LOAD);
143 * Prefetch packet data. We expect to overwrite
144 * the inbound L2 header with an ip header and a
145 * gre header. Might want to prefetch the last line
146 * of rewrite space as well; need profile data
148 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
149 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
152 /* Pick up the next two buffer indices */
156 /* Speculatively enqueue them where we sent the last buffer */
164 b0 = vlib_get_buffer (vm, bi0);
165 b1 = vlib_get_buffer (vm, bi1);
167 ip0 = vlib_buffer_get_current (b0);
168 gre_protocol0 = clib_net_to_host_u16 (0x01);
170 ip1 = vlib_buffer_get_current (b1);
171 gre_protocol1 = clib_net_to_host_u16 (0x01);
173 vlib_buffer_advance (b0, -sizeof (*h0));
174 vlib_buffer_advance (b1, -sizeof (*h1));
176 h0 = vlib_buffer_get_current (b0);
177 h1 = vlib_buffer_get_current (b1);
186 ip0 = &h0->ip4_and_gre.ip4;
187 h0->ip4_and_gre.gre.protocol = gre_protocol0;
188 ip0->ip_version_and_header_length = 0x45;
190 ip0->protocol = IP_PROTOCOL_GRE;
192 ip1 = &h1->ip4_and_gre.ip4;
193 h1->ip4_and_gre.gre.protocol = gre_protocol1;
194 ip1->ip_version_and_header_length = 0x45;
196 ip1->protocol = IP_PROTOCOL_GRE;
199 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
201 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
202 ip0->src_address.as_u32 = t->tunnel_src.as_u32;
203 ip1->src_address.as_u32 = t->tunnel_src.as_u32;
204 ip0->dst_address.as_u32 = t->tunnel_dst.as_u32;
205 ip1->dst_address.as_u32 = t->tunnel_dst.as_u32;
206 ip0->checksum = ip4_header_checksum (ip0);
207 ip1->checksum = ip4_header_checksum (ip1);
209 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
210 vnet_buffer (b0)->sw_if_index[VLIB_TX];
211 vnet_buffer (b1)->sw_if_index[VLIB_RX] =
212 vnet_buffer (b1)->sw_if_index[VLIB_TX];
214 vnet_buffer (b0)->ipsec.sad_index = t->local_sa;
215 vnet_buffer (b1)->ipsec.sad_index = t->local_sa;
217 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
218 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
220 next0 = IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT;
221 next1 = IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT;
222 error0 = IPSEC_GRE_ERROR_NONE;
223 error1 = IPSEC_GRE_ERROR_NONE;
225 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
227 ipsec_gre_tx_trace_t *tr = vlib_add_trace (vm, node,
229 tr->tunnel_id = t - igm->tunnels;
230 tr->length = ip0->length;
231 tr->src.as_u32 = ip0->src_address.as_u32;
232 tr->dst.as_u32 = ip0->dst_address.as_u32;
233 tr->sa_id = t->local_sa_id;
236 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
238 ipsec_gre_tx_trace_t *tr = vlib_add_trace (vm, node,
240 tr->tunnel_id = t - igm->tunnels;
241 tr->length = ip1->length;
242 tr->src.as_u32 = ip1->src_address.as_u32;
243 tr->dst.as_u32 = ip1->dst_address.as_u32;
244 tr->sa_id = t->local_sa_id;
247 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
248 to_next, n_left_to_next,
249 bi0, bi1, next0, next1);
252 while (n_left_from > 0 && n_left_to_next > 0)
256 ip4_and_gre_union_t *h0;
258 __attribute__ ((unused)) u8 error0;
261 bi0 = to_next[0] = from[0];
267 b0 = vlib_get_buffer (vm, bi0);
269 gre_protocol0 = clib_net_to_host_u16 (0x01);
271 vlib_buffer_advance (b0, -sizeof (*h0));
273 h0 = vlib_buffer_get_current (b0);
278 ip0 = &h0->ip4_and_gre.ip4;
279 h0->ip4_and_gre.gre.protocol = gre_protocol0;
280 ip0->ip_version_and_header_length = 0x45;
282 ip0->protocol = IP_PROTOCOL_GRE;
284 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
285 ip0->src_address.as_u32 = t->tunnel_src.as_u32;
286 ip0->dst_address.as_u32 = t->tunnel_dst.as_u32;
287 ip0->checksum = ip4_header_checksum (ip0);
289 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
290 vnet_buffer (b0)->sw_if_index[VLIB_TX];
291 vnet_buffer (b0)->ipsec.sad_index = t->local_sa;
292 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
294 next0 = IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT;
295 error0 = IPSEC_GRE_ERROR_NONE;
297 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
299 ipsec_gre_tx_trace_t *tr = vlib_add_trace (vm, node,
301 tr->tunnel_id = t - igm->tunnels;
302 tr->length = ip0->length;
303 tr->src.as_u32 = ip0->src_address.as_u32;
304 tr->dst.as_u32 = ip0->dst_address.as_u32;
305 tr->sa_id = t->local_sa_id;
308 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
309 to_next, n_left_to_next,
313 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
316 vlib_node_increment_counter (vm, ipsec_gre_input_node.index,
317 IPSEC_GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
319 return frame->n_vectors;
322 static clib_error_t *
323 ipsec_gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
326 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
327 vnet_hw_interface_set_flags (vnm, hw_if_index,
328 VNET_HW_INTERFACE_FLAG_LINK_UP);
330 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
332 return /* no error */ 0;
336 format_ipsec_gre_tunnel_name (u8 * s, va_list * args)
338 u32 dev_instance = va_arg (*args, u32);
339 return format (s, "ipsec-gre%d", dev_instance);
343 format_ipsec_gre_device (u8 * s, va_list * args)
345 u32 dev_instance = va_arg (*args, u32);
346 CLIB_UNUSED (int verbose) = va_arg (*args, int);
348 s = format (s, "IPSEC-GRE tunnel: id %d\n", dev_instance);
353 VNET_DEVICE_CLASS (ipsec_gre_device_class) = {
354 .name = "IPSec GRE tunnel device",
355 .format_device_name = format_ipsec_gre_tunnel_name,
356 .format_device = format_ipsec_gre_device,
357 .format_tx_trace = format_ipsec_gre_tx_trace,
358 .tx_function = ipsec_gre_interface_tx,
359 .admin_up_down_function = ipsec_gre_interface_admin_up_down,
362 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (ipsec_gre_device_class,
363 ipsec_gre_interface_tx)
366 VNET_HW_INTERFACE_CLASS (ipsec_gre_hw_interface_class) = {
371 static clib_error_t *
372 ipsec_gre_init (vlib_main_t * vm)
374 ipsec_gre_main_t *igm = &ipsec_gre_main;
377 memset (igm, 0, sizeof (igm[0]));
379 igm->vnet_main = vnet_get_main ();
381 if ((error = vlib_call_init_function (vm, ip_main_init)))
384 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
387 igm->tunnel_by_key = hash_create (0, sizeof (uword));
389 return vlib_call_init_function (vm, ipsec_gre_input_init);
392 VLIB_INIT_FUNCTION (ipsec_gre_init);
395 * fd.io coding-style-patch-verification: ON
398 * eval: (c-set-style "gnu")