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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/vnet.h>
19 #include <vnet/gre/gre.h>
20 #include <vnet/adj/adj_midchain.h>
26 ip4_and_gre_header_t ip4_and_gre;
29 } ip4_and_gre_union_t;
32 /* Packet trace structure */
34 /* Tunnel-id / index in tunnel vector */
40 /* tunnel ip4 addresses */
45 u8 * format_gre_tx_trace (u8 * s, va_list * args)
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 *);
51 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
52 t->tunnel_id, clib_net_to_host_u16 (t->length),
53 format_ip4_address, &t->src.as_u8,
54 format_ip4_address, &t->dst.as_u8);
58 u8 * format_gre_protocol (u8 * s, va_list * args)
60 gre_protocol_t p = va_arg (*args, u32);
61 gre_main_t * gm = &gre_main;
62 gre_protocol_info_t * pi = gre_get_protocol_info (gm, p);
65 s = format (s, "%s", pi->name);
67 s = format (s, "0x%04x", p);
72 u8 * format_gre_header_with_length (u8 * s, va_list * args)
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;
80 header_bytes = sizeof (h[0]);
81 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
82 return format (s, "gre header truncated");
84 indent = format_get_indent (s);
86 s = format (s, "GRE %U", format_gre_protocol, p);
88 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
90 gre_protocol_info_t * pi = gre_get_protocol_info (gm, p);
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);
102 u8 * format_gre_header (u8 * s, va_list * args)
104 gre_header_t * h = va_arg (*args, gre_header_t *);
105 return format (s, "%U", format_gre_header_with_length, h, 0);
108 /* Returns gre protocol as an int in host byte order. */
110 unformat_gre_protocol_host_byte_order (unformat_input_t * input,
113 u16 * result = va_arg (*args, u16 *);
114 gre_main_t * gm = &gre_main;
118 if (unformat_user (input, unformat_vlib_number_by_name,
119 gm->protocol_info_by_name, &i))
121 gre_protocol_info_t * pi = vec_elt_at_index (gm->protocol_infos, i);
122 *result = pi->protocol;
130 unformat_gre_protocol_net_byte_order (unformat_input_t * input,
133 u16 * result = va_arg (*args, u16 *);
134 if (! unformat_user (input, unformat_gre_protocol_host_byte_order, result))
136 *result = clib_host_to_net_u16 ((u16) *result);
141 unformat_gre_header (unformat_input_t * input, va_list * args)
143 u8 ** result = va_arg (*args, u8 **);
144 gre_header_t _h, * h = &_h;
147 if (! unformat (input, "%U",
148 unformat_gre_protocol_host_byte_order, &p))
151 h->protocol = clib_host_to_net_u16 (p);
153 /* Add header to result. */
156 u32 n_bytes = sizeof (h[0]);
158 vec_add2 (*result, p, n_bytes);
159 clib_memcpy (p, h, n_bytes);
166 gre_proto_from_vnet_link (vnet_link_t link)
171 return (GRE_PROTOCOL_ip4);
173 return (GRE_PROTOCOL_ip6);
175 return (GRE_PROTOCOL_mpls_unicast);
176 case VNET_LINK_ETHERNET:
177 return (GRE_PROTOCOL_teb);
179 return (GRE_PROTOCOL_arp);
185 return (GRE_PROTOCOL_ip4);
189 gre_build_rewrite (vnet_main_t * vnm,
191 vnet_link_t link_type,
192 const void *dst_address)
194 gre_main_t * gm = &gre_main;
195 ip4_and_gre_header_t * h;
200 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
203 /* not one of ours */
206 t = pool_elt_at_index(gm->tunnels, ti);
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));
212 h->ip4.ip_version_and_header_length = 0x45;
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);
224 gre_fixup (vlib_main_t *vm,
230 ip0 = vlib_buffer_get_current (b0);
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);
239 gre_update_adj (vnet_main_t * vnm,
243 adj_nbr_midchain_update_rewrite (ai, gre_fixup,
244 ADJ_MIDCHAIN_FLAG_NONE,
245 gre_build_rewrite(vnm, sw_if_index,
246 adj_get_link_type(ai),
249 gre_tunnel_stack(ai);
253 * @brief TX function. Only called L2. L3 traffic uses the adj-midchains
256 gre_interface_tx_inline (vlib_main_t * vm,
257 vlib_node_runtime_t * node,
258 vlib_frame_t * frame)
260 gre_main_t * gm = &gre_main;
262 u32 * from, * to_next, n_left_from, n_left_to_next;
263 vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
264 const gre_tunnel_t *gt = pool_elt_at_index (gm->tunnels, rd->dev_instance);
266 /* Vector of buffer / pkt indices we're supposed to process */
267 from = vlib_frame_vector_args (frame);
269 /* Number of buffers / pkts */
270 n_left_from = frame->n_vectors;
272 /* Speculatively send the first buffer to the last disposition we used */
273 next_index = node->cached_next_index;
275 while (n_left_from > 0)
277 /* set up to enqueue to our disposition with index = next_index */
278 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
284 while (n_left_from > 0 && n_left_to_next > 0)
296 b0 = vlib_get_buffer(vm, bi0);
298 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = gt->l2_adj_index;
300 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
302 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
304 tr->tunnel_id = gt - gm->tunnels;
305 tr->length = vlib_buffer_length_in_chain (vm, b0);
306 tr->src.as_u32 = gt->tunnel_src.as_u32;
307 tr->dst.as_u32 = gt->tunnel_src.as_u32;
310 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
311 to_next, n_left_to_next,
315 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
318 vlib_node_increment_counter (vm, gre_input_node.index,
319 GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
321 return frame->n_vectors;
325 gre_interface_tx (vlib_main_t * vm,
326 vlib_node_runtime_t * node,
327 vlib_frame_t * frame)
329 return (gre_interface_tx_inline (vm, node, frame));
333 gre_teb_interface_tx (vlib_main_t * vm,
334 vlib_node_runtime_t * node,
335 vlib_frame_t * frame)
337 return (gre_interface_tx_inline (vm, node, frame));
340 static u8 * format_gre_tunnel_name (u8 * s, va_list * args)
342 u32 dev_instance = va_arg (*args, u32);
343 return format (s, "gre%d", dev_instance);
346 static u8 * format_gre_tunnel_teb_name (u8 * s, va_list * args)
348 u32 dev_instance = va_arg (*args, u32);
349 return format (s, "teb-gre%d", dev_instance);
352 static u8 * format_gre_device (u8 * s, va_list * args)
354 u32 dev_instance = va_arg (*args, u32);
355 CLIB_UNUSED (int verbose) = va_arg (*args, int);
357 s = format (s, "GRE tunnel: id %d\n", dev_instance);
361 VNET_DEVICE_CLASS (gre_device_class) = {
362 .name = "GRE tunnel device",
363 .format_device_name = format_gre_tunnel_name,
364 .format_device = format_gre_device,
365 .format_tx_trace = format_gre_tx_trace,
366 .tx_function = gre_interface_tx,
367 .admin_up_down_function = gre_interface_admin_up_down,
373 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (gre_device_class,
376 VNET_DEVICE_CLASS (gre_device_teb_class) = {
377 .name = "GRE TEB tunnel device",
378 .format_device_name = format_gre_tunnel_teb_name,
379 .format_device = format_gre_device,
380 .format_tx_trace = format_gre_tx_trace,
381 .tx_function = gre_teb_interface_tx,
382 .admin_up_down_function = gre_interface_admin_up_down,
388 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (gre_device_teb_class,
389 gre_teb_interface_tx)
391 VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
393 .format_header = format_gre_header_with_length,
394 .unformat_header = unformat_gre_header,
395 .build_rewrite = gre_build_rewrite,
396 .update_adjacency = gre_update_adj,
397 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
400 static void add_protocol (gre_main_t * gm,
401 gre_protocol_t protocol,
402 char * protocol_name)
404 gre_protocol_info_t * pi;
407 vec_add2 (gm->protocol_infos, pi, 1);
408 i = pi - gm->protocol_infos;
410 pi->name = protocol_name;
411 pi->protocol = protocol;
412 pi->next_index = pi->node_index = ~0;
414 hash_set (gm->protocol_info_by_protocol, protocol, i);
415 hash_set_mem (gm->protocol_info_by_name, pi->name, i);
418 static clib_error_t * gre_init (vlib_main_t * vm)
420 gre_main_t * gm = &gre_main;
421 clib_error_t * error;
422 ip_main_t * im = &ip_main;
423 ip_protocol_info_t * pi;
425 memset (gm, 0, sizeof (gm[0]));
427 gm->vnet_main = vnet_get_main();
429 if ((error = vlib_call_init_function (vm, ip_main_init)))
432 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
435 /* Set up the ip packet generator */
436 pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
437 pi->format_header = format_gre_header;
438 pi->unformat_pg_edit = unformat_pg_gre_header;
440 gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
441 gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
442 gm->tunnel_by_key = hash_create (0, sizeof (uword));
444 #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
448 return vlib_call_init_function (vm, gre_input_init);
451 VLIB_INIT_FUNCTION (gre_init);
453 gre_main_t * gre_get_main (vlib_main_t * vm)
455 vlib_call_init_function (vm, gre_init);