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>
21 #include <vnet/ip/format.h>
23 u8 * format_gre_tunnel (u8 * s, va_list * args)
25 gre_tunnel_t * t = va_arg (*args, gre_tunnel_t *);
26 gre_main_t * gm = &gre_main;
29 "[%d] %U (src) %U (dst) payload %s outer_fib_index %d",
31 format_ip4_address, &t->tunnel_src,
32 format_ip4_address, &t->tunnel_dst,
33 (t->teb ? "teb" : "ip"),
38 int vnet_gre_add_del_tunnel
39 (vnet_gre_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
41 gre_main_t * gm = &gre_main;
42 vnet_main_t * vnm = gm->vnet_main;
43 ip4_main_t * im = &ip4_main;
45 vnet_hw_interface_t * hi;
46 u32 hw_if_index, sw_if_index;
54 key = (u64)a->src.as_u32 << 32 | (u64)a->dst.as_u32;
55 p = hash_get (gm->tunnel_by_key, key);
58 /* check if same src/dst pair exists */
60 return VNET_API_ERROR_INVALID_VALUE;
62 p = hash_get (im->fib_index_by_table_id, a->outer_fib_id);
64 return VNET_API_ERROR_NO_SUCH_FIB;
66 outer_fib_index = p[0];
68 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
69 memset (t, 0, sizeof (*t));
71 if (vec_len (gm->free_gre_tunnel_hw_if_indices) > 0) {
72 vnet_interface_main_t * im = &vnm->interface_main;
74 hw_if_index = gm->free_gre_tunnel_hw_if_indices
75 [vec_len (gm->free_gre_tunnel_hw_if_indices)-1];
76 _vec_len (gm->free_gre_tunnel_hw_if_indices) -= 1;
78 hi = vnet_get_hw_interface (vnm, hw_if_index);
79 hi->dev_instance = t - gm->tunnels;
80 hi->hw_instance = hi->dev_instance;
82 /* clear old stats of freed tunnel before reuse */
83 sw_if_index = hi->sw_if_index;
84 vnet_interface_counter_lock(im);
85 vlib_zero_combined_counter
86 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
87 vlib_zero_combined_counter
88 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
89 vlib_zero_simple_counter
90 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
91 vnet_interface_counter_unlock(im);
95 /* Default MAC address (d00b:eed0:0000 + sw_if_index) */
96 memset (address, 0, sizeof (address));
101 address[4] = t - gm->tunnels;
103 error = ethernet_register_interface
105 gre_device_class.index, t - gm->tunnels, address, &hw_if_index,
110 clib_error_report (error);
111 return VNET_API_ERROR_INVALID_REGISTRATION;
114 hw_if_index = vnet_register_interface
115 (vnm, gre_device_class.index, t - gm->tunnels,
116 gre_hw_interface_class.index,
119 hi = vnet_get_hw_interface (vnm, hw_if_index);
120 sw_if_index = hi->sw_if_index;
123 t->hw_if_index = hw_if_index;
124 t->outer_fib_index = outer_fib_index;
125 t->sw_if_index = sw_if_index;
127 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
128 gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels;
130 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
131 im->fib_index_by_sw_if_index[sw_if_index] = t->outer_fib_index;
133 hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
134 hi->per_packet_overhead_bytes =
135 /* preamble */ 8 + /* inter frame gap */ 12;
137 /* Standard default gre MTU. */
138 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
141 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
142 clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
144 hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
146 slot = vlib_node_add_named_next_with_slot
147 (vnm->vlib_main, hi->tx_node_index, "ip4-lookup", GRE_OUTPUT_NEXT_LOOKUP);
149 ASSERT (slot == GRE_OUTPUT_NEXT_LOOKUP);
151 } else { /* !is_add => delete */
152 /* tunnel needs to exist */
154 return VNET_API_ERROR_NO_SUCH_ENTRY;
156 t = pool_elt_at_index (gm->tunnels, p[0]);
158 sw_if_index = t->sw_if_index;
159 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */);
160 /* make sure tunnel is removed from l2 bd or xconnect */
161 set_int_l2_mode(gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
162 vec_add1 (gm->free_gre_tunnel_hw_if_indices, t->hw_if_index);
163 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
165 hash_unset (gm->tunnel_by_key, key);
166 pool_put (gm->tunnels, t);
170 *sw_if_indexp = sw_if_index;
176 static clib_error_t *
177 create_gre_tunnel_command_fn (vlib_main_t * vm,
178 unformat_input_t * input,
179 vlib_cli_command_t * cmd)
181 unformat_input_t _line_input, * line_input = &_line_input;
182 vnet_gre_add_del_tunnel_args_t _a, * a = &_a;
183 ip4_address_t src, dst;
184 u32 outer_fib_id = 0;
191 /* Get a line of input. */
192 if (! unformat_user (input, unformat_line_input, line_input))
195 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
196 if (unformat (line_input, "del"))
198 else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
200 else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
202 else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
204 else if (unformat (line_input, "teb"))
207 return clib_error_return (0, "unknown input `%U'",
208 format_unformat_error, input);
210 unformat_free (line_input);
213 return clib_error_return (0, "mandatory argument(s) missing");
215 if (memcmp (&src, &dst, sizeof(src)) == 0)
216 return clib_error_return (0, "src and dst are identical");
218 memset (a, 0, sizeof (*a));
220 a->outer_fib_id = outer_fib_id;
222 clib_memcpy(&a->src, &src, sizeof(src));
223 clib_memcpy(&a->dst, &dst, sizeof(dst));
225 rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
230 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
232 case VNET_API_ERROR_INVALID_VALUE:
233 return clib_error_return (0, "GRE tunnel already exists...");
234 case VNET_API_ERROR_NO_SUCH_FIB:
235 return clib_error_return (0, "outer fib ID %d doesn't exist\n",
238 return clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
244 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
245 .path = "create gre tunnel",
246 .short_help = "create gre tunnel src <addr> dst <addr> "
247 "[outer-fib-id <fib>] [teb] [del]",
248 .function = create_gre_tunnel_command_fn,
251 static clib_error_t *
252 show_gre_tunnel_command_fn (vlib_main_t * vm,
253 unformat_input_t * input,
254 vlib_cli_command_t * cmd)
256 gre_main_t * gm = &gre_main;
259 if (pool_elts (gm->tunnels) == 0)
260 vlib_cli_output (vm, "No GRE tunnels configured...");
262 pool_foreach (t, gm->tunnels,
264 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
270 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
271 .path = "show gre tunnel",
272 .function = show_gre_tunnel_command_fn,
275 /* force inclusion from application's main.c */
276 clib_error_t *gre_interface_init (vlib_main_t *vm)
280 VLIB_INIT_FUNCTION(gre_interface_init);