2 * gre_interface.c: gre interfaces
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/pg/pg.h>
20 #include <vnet/gre/gre.h>
23 gre_register_interface (vnet_main_t * vnm,
25 ip4_address_t *tunnel_src,
26 ip4_address_t *tunnel_dst,
28 u32 * gi_index_return)
30 gre_main_t * gm = &gre_main;
31 ip4_main_t * im = &ip4_main;
33 vnet_hw_interface_t * hi;
39 u64 key = (u64)tunnel_src->as_u32 << 32 | (u64)tunnel_dst->as_u32;
41 /* check if same src/dst pair exists */
42 if (hash_get (gm->tunnel_by_key, key))
43 return VNET_API_ERROR_INVALID_VALUE;
45 p = hash_get (im->fib_index_by_table_id, outer_fib_id);
47 return VNET_API_ERROR_NO_SUCH_FIB;
49 outer_fib_index = p[0];
51 pool_get (gm->tunnels, t);
52 memset (t, 0, sizeof (*t));
54 hw_if_index = vnet_register_interface
55 (vnm, gre_device_class.index, t - gm->tunnels,
56 gre_hw_interface_class.index,
59 *gi_index_return = t - gm->tunnels;
61 t->hw_if_index = hw_if_index;
62 t->outer_fib_index = outer_fib_index;
64 hi = vnet_get_hw_interface (vnm, hw_if_index);
66 hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
67 hi->per_packet_overhead_bytes =
68 /* preamble */ 8 + /* inter frame gap */ 12;
70 /* Standard default gre MTU. */
71 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 1500;
73 memcpy (&t->tunnel_src, tunnel_src, sizeof (t->tunnel_src));
74 memcpy (&t->tunnel_dst, tunnel_dst, sizeof (t->tunnel_dst));
76 hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
78 slot = vlib_node_add_named_next_with_slot
79 (vnm->vlib_main, hi->tx_node_index, "ip4-lookup", GRE_OUTPUT_NEXT_LOOKUP);
81 ASSERT (slot == GRE_OUTPUT_NEXT_LOOKUP);
88 create_gre_tunnel_command_fn (vlib_main_t * vm,
89 unformat_input_t * input,
90 vlib_cli_command_t * cmd)
92 unformat_input_t _line_input, * line_input = &_line_input;
93 vnet_main_t * vnm = vnet_get_main();
94 ip4_address_t src, dst;
100 /* Get a line of input. */
101 if (! unformat_user (input, unformat_line_input, line_input))
104 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
105 if (unformat (line_input, "src %U", unformat_ip4_address, &src))
107 else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
109 else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
112 return clib_error_return (0, "unknown input `%U'",
113 format_unformat_error, input);
115 unformat_free (line_input);
118 return clib_error_return (0, "mandatory argument(s) missing");
120 rv = gre_register_interface (vnm, gre_hw_interface_class.index,
121 &src, &dst, outer_fib_id, &gi_index);
127 case VNET_API_ERROR_INVALID_VALUE:
128 return clib_error_return (0, "GRE tunnel already exists...");
129 case VNET_API_ERROR_NO_SUCH_FIB:
130 return clib_error_return (0, "outer fib ID %d doesn't exist\n",
133 return clib_error_return (0, "gre_register_interface returned %d", rv);
139 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
140 .path = "create gre tunnel",
141 .short_help = "create gre tunnel src <addr> dst <addr> [outer-fib-id <fib>]",
142 .function = create_gre_tunnel_command_fn,
145 /* force inclusion from application's main.c */
146 clib_error_t *gre_interface_init (vlib_main_t *vm)
150 VLIB_INIT_FUNCTION(gre_interface_init);