864c384b992b0a58a8adc4df45615337b2169c72
[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) payload %s outer_fib_index %d",
30               t - gm->tunnels,
31               format_ip4_address, &t->tunnel_src,
32               format_ip4_address, &t->tunnel_dst,
33               (t->teb ? "teb" : "ip"),
34               t->outer_fib_index);
35   return s;
36 }
37
38 int vnet_gre_add_del_tunnel
39   (vnet_gre_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
40 {
41   gre_main_t * gm = &gre_main;
42   vnet_main_t * vnm = gm->vnet_main;
43   ip4_main_t * im = &ip4_main;
44   gre_tunnel_t * t;
45   vnet_hw_interface_t * hi;
46   u32 hw_if_index, sw_if_index;
47   u32 slot;
48   u32 outer_fib_index;
49   uword * p;
50   u64 key;
51   u8 address[6];
52   clib_error_t *error;
53
54   key = (u64)a->src.as_u32 << 32 | (u64)a->dst.as_u32;
55   p = hash_get (gm->tunnel_by_key, key);
56
57   if (a->is_add) {
58     /* check if same src/dst pair exists */
59     if (p)
60       return VNET_API_ERROR_INVALID_VALUE;
61
62     p = hash_get (im->fib_index_by_table_id, a->outer_fib_id);
63     if (! p)
64       return VNET_API_ERROR_NO_SUCH_FIB;
65
66     outer_fib_index = p[0];
67
68     pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
69     memset (t, 0, sizeof (*t));
70
71     if (vec_len (gm->free_gre_tunnel_hw_if_indices) > 0) {
72         vnet_interface_main_t * im = &vnm->interface_main;
73
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;
77
78         hi = vnet_get_hw_interface (vnm, hw_if_index);
79         hi->dev_instance = t - gm->tunnels;
80         hi->hw_instance = hi->dev_instance;
81
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);
92     } else {
93       if (a->teb)
94       {
95         /* Default MAC address (d00b:eed0:0000 + sw_if_index) */
96         memset (address, 0, sizeof (address));
97         address[0] = 0xd0;
98         address[1] = 0x0b;
99         address[2] = 0xee;
100         address[3] = 0xd0;
101         address[4] = t - gm->tunnels;
102
103         error = ethernet_register_interface
104           (vnm,
105            gre_device_class.index, t - gm->tunnels, address, &hw_if_index,
106            0);
107
108         if (error)
109         {
110           clib_error_report (error);
111           return VNET_API_ERROR_INVALID_REGISTRATION;
112         }
113       } else {
114         hw_if_index = vnet_register_interface
115           (vnm, gre_device_class.index, t - gm->tunnels,
116            gre_hw_interface_class.index,
117            t - gm->tunnels);
118       }
119       hi = vnet_get_hw_interface (vnm, hw_if_index);
120       sw_if_index = hi->sw_if_index;
121     }
122
123     t->hw_if_index = hw_if_index;
124     t->outer_fib_index = outer_fib_index;
125     t->sw_if_index = sw_if_index;
126
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;
129
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;
132
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;
136
137     /* Standard default gre MTU. */
138     hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
139
140     t->teb = a->teb;
141     clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
142     clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
143
144     hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
145
146     slot = vlib_node_add_named_next_with_slot
147       (vnm->vlib_main, hi->tx_node_index, "ip4-lookup", GRE_OUTPUT_NEXT_LOOKUP);
148
149     ASSERT (slot == GRE_OUTPUT_NEXT_LOOKUP);
150
151   } else { /* !is_add => delete */
152     /* tunnel needs to exist */
153     if (! p)
154       return VNET_API_ERROR_NO_SUCH_ENTRY;
155
156     t = pool_elt_at_index (gm->tunnels, p[0]);
157
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;
164
165     hash_unset (gm->tunnel_by_key, key);
166     pool_put (gm->tunnels, t);
167   }
168
169   if (sw_if_indexp)
170     *sw_if_indexp = sw_if_index;
171
172   return 0;
173 }
174
175
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)
180 {
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;
185   u8 teb = 0;
186   int rv;
187   u32 num_m_args = 0;
188   u8 is_add = 1;
189   u32 sw_if_index;
190
191   /* Get a line of input. */
192   if (! unformat_user (input, unformat_line_input, line_input))
193     return 0;
194
195   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
196     if (unformat (line_input, "del"))
197       is_add = 0;
198     else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
199       num_m_args++;
200     else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
201       num_m_args++;
202     else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
203       ;
204     else if (unformat (line_input, "teb"))
205       teb = 1;
206     else
207       return clib_error_return (0, "unknown input `%U'",
208                                 format_unformat_error, input);
209   }
210   unformat_free (line_input);
211
212   if (num_m_args < 2)
213       return clib_error_return (0, "mandatory argument(s) missing");
214
215   if (memcmp (&src, &dst, sizeof(src)) == 0)
216       return clib_error_return (0, "src and dst are identical");
217
218   memset (a, 0, sizeof (*a));
219   a->is_add = is_add;
220   a->outer_fib_id = outer_fib_id;
221   a->teb = teb;
222   clib_memcpy(&a->src, &src, sizeof(src));
223   clib_memcpy(&a->dst, &dst, sizeof(dst));
224
225   rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
226
227   switch(rv)
228     {
229     case 0:
230       vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
231       break;
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",
236                                 outer_fib_id);
237     default:
238       return clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
239     }
240
241   return 0;
242 }
243
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,
249 };
250
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)
255 {
256   gre_main_t * gm = &gre_main;
257   gre_tunnel_t * t;
258
259   if (pool_elts (gm->tunnels) == 0)
260     vlib_cli_output (vm, "No GRE tunnels configured...");
261
262   pool_foreach (t, gm->tunnels,
263   ({
264     vlib_cli_output (vm, "%U", format_gre_tunnel, t);
265   }));
266
267   return 0;
268 }
269
270 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
271     .path = "show gre tunnel",
272     .function = show_gre_tunnel_command_fn,
273 };
274
275 /* force inclusion from application's main.c */
276 clib_error_t *gre_interface_init (vlib_main_t *vm)
277 {
278   return 0;
279 }
280 VLIB_INIT_FUNCTION(gre_interface_init);