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) outer_fib_index %d",
31 format_ip4_address, &t->tunnel_src,
32 format_ip4_address, &t->tunnel_dst,
37 int vnet_gre_add_del_tunnel
38 (vnet_gre_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
40 gre_main_t * gm = &gre_main;
41 vnet_main_t * vnm = gm->vnet_main;
42 ip4_main_t * im = &ip4_main;
44 vnet_hw_interface_t * hi;
45 u32 hw_if_index, sw_if_index;
51 key = (u64)a->src.as_u32 << 32 | (u64)a->dst.as_u32;
52 p = hash_get (gm->tunnel_by_key, key);
55 /* check if same src/dst pair exists */
57 return VNET_API_ERROR_INVALID_VALUE;
59 p = hash_get (im->fib_index_by_table_id, a->outer_fib_id);
61 return VNET_API_ERROR_NO_SUCH_FIB;
63 outer_fib_index = p[0];
65 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
66 memset (t, 0, sizeof (*t));
68 if (vec_len (gm->free_gre_tunnel_hw_if_indices) > 0) {
69 vnet_interface_main_t * im = &vnm->interface_main;
71 hw_if_index = gm->free_gre_tunnel_hw_if_indices
72 [vec_len (gm->free_gre_tunnel_hw_if_indices)-1];
73 _vec_len (gm->free_gre_tunnel_hw_if_indices) -= 1;
75 hi = vnet_get_hw_interface (vnm, hw_if_index);
76 hi->dev_instance = t - gm->tunnels;
77 hi->hw_instance = hi->dev_instance;
79 /* clear old stats of freed tunnel before reuse */
80 sw_if_index = hi->sw_if_index;
81 vnet_interface_counter_lock(im);
82 vlib_zero_combined_counter
83 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
84 vlib_zero_combined_counter
85 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
86 vlib_zero_simple_counter
87 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
88 vnet_interface_counter_unlock(im);
90 hw_if_index = vnet_register_interface
91 (vnm, gre_device_class.index, t - gm->tunnels,
92 gre_hw_interface_class.index,
94 hi = vnet_get_hw_interface (vnm, hw_if_index);
95 sw_if_index = hi->sw_if_index;
98 t->hw_if_index = hw_if_index;
99 t->outer_fib_index = outer_fib_index;
100 t->sw_if_index = sw_if_index;
102 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
103 gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels;
105 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
106 im->fib_index_by_sw_if_index[sw_if_index] = t->outer_fib_index;
108 hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
109 hi->per_packet_overhead_bytes =
110 /* preamble */ 8 + /* inter frame gap */ 12;
112 /* Standard default gre MTU. */
113 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
115 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
116 clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
118 hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
120 slot = vlib_node_add_named_next_with_slot
121 (vnm->vlib_main, hi->tx_node_index, "ip4-lookup", GRE_OUTPUT_NEXT_LOOKUP);
123 ASSERT (slot == GRE_OUTPUT_NEXT_LOOKUP);
125 } else { /* !is_add => delete */
126 /* tunnel needs to exist */
128 return VNET_API_ERROR_NO_SUCH_ENTRY;
130 t = pool_elt_at_index (gm->tunnels, p[0]);
132 sw_if_index = t->sw_if_index;
133 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */);
134 /* make sure tunnel is removed from l2 bd or xconnect */
135 set_int_l2_mode(gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
136 vec_add1 (gm->free_gre_tunnel_hw_if_indices, t->hw_if_index);
137 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
139 hash_unset (gm->tunnel_by_key, key);
140 pool_put (gm->tunnels, t);
144 *sw_if_indexp = sw_if_index;
150 static clib_error_t *
151 create_gre_tunnel_command_fn (vlib_main_t * vm,
152 unformat_input_t * input,
153 vlib_cli_command_t * cmd)
155 unformat_input_t _line_input, * line_input = &_line_input;
156 vnet_gre_add_del_tunnel_args_t _a, * a = &_a;
157 ip4_address_t src, dst;
158 u32 outer_fib_id = 0;
164 /* Get a line of input. */
165 if (! unformat_user (input, unformat_line_input, line_input))
168 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
169 if (unformat (line_input, "del"))
171 else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
173 else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
175 else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
178 return clib_error_return (0, "unknown input `%U'",
179 format_unformat_error, input);
181 unformat_free (line_input);
184 return clib_error_return (0, "mandatory argument(s) missing");
186 if (memcmp (&src, &dst, sizeof(src)) == 0)
187 return clib_error_return (0, "src and dst are identical");
189 memset (a, 0, sizeof (*a));
191 a->outer_fib_id = outer_fib_id;
192 clib_memcpy(&a->src, &src, sizeof(src));
193 clib_memcpy(&a->dst, &dst, sizeof(dst));
195 rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
200 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
202 case VNET_API_ERROR_INVALID_VALUE:
203 return clib_error_return (0, "GRE tunnel already exists...");
204 case VNET_API_ERROR_NO_SUCH_FIB:
205 return clib_error_return (0, "outer fib ID %d doesn't exist\n",
208 return clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
214 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
215 .path = "create gre tunnel",
216 .short_help = "create gre tunnel src <addr> dst <addr> "
217 "[outer-fib-id <fib>] [del]",
218 .function = create_gre_tunnel_command_fn,
221 static clib_error_t *
222 show_gre_tunnel_command_fn (vlib_main_t * vm,
223 unformat_input_t * input,
224 vlib_cli_command_t * cmd)
226 gre_main_t * gm = &gre_main;
229 if (pool_elts (gm->tunnels) == 0)
230 vlib_cli_output (vm, "No GRE tunnels configured...");
232 pool_foreach (t, gm->tunnels,
234 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
240 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
241 .path = "show gre tunnel",
242 .function = show_gre_tunnel_command_fn,
245 /* force inclusion from application's main.c */
246 clib_error_t *gre_interface_init (vlib_main_t *vm)
250 VLIB_INIT_FUNCTION(gre_interface_init);