ae59b2466081470702fe79c17bca452ac935d5bf
[vpp.git] / vnet / vnet / gre / interface.c
1 /*
2  * gre_interface.c: gre interfaces
3  *
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:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/gre/gre.h>
21 #include <vnet/ip/format.h>
22
23 u8 * format_gre_tunnel (u8 * s, va_list * args)
24 {
25   gre_tunnel_t * t = va_arg (*args, gre_tunnel_t *);
26   gre_main_t * gm = &gre_main;
27
28   s = format (s,
29               "[%d] %U (src) %U (dst) outer_fib_index %d",
30               t - gm->tunnels,
31               format_ip4_address, &t->tunnel_src,
32               format_ip4_address, &t->tunnel_dst,
33               t->outer_fib_index);
34   return s;
35 }
36
37 int vnet_gre_add_del_tunnel
38   (vnet_gre_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
39 {
40   gre_main_t * gm = &gre_main;
41   vnet_main_t * vnm = gm->vnet_main;
42   ip4_main_t * im = &ip4_main;
43   gre_tunnel_t * t;
44   vnet_hw_interface_t * hi;
45   u32 hw_if_index, sw_if_index;
46   u32 slot;
47   u32 outer_fib_index;
48   uword * p;
49   u64 key;
50
51   key = (u64)a->src.as_u32 << 32 | (u64)a->dst.as_u32;
52   p = hash_get (gm->tunnel_by_key, key);
53
54   if (a->is_add) {
55     /* check if same src/dst pair exists */
56     if (p)
57       return VNET_API_ERROR_INVALID_VALUE;
58
59     p = hash_get (im->fib_index_by_table_id, a->outer_table_id);
60     if (! p)
61       return VNET_API_ERROR_NO_SUCH_FIB;
62
63     outer_fib_index = p[0];
64
65     pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
66     memset (t, 0, sizeof (*t));
67
68     if (vec_len (gm->free_vxlan_tunnel_hw_if_indices) > 0) {
69         vnet_interface_main_t * im = &vnm->interface_main;
70
71         hw_if_index = gm->free_vxlan_tunnel_hw_if_indices
72           [vec_len (gm->free_vxlan_tunnel_hw_if_indices)-1];
73           _vec_len (gm->free_vxlan_tunnel_hw_if_indices) -= 1;
74
75         hi = vnet_get_hw_interface (vnm, hw_if_index);
76         hi->dev_instance = t - gm->tunnels;
77         hi->hw_instance = hi->dev_instance;
78
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);
89     } else {
90         hw_if_index = vnet_register_interface
91           (vnm, gre_device_class.index, t - gm->tunnels,
92            gre_hw_interface_class.index,
93            t - gm->tunnels);
94         hi = vnet_get_hw_interface (vnm, hw_if_index);
95         sw_if_index = hi->sw_if_index;
96     }
97
98     t->hw_if_index = hw_if_index;
99     t->outer_fib_index = outer_fib_index;
100     t->sw_if_index = sw_if_index;
101
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;
104
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;
107
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;
111
112     /* Standard default gre MTU. */
113     hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
114
115     clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
116     clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
117
118     hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
119
120     slot = vlib_node_add_named_next_with_slot
121       (vnm->vlib_main, hi->tx_node_index, "ip4-lookup", GRE_OUTPUT_NEXT_LOOKUP);
122
123     ASSERT (slot == GRE_OUTPUT_NEXT_LOOKUP);
124
125   } else { /* !is_add => delete */
126     /* tunnel needs to exist */
127     if (! p)
128       return VNET_API_ERROR_NO_SUCH_ENTRY;
129
130     t = pool_elt_at_index (gm->tunnels, p[0]);
131
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_vxlan_tunnel_hw_if_indices, t->hw_if_index);
137     gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
138
139     hash_unset (gm->tunnel_by_key, key);
140     pool_put (gm->tunnels, t);
141   }
142
143   if (sw_if_indexp)
144     *sw_if_indexp = sw_if_index;
145
146   return 0;
147 }
148
149
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)
154 {
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;
159   int rv;
160   u32 num_m_args = 0;
161   u8 is_add = 1;
162   u32 sw_if_index;
163
164   /* Get a line of input. */
165   if (! unformat_user (input, unformat_line_input, line_input))
166     return 0;
167
168   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
169     if (unformat (line_input, "del"))
170       is_add = 0;
171     else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
172       num_m_args++;
173     else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
174       num_m_args++;
175     else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
176       ;
177     else
178       return clib_error_return (0, "unknown input `%U'",
179                                 format_unformat_error, input);
180   }
181   unformat_free (line_input);
182
183   if (num_m_args < 2)
184       return clib_error_return (0, "mandatory argument(s) missing");
185
186   if (memcmp (&src, &dst, sizeof(src)) == 0)
187       return clib_error_return (0, "src and dst are identical");
188
189   memset (a, 0, sizeof (*a));
190   a->is_add = is_add;
191   a->outer_table_id = outer_fib_id;
192   clib_memcpy(&a->src, &src, sizeof(src));
193   clib_memcpy(&a->dst, &dst, sizeof(dst));
194
195   rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
196
197   switch(rv)
198     {
199     case 0:
200       vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
201       break;
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",
206                                 outer_fib_id);
207     default:
208       return clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
209     }
210
211   return 0;
212 }
213
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,
219 };
220
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)
225 {
226   gre_main_t * gm = &gre_main;
227   gre_tunnel_t * t;
228
229   if (pool_elts (gm->tunnels) == 0)
230     vlib_cli_output (vm, "No GRE tunnels configured...");
231
232   pool_foreach (t, gm->tunnels,
233   ({
234     vlib_cli_output (vm, "%U", format_gre_tunnel, t);
235   }));
236
237   return 0;
238 }
239
240 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
241     .path = "show gre tunnel",
242     .function = show_gre_tunnel_command_fn,
243 };
244
245 /* force inclusion from application's main.c */
246 clib_error_t *gre_interface_init (vlib_main_t *vm)
247 {
248   return 0;
249 }
250 VLIB_INIT_FUNCTION(gre_interface_init);