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;
33 ip6_and_gre_header_t ip6_and_gre;
36 } ip6_and_gre_union_t;
39 /* Packet trace structure */
41 /* Tunnel-id / index in tunnel vector */
47 /* tunnel ip addresses */
52 u8 * format_gre_tx_trace (u8 * s, va_list * args)
54 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
55 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
56 gre_tx_trace_t * t = va_arg (*args, gre_tx_trace_t *);
58 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
59 t->tunnel_id, clib_net_to_host_u16 (t->length),
60 format_ip46_address, &t->src, IP46_TYPE_ANY,
61 format_ip46_address, &t->dst, IP46_TYPE_ANY);
65 u8 * format_gre_protocol (u8 * s, va_list * args)
67 gre_protocol_t p = va_arg (*args, u32);
68 gre_main_t * gm = &gre_main;
69 gre_protocol_info_t * pi = gre_get_protocol_info (gm, p);
72 s = format (s, "%s", pi->name);
74 s = format (s, "0x%04x", p);
79 u8 * format_gre_header_with_length (u8 * s, va_list * args)
81 gre_main_t * gm = &gre_main;
82 gre_header_t * h = va_arg (*args, gre_header_t *);
83 u32 max_header_bytes = va_arg (*args, u32);
84 gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
85 uword indent, header_bytes;
87 header_bytes = sizeof (h[0]);
88 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
89 return format (s, "gre header truncated");
91 indent = format_get_indent (s);
93 s = format (s, "GRE %U", format_gre_protocol, p);
95 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
97 gre_protocol_info_t * pi = gre_get_protocol_info (gm, p);
98 vlib_node_t * node = vlib_get_node (gm->vlib_main, pi->node_index);
99 if (node->format_buffer)
100 s = format (s, "\n%U%U",
101 format_white_space, indent,
102 node->format_buffer, (void *) (h + 1),
103 max_header_bytes - header_bytes);
109 u8 * format_gre_header (u8 * s, va_list * args)
111 gre_header_t * h = va_arg (*args, gre_header_t *);
112 return format (s, "%U", format_gre_header_with_length, h, 0);
115 /* Returns gre protocol as an int in host byte order. */
117 unformat_gre_protocol_host_byte_order (unformat_input_t * input,
120 u16 * result = va_arg (*args, u16 *);
121 gre_main_t * gm = &gre_main;
125 if (unformat_user (input, unformat_vlib_number_by_name,
126 gm->protocol_info_by_name, &i))
128 gre_protocol_info_t * pi = vec_elt_at_index (gm->protocol_infos, i);
129 *result = pi->protocol;
137 unformat_gre_protocol_net_byte_order (unformat_input_t * input,
140 u16 * result = va_arg (*args, u16 *);
141 if (! unformat_user (input, unformat_gre_protocol_host_byte_order, result))
143 *result = clib_host_to_net_u16 ((u16) *result);
148 unformat_gre_header (unformat_input_t * input, va_list * args)
150 u8 ** result = va_arg (*args, u8 **);
151 gre_header_t _h, * h = &_h;
154 if (! unformat (input, "%U",
155 unformat_gre_protocol_host_byte_order, &p))
158 h->protocol = clib_host_to_net_u16 (p);
160 /* Add header to result. */
163 u32 n_bytes = sizeof (h[0]);
165 vec_add2 (*result, p, n_bytes);
166 clib_memcpy (p, h, n_bytes);
173 gre_proto_from_vnet_link (vnet_link_t link)
178 return (GRE_PROTOCOL_ip4);
180 return (GRE_PROTOCOL_ip6);
182 return (GRE_PROTOCOL_mpls_unicast);
183 case VNET_LINK_ETHERNET:
184 return (GRE_PROTOCOL_teb);
186 return (GRE_PROTOCOL_arp);
192 return (GRE_PROTOCOL_ip4);
196 gre_build_rewrite (vnet_main_t * vnm,
198 vnet_link_t link_type,
199 const void *dst_address)
201 gre_main_t * gm = &gre_main;
202 ip4_and_gre_header_t * h4;
203 ip6_and_gre_header_t * h6;
209 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
212 /* not one of ours */
215 t = pool_elt_at_index(gm->tunnels, ti);
217 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
221 vec_validate(rewrite, sizeof(*h4)-1);
222 h4 = (ip4_and_gre_header_t*)rewrite;
223 h4->gre.protocol = clib_host_to_net_u16(gre_proto_from_vnet_link(link_type));
225 h4->ip4.ip_version_and_header_length = 0x45;
227 h4->ip4.protocol = IP_PROTOCOL_GRE;
228 /* fixup ip4 header length and checksum after-the-fact */
229 h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32;
230 h4->ip4.dst_address.as_u32 = t->tunnel_dst.fp_addr.ip4.as_u32;
231 h4->ip4.checksum = ip4_header_checksum (&h4->ip4);
235 vec_validate(rewrite, sizeof(*h6)-1);
236 h6 = (ip6_and_gre_header_t*)rewrite;
237 h6->gre.protocol = clib_host_to_net_u16(gre_proto_from_vnet_link(link_type));
239 h6->ip6.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
240 h6->ip6.hop_limit = 255;
241 h6->ip6.protocol = IP_PROTOCOL_GRE;
242 /* fixup ip6 header length and checksum after-the-fact */
243 h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
244 h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
245 h6->ip6.dst_address.as_u64[0] = t->tunnel_dst.fp_addr.ip6.as_u64[0];
246 h6->ip6.dst_address.as_u64[1] = t->tunnel_dst.fp_addr.ip6.as_u64[1];
252 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
255 gre4_fixup (vlib_main_t *vm,
261 ip0 = vlib_buffer_get_current (b0);
263 /* Fixup the checksum and len fields in the GRE tunnel encap
264 * that was applied at the midchain node */
265 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
266 ip0->checksum = ip4_header_checksum (ip0);
270 gre6_fixup (vlib_main_t *vm,
276 ip0 = vlib_buffer_get_current (b0);
278 /* Fixup the payload length field in the GRE tunnel encap that was applied
279 * at the midchain node */
280 ip0->payload_length =
281 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0))
286 gre_update_adj (vnet_main_t * vnm,
290 gre_main_t * gm = &gre_main;
295 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
296 t = pool_elt_at_index(gm->tunnels, ti);
297 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
299 adj_nbr_midchain_update_rewrite (ai, !is_ipv6 ? gre4_fixup : gre6_fixup,
300 (VNET_LINK_ETHERNET == adj_get_link_type (ai) ?
301 ADJ_FLAG_MIDCHAIN_NO_COUNT :
303 gre_build_rewrite(vnm, sw_if_index,
304 adj_get_link_type(ai),
307 gre_tunnel_stack(ai);
311 * @brief TX function. Only called L2. L3 traffic uses the adj-midchains
314 gre_interface_tx_inline (vlib_main_t * vm,
315 vlib_node_runtime_t * node,
316 vlib_frame_t * frame)
318 gre_main_t * gm = &gre_main;
320 u32 * from, * to_next, n_left_from, n_left_to_next;
321 vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
322 const gre_tunnel_t *gt = pool_elt_at_index (gm->tunnels, rd->dev_instance);
323 u8 is_ipv6 = gt->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
325 /* Vector of buffer / pkt indices we're supposed to process */
326 from = vlib_frame_vector_args (frame);
328 /* Number of buffers / pkts */
329 n_left_from = frame->n_vectors;
331 /* Speculatively send the first buffer to the last disposition we used */
332 next_index = node->cached_next_index;
334 while (n_left_from > 0)
336 /* set up to enqueue to our disposition with index = next_index */
337 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
343 while (n_left_from > 0 && n_left_to_next > 0)
355 b0 = vlib_get_buffer(vm, bi0);
357 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = gt->l2_adj_index;
359 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
361 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
363 tr->tunnel_id = gt - gm->tunnels;
364 tr->src = gt->tunnel_src;
365 tr->dst = gt->tunnel_src;
366 tr->length = vlib_buffer_length_in_chain (vm, b0);
369 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
370 to_next, n_left_to_next,
374 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
377 vlib_node_increment_counter (vm, !is_ipv6 ? gre4_input_node.index :
378 gre6_input_node.index,
379 GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
381 return frame->n_vectors;
385 gre_interface_tx (vlib_main_t * vm,
386 vlib_node_runtime_t * node,
387 vlib_frame_t * frame)
389 return (gre_interface_tx_inline (vm, node, frame));
393 gre_teb_interface_tx (vlib_main_t * vm,
394 vlib_node_runtime_t * node,
395 vlib_frame_t * frame)
397 return (gre_interface_tx_inline (vm, node, frame));
400 static u8 * format_gre_tunnel_name (u8 * s, va_list * args)
402 u32 dev_instance = va_arg (*args, u32);
403 return format (s, "gre%d", dev_instance);
406 static u8 * format_gre_tunnel_teb_name (u8 * s, va_list * args)
408 u32 dev_instance = va_arg (*args, u32);
409 return format (s, "teb-gre%d", dev_instance);
412 static u8 * format_gre_device (u8 * s, va_list * args)
414 u32 dev_instance = va_arg (*args, u32);
415 CLIB_UNUSED (int verbose) = va_arg (*args, int);
417 s = format (s, "GRE tunnel: id %d\n", dev_instance);
421 VNET_DEVICE_CLASS (gre_device_class) = {
422 .name = "GRE tunnel device",
423 .format_device_name = format_gre_tunnel_name,
424 .format_device = format_gre_device,
425 .format_tx_trace = format_gre_tx_trace,
426 .tx_function = gre_interface_tx,
427 .admin_up_down_function = gre_interface_admin_up_down,
433 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (gre_device_class,
436 VNET_DEVICE_CLASS (gre_device_teb_class) = {
437 .name = "GRE TEB tunnel device",
438 .format_device_name = format_gre_tunnel_teb_name,
439 .format_device = format_gre_device,
440 .format_tx_trace = format_gre_tx_trace,
441 .tx_function = gre_teb_interface_tx,
442 .admin_up_down_function = gre_interface_admin_up_down,
448 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (gre_device_teb_class,
449 gre_teb_interface_tx)
451 VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
453 .format_header = format_gre_header_with_length,
454 .unformat_header = unformat_gre_header,
455 .build_rewrite = gre_build_rewrite,
456 .update_adjacency = gre_update_adj,
457 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
460 static void add_protocol (gre_main_t * gm,
461 gre_protocol_t protocol,
462 char * protocol_name)
464 gre_protocol_info_t * pi;
467 vec_add2 (gm->protocol_infos, pi, 1);
468 i = pi - gm->protocol_infos;
470 pi->name = protocol_name;
471 pi->protocol = protocol;
472 pi->next_index = pi->node_index = ~0;
474 hash_set (gm->protocol_info_by_protocol, protocol, i);
475 hash_set_mem (gm->protocol_info_by_name, pi->name, i);
478 static clib_error_t * gre_init (vlib_main_t * vm)
480 gre_main_t * gm = &gre_main;
481 clib_error_t * error;
482 ip_main_t * im = &ip_main;
483 ip_protocol_info_t * pi;
485 memset (gm, 0, sizeof (gm[0]));
487 gm->vnet_main = vnet_get_main();
489 if ((error = vlib_call_init_function (vm, ip_main_init)))
492 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
495 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
498 /* Set up the ip packet generator */
499 pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
500 pi->format_header = format_gre_header;
501 pi->unformat_pg_edit = unformat_pg_gre_header;
503 gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
504 gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
505 gm->tunnel_by_key4 = hash_create (0, sizeof (uword));
506 gm->tunnel_by_key6 = hash_create_mem (0, sizeof(u64[4]), sizeof (uword));
508 #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
512 return vlib_call_init_function (vm, gre_input_init);
515 VLIB_INIT_FUNCTION (gre_init);
517 gre_main_t * gre_get_main (vlib_main_t * vm)
519 vlib_call_init_function (vm, gre_init);